CSUN COMP 106 - Exercises in Array Programming

Unformatted text preview:

Array programming exercises April 25, 20061Exercises in Array ProgrammingExercises in Array ProgrammingLarry CarettoComputer Science 106Computing in Engineering and ScienceApril 25, 20062Outline• Review introduction to arrays• Review writing code with arrays and for loops– Array sums– Finding maximum and minimum elements in an array– Data processing with arrays• Exercises in array programming3Representing DataDataRun14.113.212.511.814.412.3543210C++Mathx[5]x[4]x[3]x[2]x[1]x[0]x5x4x3x2x1x0• C++ array, x[i] for math xi• Maximum subscript = number of elements minus one4General Array Processing• To process each element in an array with N elements, starting with the initial element, use a for loop with index k starting at zero and < Nfor ( int k = 0; k < N; k++ )• To process a subset of elements in the array starting at element F and ending with (and including) element Lfor ( int k = F; k <= L; k++)5File Input Screen Outputconst int MAX_SIZE = 100;double z[MAX_SIZE];ifstream inFile( “array.dat” );for (int i = 0; i < MAX_SIZE; i++){ inFile >> z[i]; cout << “\nz[“ << i << “] = ”<< z[i]; }6Console Input File Outputconst int MAX_SIZE = 100;double z[MAX_SIZE];ofstream outFile( “array.dat” );for ( int i = 0; i < MAX_SIZE; i++ ){ cout << “\nEnter z[“ << i << “]: “;cin >> z[i]outFile << z[i] << endl; }Array programming exercises April 25, 200627Defined Elements• The number of elements defined may be less then the array size• You may declare an array to be the maximum size expected but actually specify a value for fewer elementsdouble x[10];for ( int j = 1; j < 5; j++ )x[j] = 1 / double( j );8Computing the Meansum = 0;for( i = 0; i < N; i++ ){ sum += x[i]; }average = sum / N;∑∑−====10111NiiNiixNxNx• N data items to average• Subscripts starts at 0• Last data item is element N-1 in array•{}not needed9• Store the current maximum in a variable (e.g., max) and compare max to new array values• If a new value is greater than maxreplace max by that valuedouble max = x[0];// initialize maxfor ( int i = 1; i < N; i++ ) { if ( x[i] > max ){ max = x[i]; }}Finding the Maximum• Can omit both sets of braces10• Store the current minimum in a variable (e.g., min) and compare min to new array values• If a new value is greater than minreplace min by that valuedouble min = x[0];// initialize minfor ( int i = 1; i < N; i++ ) { if ( x[i] < min ){ min = x[i]; }}Finding the Minimum• Can omit both sets of braces11Array Processing Example• You have taken current and voltage data from a circuit• There are N pairs of data• Current is stored as the amps[k] array and voltage as the volts[k] array• Write the code to compute the average power if arrays are already declared and data on N, volts[] and amps[] are already input12Average Power Onedouble sum = 0;for ( int k = 0; k < N; k++ ){power[k] = amps[k] * volts[k];sum += power[k];}double averagePower = sum / N;cout << “Power = “ << averagePower<< “ watts”;The power array could be used in subsequent (or the same) loopsArray programming exercises April 25, 2006313Average Power Twodouble sum = 0for ( int k = 0; k < N; k++ ){power = amps[k] * volts[k];sum += power;}double averagePower = sum / N;cout << “Power = “ << averagePower<< “ watts”;The power variable can be used in the same loop only14Average Power Threedouble sum = 0for ( int k = 0; k < N; k++ ){sum += amps[k] * volts[k];}double averagePower = sum / N;cout << “Power = “ << averagePower<< “ watts”;The power calculation has to be redone if we want to use power[k] subsequently15Standard Deviation Exercise• Measure of spread around mean∑−=102Niix()()11112101022102102−⎟⎠⎞⎜⎝⎛−⎟⎠⎞⎜⎝⎛=−−⎟⎠⎞⎜⎝⎛=−−=∑∑∑∑−=−=−=−=NxNxNxNxNxxsNiiNiiNiiNii• Use final equation to write code to compute standard deviation • Hint: Compute two sums in a single for loop: and ∑−=10Niix16double sum = 0, sum2 = 0;for ( int k = 0; k < N; k++ ){sum += x[k];sum2 += x[k] * x[k];}return sqrt( ( sum2 – sum * sum )/ N ) / ( N – 1 ) );} Standard Deviation Code11210102−⎟⎠⎞⎜⎝⎛−⎟⎠⎞⎜⎝⎛=∑∑−=−=NxNxsNiiNii17Calculating Tables with Arrays• We have previously calculated tables where the step between variables was the same• How can we construct a table with uneven step sizes in the variables?– We can define one or two arrays for a one-way or two-way table– Each array has all the values that we want in the independent value of the table18Calculating One-Way Tables25516493421100f(x)x40020100104971644211f(x)xEven increments in x; no array requiredUneven increments in x; array required for xArray programming exercises April 25, 2006419Two-way, Both Fixed Increment27.022.518.013.59.04.5V = 312.010.08.06.04.02.0V = 23.02.52.01.51.00.5V = 1m = 6m = 5m = 4m = 3m = 2m = 1Kinetic Energy Table ( m = mass in kg, V = velocity in m/s and kinetic energy in joules)• When both independent variables have fixed increments, no arrays are required– Exercise six and project two code20Even Increments In Both• KE with even step sizes in both mass and velocityfor ( int v = 1; v <= 3; v++ ) {cout << setprecision(0) << “\nv = “ << v<< setprecision(1);for ( int m = 1; m < 7; m++ ) {cout << setw(7)<< 0.5 * m * v * v;}21Two-way, Variable Columns90.045.022.59.04.5V = 340.020.010.04.02.0V = 210.05.02.51.00.5V = 1m = 20m = 10m = 5m = 2m = 1Kinetic Energy Table ( m = mass in kg, V = velocity in m/s and kinetic energy in joules)This requires an array for the column variable (mass), but not for the row variable (velocity)22Variable Column Incrementsconst int NM = 5;int m[NM] = { 1, 2, 5, 10, 20 };for ( int v = 1; v <= 3; v++ ) {cout << setprecision(0) << “\nv = “ << v<< setprecision(1);for ( int j = 1; j < NM; j++ ) cout << setw(7)<< 0.5 * m[j] * v * v;}23Two-way, Variable Row Increment27.022.518.024.016.08.0V = 412.010.08.06.04.02.0V = 23.02.52.01.51.00.5V = 1m = 6m = 5m = 4m = 3m = 2m = 1Kinetic Energy Table ( m = mass in kg, V = velocity in m/s and kinetic energy in joules)• This requires an array for the row variable (velocity), but not for the column variable (mass)24Variable Row Incrementsconst int NV = 3;int v[NV] = { 1, 2, 4 };for ( int i = 1; i < NV; i++ ) {cout << setprecision(0) << “\nv = “ << v[i]<< setprecision(1);for ( int m = 1; m <= 6; m++ )cout << setw(7) << 0.5 * m * v[i] * v[i];}Array programming exercises April


View Full Document

CSUN COMP 106 - Exercises in Array Programming

Download Exercises in Array Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exercises in Array Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exercises in Array Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?