

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


This two-part homework explores the background and review material covered in Lectures 1 and 2. Part 1 is a set of short problems, similar to problems that might appear on a test. Part 2 is a short pro- gramming problem. These problems are not particularly difficult. Students who feel uncomfortable about their programming background should view this as an opportunity to gauge their current skills. Program- ming assignments will become more challenging soon, so if you struggle with this one, you should work hard to practice and catch up. Before starting this assignment, make sure you have read and understood the statement of Academic Integrity for Programming Assignments.
While it is clearly possible for you to create a program file containing the code below and then compile and execute the program, you will not have the luxury of doing this on a test. You should practice by studying the code carefully yourself first and trying to determine the answers. This is a good skill to develop. In your main CSII folder on your laptop, create a subfolder for homeworks and within that folder create a subfolder hw1 for this assignment. Create and open a plain text file named part1.txt and write your solutions for Part 1 in this file.
#include
void stats_calculation(float values[], int &n, float &max_value, float min_value, float &avg) { // Find the average value float sum = values[0]; for (int i=0; i<n; ++i) sum += values[n]; avg /= n;
// Find the maximum and minimum values. min_value = values[n-1], max_value = values[n-1]; while (n > 0) { --n; if (values[n] > max_value) max_value = values[n]; else if (values[n] < min_value) min_value = values[n]; } }
int main() { // Initialize an array in order to test the function. The size of // the array (6) is automatically determined from the number of // values in the list. float a[] = { 12.3, 15.4, 1.5, 7.8, 2.3, 8.9 }; int size = 6; float min_value = 0, max_value = 0, average = 0;
stats_calculation(a, size, max_value, min_value, average);
cout << "Here are the values: "; for (int i=0; i<size; ++i) cout << a[i] << " "; cout << ’\n’; cout << "average = " << average << ’\n’ << "max = " << max_value << ’\n’ << "min = " << min_value << ’\n’; return 0; }
#include
int main() { int x=5, y=4; float a[3] = { 1.0, 2.0, 3.0 };
if (x > y) { float y = 1.0; int a = 6; cout << "x = " << x << ", y = " << y << ", a = " << a << endl; x = 2; }
cout << "x = " << x << ", y = " << y << ", a[0] = " << a[0] << endl; return 0; }
// Count the number of entries in an array that have both an even // index (subscript) and an even value. int count_evens( int arr[], int n ) { int count = 0; for (unsigned int i=0; i<n; i+=2) if (arr[i] % 2 == 0) ++count; return count; }
// Given an array of floats, form an array of sums. Entry 0 // in the array of sums is the addition of all values in the // array. Entry 1 is the addition of values in locations // 1..n-1. In general, entry i is the addition of the values // in locations i..n-1. void subsequence_sum( float arr[], int n, float sums[] ) { for (int i = 0; i < n; ++i) { sums[i] = 0.0; for ( int j=i; j<n; ++j ) sums[i] += arr[j]; } }