Download Array Programming Exercises - Computing in Engineering and Science - Lecture Slides and more Exercises Computer Science in PDF only on Docsity!
Exercises in Array ProgrammingExercises in Array Programming
Larry Caretto Computer Science 106
Computing in Engineering
and Science
April 25, 2006
2
Outline
- 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 programming
3
Representing Data
Run Data
Math C++
x[5]
x[4]
x[3]
x[2]
x[1]
x[0]
x 5
x 4
x 3
x 2
x 1
x 0
x[i] for
math xi
subscript =
number of
elements
minus one
4
General 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 < N
for ( 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 L
for ( int k = F; k <= L; k++)
5
File Input Screen Output
const 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]; }
6
Console Input File Output
const 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; }
7
Defined 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 elements
double x[10]; for ( int j = 1; j < 5; j++ ) x[j] = 1 / double( j );
8
Computing the Mean
sum = 0; for( i = 0; i < N; i++ ) { sum += x[i]; } average = sum / N;
−
= =
1
1 0
1 1 N
i
i
N
i
i x
N
x
N
x
to average
starts at 0
is element N-
1 in array
9
- Store the current maximum in a variable
(e.g., max) and compare max to new
array values
- If a new value is greater than max
replace max by that value
double max = x[0];// initialize max for ( int i = 1; i < N; i++ ) { if ( x[i] > max ) { max = x[i]; } }
Finding the Maximum
sets of braces
10
- Store the current minimum in a variable
(e.g., min) and compare min to new
array values
- If a new value is greater than min
replace min by that value
double min = x[0];// initialize min for ( int i = 1; i < N; i++ ) { if ( x[i] < min ) { min = x[i]; } }
Finding the Minimum
sets of braces
11
Array 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 input
12
Average Power One
double 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) loops
19
Two-way, Both Fixed Increment
V = 3 4.5 9.0 13.5 18.0 22.5 27.
V = 2 2.0 4.0 6.0 8.0 10.0 12.
V = 1 0.5 1.0 1.5 2.0 2.5 3.
m = 1m = 2m = 3 m = 4m = 5m = 6
Kinetic 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 code 20
Even Increments In Both
- KE with even step sizes in both mass
and velocity
for ( 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; }
21
Two-way, Variable Columns
V = 3 4.5 9.0 22.5 45.0 90.
V = 2 2.0 4.0 10.0 20.0 40.
V = 1 0.5 1.0 2.5 5.0 10.
m = 1m = 2m = 5m = 10 m = 20
Kinetic 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)
22
Variable Column Increments
const 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; }
23
Two-way, Variable Row Increment
V = 4 8.0 16.0 24.0 18.0 22.5 27.
V = 2 2.0 4.0 6.0 8.0 10.0 12.
V = 1 0.5 1.0 1.5 2.0 2.5 3.
m = 1m = 2m = 3 m = 4m = 5m = 6
Kinetic 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)
24
Variable Row Increments
const 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]; }
25
Two-way, Both Increments Vary
- This requires arrays for both the row
variable (velocity and the column
variable (mass)
V = 4 8.0 16.0 40.0 80.0 160.
V = 2 2.0 4.0 10.0 20.0 40.
V = 1 0.5 1.0 2.5 5.0 10.
m = 1m = 2m = 5m = 10 m = 20
Kinetic Energy Table ( m = mass in kg, V = velocity in m/s and kinetic energy in joules)
26
Both Increments Variable
const int NM = 5, NV = 3; int m[NM] = { 1, 2, 5, 10, 20 }; int v[NV] = { 1, 2, 4 }; for ( int i = 1; i < NV; i++ ) { cout << setprecision(0) << “\nv = “ << v[i] << setprecision(1); for ( int j = 1; j < NM; j++ ) cout << setw(7) << 0.5 * m[j] * v[i] * v[i]; }
27
Exercise
- The volume occupied by an idea gas is
found from the equation V = nRT/P
- V is the volume in m 3
- n is the number of kmols (kgmoles)
- R = 8.3144 kPa·m^3 /kmol·K is the (universal) gas constant
- T is the temperature in kelvins
- P is the pressure in kPa
- Write a program that computes V for
fixed n and T using several P values
28
Exercise II
- Write a program that computes and
prints V = nRT/P for a series of
pressures using n = 1 kmol and T =
273.15 K
– P = 1, 2, 5, 10, 20, 50, 100, 200, 500,
1000, 2000, 5000, and 10000 kPa
- R = 8.3144 kPa·m^3 /kmol·K
29
Exercise Solution
const double R = 8.3144; const int NP = 13; double n = 1, T = 273.15, P[NP] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000 }; for ( int k = 0; k < NP; k++ ) { cout << “\nFor P = “ << P[k] << “ kPa, V = “ << n * R
Calculating Two-way Tables
- The previous exercise used an array to
compute a one way table with uneven
increments in pressure
- Found V as a function of P for one kmol of an ideal gas at T = 273.15 k
- What if we wanted a two-way table with
uneven increments in both tables?
- Define arrays for both the row and the column variables