Lecture 21 Examples
Example 1
#include<iostream>
#include<iomanip>
usingnamespace std;
constint numberOfRows = 6;
constint numberOfColumns = 5;
void printMatrix(int matrix[][numberOfColumns], int noOfRows);
void sumRows(int matrix[][numberOfColumns], int noOfRows);
void largestInRows(int matrix[][numberOfColumns], int noOfRows);
int main()
{
int board[numberOfRows][numberOfColumns]
= {{23, 5, 6, 15, 18},
{4, 16, 24, 67, 10},
{12, 54, 23, 76, 11},
{1, 12, 34, 22, 8},
{81, 54, 32, 67, 33},
{12, 34, 76, 78, 9}}; //Line 1
printMatrix(board, numberOfRows); //Line 2
cout < endl; //Line 3
sumRows(board, numberOfRows); //Line 4
cout < endl; //Line 5
largestInRows(board, numberOfRows); //Line 6
return 0;
}
void printMatrix(int matrix[][5], int rowSize)
{
int row, col;
for (row = 0; row < rowSize; row++)
{
for (col = 0; col < 5; col++)
cout < setw(5) < matrix[row][col] < " ";
cout < endl;
}
}
void sumRows(int matrix[][5], int rowSize)
{
int row, col;
int sum;
//sum of each individual row
for (row = 0; row < rowSize; row++)
{
sum = 0;
for (col = 0; col < 5; col++)
sum = sum + matrix[row][col];
cout < "Sum of row " < (row + 1) < " = " < sum
< endl;
}
}
void largestInRows(int matrix[][5], int rowSize)
{
int row, col;
int largest;
//Largest element in each row
for (row = 0; row < rowSize; row++)
{
largest = matrix[row][0]; //assume that the first element
//of the row is the largest
for (col = 1; col < 5; col++)
if (largest < matrix[row][col])
largest = matrix[row][col];
cout < "The largest element of row " < (row + 1)
< " = " < largest < endl;
}
}
Example 2
//Program: Check Code
#include<iostream>
#include<fstream>
#include<iomanip>
usingnamespace std;
constint maxCodeSize = 250;
void readCode(ifstream& infile, int list[],
int& length, bool& lenCodeOk);
void compareCode(ifstream& infile, ofstream& outfile,
int list[], int length);
int main()
{
//Step 1
int codeArray[maxCodeSize];//array to store the secret code
intcodeLength; //variable to store the length
//of the secret code
bool lengthCodeOk;//variable to indicate if the length of the
//secret code is less than or equal to 250
ifstream incode; //input file stream variable
ofstream outcode; //output file stream variable
char inputfile[25];//variable to store the name of the
//input file
char outputfile[25];//variable to store the name of the
//output file
cout < "Enter the input file name: ";
cin > inputfile;
cout < endl;
//Step 2
incode.open(inputfile);
if (!incode)
{
cout < "Cannot open the input file." < endl;
return 1;
}
cout < "Enter the output file name: ";
cin > outputfile;
cout < endl;
outcode.open(outputfile);
readCode(incode, codeArray, codeLength, lengthCodeOk);//Step 3
if (lengthCodeOk) //Step 4
compareCode(incode, outcode, codeArray, codeLength);
else
cout < "Length of the secret code must be <= "
< maxCodeSize < endl;//Step 5
incode.close();
outcode.close();
return 0;
}
void readCode(ifstream& infile, int list[], int& length,
bool& lenCodeOk)
{
int count;
lenCodeOk = true;
infile > length; // get the length of the secret code
if (length > maxCodeSize)
{
lenCodeOk = false;
return;
}
for (count = 0; count < length; count++) // get the secret code
infile > list[count];
}
void compareCode(ifstream& infile, ofstream& outfile,
int list[], int length)
{
int length2;
int digit;
bool codeOk;
int count;
codeOk = true;
infile > length2;
if (length != length2)
{
cout < "The original code and its copy are not of"
" the same length." < endl;
return;
}
outfile < "Code Digit Code Digit Copy" < endl;
for (count = 0; count < length; count++)
{
infile > digit;
outfile < setw(7) < list[count] < setw(20) < digit;
if (digit != list[count])
{
outfile < " code digit not the same" < endl;
codeOk = false;
}
else
outfile < endl;
}
if (codeOk)
outfile < "Message transmitted OK." < endl;
else
outfile < "Error in transmission. Retransmit!!" < endl;
}
Page 1