Exam Practice!
Memory Snapshot
1. x => 0 1 2 3 4
2. x => 0 0 0 0 0
Program Output
3. 15
Multiple-Choice Problems
7. (e)
Programming Exercises
Power Plant Data
/*——————————————————————–*/
int main()
{
/* Define variables. */
double total_power=0, average_power, power_out[NROW][NCOL];
/* Print the results. */
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter8_12 */
/* */
#include <iostream>
#include <cstdlib>
int main()
{
/* Define variables. */
}
}
/*——————————————————————–*/
double col_ave(double x[][NCOLS], int col)
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter8_14 */
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter8_15 */
/* */
double col_ave(double x[][], int col);
int main()
{
/* Define variables. */
double power_out[NROWS][NCOLS];
int row, col;
ifstream power;
}
/* Exit program. */
return 0;
}
double row_ave(double x[][NCOLS], int row)
{
/* Declare variables. */
int col;
double sum=0;
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter8_17 */
/* */
int main()
{
/* Define variables. */
double power_out[NROWS][NCOLS];
int row, col;
ifstream power;
/* Exit program. */
return 0;
}
/*——————————————————————–*/
/*——————————————————————–*/
int main()
/* Open input file. */
power.open(“power1.dat”,ios::in);
/* Read data. */
/* Print results. */
cout << “Mean output of power plant, in MegaWatts: ” << mean << endl;
cout << “Variance of output, in MegaWatts: ” << var << endl;
Temperature Distribution
/*——————————————————————–*/
/* Problem chapter8_19 */
/* */
/* This program models a temperature distribution for a grid */
/* with six rows and eight columns. The user enters the */
int main()
{
/* Declare and initialize variables. */
int row, col;
bool done(false);
}
t[row][0] = left;
t[row][NCOLS-1] = right;
}
t[NROWS-1].resize(NCOLS); //sllocate columns for last row
for (col=0; col<NCOLS; col++)
{
+ t[row][col+1])/4;
check = fabs(update t[row][col]);
if (check > maxDiff)
maxDiff = check;
t[row][col] = update;
}
}
}
/*——————————————————————–*/
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter8_20 */
/* */
int main()
{
/* Declare and initialize variables. */
int row, col;
double t[NROWS][NCOLS], top, right, left, bottom, tolerance,
{
t[0][col] = top;
t[NROWS-1][col] = bottom;
}
if (check > max_update)
max_update = check;
t[row][col] = update;
}
}
}
/* Print results. */
}
/* Exit program. */
return 0;
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter8_21 */
/* */
/* This program models a temperature distribution for a grid */
/* with six rows and eight columns. The user enters the */
/* value. */
int main()
{
/* Declare and initialize variables. */
int row, col;
double t[NROWS][NCOLS], t2[NROWS][NCOLS], top, right, left, bottom,
tolerance, check, update, max_update=0;
/* Prompt user to enter initial temperatures and tolerance. */
cout << “Enter initial temperatures (top, right, bottom, left): “;
cin >> top >> right >> bottom >> left;
}
for (col=0; col<NCOLS; col++)
{
t[0][col] = top;
t[NROWS-1][col] = bottom;
t2[0][col] = top;
t2[NROWS-1][col] = bottom;
}
/* Update the grid across the rows. */
while((max_update > tolerance) || (max_update < –tolerance))
if (check > max_update)
max_update = check;
t2[row][col] = update;
}
}
/* Exit program. */
return 0;
}
/*——————————————————————–*/
cin >> tolerance;
tolerance = fabs(tolerance);
/* Initialize grid. */
}
/* Update the grid across the rows. */
while(!done) //(maxDiff > tolerance) || (maxDiff < –tolerance));
{
/* Initialize the maximum update this iteration to zero */
maxDiff = 0.0;