Lecture 1 -- 1 Computer Science I - Martin Hardwick Function Prototypes Sometimes we want to use a function before it is defined. If so we define a function prototype. This is the same as defining the function but we end the header with a semicolon. Later in the program we define the function using the normal syntax. I.E. {} follows the header #include <iostream> using namespace std; // Function prototypes int readData(int &a, int &b int &c); int addData(int a, int b, int c, int d, int e, int f, int &x, int &y, int &z); int main() { int a, b, c; int d, e, f; int r, s, t; readData (a, b, c); readData (d, e, f); addData (a, b, c, d, e, f, r, s, t);
23
Embed
Lecture 1 -- 1Computer Science I - Martin Hardwick Function Prototypes rSometimes we want to use a function before it is defined. rIf so we define a function.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Lecture 1 -- 1Computer Science I - Martin Hardwick
Function Prototypes
Sometimes we want to use a function before it is defined.
If so we define a function prototype.
This is the same as defining the function but we end the header with a semicolon.
Later in the program we define the function using the normal syntax.
I.E. {} follows the header
#include <iostream>using namespace std;
// Function prototypesint readData(int &a, int &b int &c);int addData(int a, int b, int c,
int d, int e, int f, int &x, int &y, int
&z);int main(){
int a, b, c;int d, e, f;int r, s, t;
readData (a, b, c);readData (d, e, f);addData (a, b, c, d, e, f, r, s, t);
return;}// code for readData, addData goes
here
Lecture 1 -- 2Computer Science I - Martin Hardwick
Adding Matrices
1 2 34 5 67 8 90 1 2
1 1 12 2 23 3 34 4 4
2 3 4 5 7 810 11 12 4 5 6
+ =
Matrix 1 Matrix 2 Sum ofMatrices1 and 2
Add corresponding elements of Matrix 1 and Matrix 2 to produce the Sum matrix.
To add two matrices, they must have the same number of rows and columns.
Lecture 1 -- 3Computer Science I - Martin Hardwick
Adding Matrices (1)
#include <iostream>using namespace std;
// Function prototypesvoid readMatrix(int matrix[ ][100],
int rows, int cols);void addMatrix( int mat1[ ][100],
int mat2[ ][100], int sum[ ][100],
int rows, int cols);int main()//PURPOSE: test readMatrix &
addMatrix//PRECONDITIONS: none//POSTCONDITIONS: ret 0 if success{
// First matrixint matrix1[100][100];
// Second matrixint matrix2[100][100];
We will represent a table / matrix / spreadsheet in a C++ program as a two-dimensional array.
A two-dimensional array is declared similarly to a one-dimensional array, but you must specify the maximum number of rows and columns.
In this program, we read two matrices and compute their sum in a third matrix.
We need two functions: readMatrix – prompt the
user for the data to put in a matrix
addMatrix – sum two matrices putting the result in a third
Remember: arrays are always passed by reference (you don’t use the & symbol)
Lecture 1 -- 4Computer Science I - Martin Hardwick
Two-Dimensional Arrays
Example: int tab[3][4];
The first subscript is the row number, starting at 0.
The second subscript is the column number, starting at 0.
All the elements of a two-dimensional array are the same type.
An element in a two-dimensional array can be used anywhere an ordinary variable can be used.
The subscripts can be expressions whose values are used to identify a particular array element.
tab[0][0] tab[0][1] tab[0][2] tab[0][3]
tab[1][0] tab[1][1] tab[1][2] tab[1][3]
tab[2][0] tab[2][1] tab[2][2] tab[2][3]
Lecture 1 -- 5Computer Science I - Martin Hardwick
Adding Matrices (2)
// Sum of matricesint sum[100][100];
// Matrix sizeint rows, cols;
// Loop variablesint i,j;
// Read size of matricescout << "Enter number of rows: ";cin >> rows;cout<<"Enter number of columns: ";cin >> cols;
We may not use the entire array each time the program runs.
we use the upper left corner of the array
variable rows tells us how many rows we are using
variable cols tells us how many columns we are using
When passing a two-dimensional array to a function, just use the name of the array without subscripts as the argument.
Lecture 1 -- 6Computer Science I - Martin Hardwick
Adding Matrices (3)
// Add matricesaddMatrix(matrix1, matrix2, sum,
rows, cols);
// Display sum of matricescout << endl
<< "Sum of the matrices:" << endl;
for (i=0; i<rows; i=i+1) { for (j=0; j<cols; j=j+1) {
cout << "\t" << sum[i][j]; } cout << endl;}
return 0;}
When using two-dimensional arrays, it is common to use nested for loops to process all the elements in an array.
the outer loop ranges over the rows of the array
the inner loop ranges over the elements in one row
When displaying the array, note that a tab is used to force the display into columns.
Note also that the endl is done inside the outer loop, but outside the inner loop.
this starts a new line for every row of the array
Lecture 1 -- 7Computer Science I - Martin Hardwick
Adding Matrices (4)
void readMatrix(int matrix[ ][100], int rows, int cols)
//PURPOSE: get matrix data from user//PRECONDITIONS: 0 < rows < number // of rows, and 0 < cols < number of // columns//POSTCONDITIONS: data put in matrix{
int i,j; // loop variables
cout << "Enter data for matrix with" << rows << " rows and " << cols << " columns:" << endl;
for (i=0; i<rows; i++) {for (j=0; j<cols; j++) {
cin >> matrix[i][j];}
}}
This function prompts the user for data to read into a two-dimensional array.
When a parameter is a two-dimensional array, do not specify the number of rows.
but you must specify the number of columns
Parameters rows and cols are needed so that the function knows how many rows and columns of data to read.
Note the nested for loops again.
Lecture 1 -- 8Computer Science I - Martin Hardwick
Adding Matrices (5)
void addMatrix( int mat1[ ][100], int mat2[ ][100],
int sum[ ][100], int rows, int cols)
//PURPOSE: add to matrices//PRECONDITIONS: 0 < rows <
number // of rows, and 0 < cols < number
of // columns//POSTCONDITIONS: matrix sum // contains mat1 + mat2{
int main ()//PURPPOSE: simulate heat flow in a // metal plate//PRECONDITIONS: none//POSTCONDITIONS: create a file // named report.txt and graphical// display with final temperatures
{
// grid of nodes representing platefloat plate[100][100];
Problem Statement:
We will use an iterative technique to solve this problem.
finite-element analysis
100 degrees
100 degrees
10 d
egre
es
10 degrees
What is thetemperaturehere ?
Lecture 1 -- 11Computer Science I - Martin Hardwick
Solution Technique
Compute the new temperature of each interior point as the average of its four neighbors’ temperatures.
Repeat until the temperatures at each node do not change much.
100 degrees
100 degrees
10 d
egre
es 10 degrees
Ti , j
T i , j
T i , j + 1
T i + 1 , j
T i - 1 , j
T i , j - 1
T i + 1, j + T i, j + 1 + T i - 1, j + T i, j - 1
4T i, j =
Lecture 1 -- 12Computer Science I - Martin Hardwick
Heat Flow (2)
int i,j; // loop variablesfloat temp; // temp at edge of platefloat maxchg; // max temp change during iterationfloat tolerance; // tolerance for stable tempsfloat prev; // previous temp at nodeint size; // size of plate in nodesint numits; // number of iterationsfloat maxtemp; // maximum tempfloat mintemp; // minimum tempfloat temprange; // temp range size for coloringfloat sqrsize; // size of square in pixelsint x,y; // location of square in pixelsint tempcat; // temperature color categoryCanvas display(400, 400, 0, 0); // canvas for display of tempscolor c; // color for a squareofstream reportfile; // file for final report
Lecture 1 -- 13Computer Science I - Martin Hardwick
Heat Flow (3)
// Get size of platecout << "Enter size of plate in nodes: ";cin >> size;
// Get temperatures at edges of platecout << "Enter temperature at top: ";cin >> temp;for (i=0; i<size; i=i+1) {
plate[0][i] = temp;}cout << "Enter temperature at bottom: ";cin >> temp;for (i=0; i<size; i=i+1) {
plate[size-1][i] = temp;}cout << "Enter temperature at left side: ";cin >> temp;for (i=0; i<size; i=i+1) {
plate[i][0] = temp;}
A temperature at the edge of the plate applies uniformly to the entire edge of the plate.
all nodes along the edge must be set to this temperature
Each for loop iterates over the nodes along one of the edges.
one of the arrays subscripts is constant throughout the loop
Lecture 1 -- 14Computer Science I - Martin Hardwick
Heat Flow (4)
cout << "Enter temp at right side: ";cin >> temp;for (i=0; i<size; i=i+1) {
plate[i][size-1] = temp;}
// Initialize rest of plate to zerofor (i=1; i<size-1; i=i+1) {
for (j=1; j<size-1; j=j+1) {plate[i][j] = 0.0;
}}
// Get tolerance for stable tempscout << "Get tolerance for "
<< "stable temps: ";cin >> tolerance;
The nodes along the edges remain fixed at the input temperatures.
The remaining interior nodes must be initialized to 0.
The program will iterate, computing new temperatures for the nodes until the temperatures at the nodes stabilize.
they change by less than a user-specified tolerance from one iteration until the next
Lecture 1 -- 15Computer Science I - Martin Hardwick