Code: #include <iostream> #include <iomanip> //#include using namespace std; voi
ID: 3791810 • Letter: C
Question
Code:
#include <iostream>
#include <iomanip>
//#include
using namespace std;
void initialize (int a [4][5]);
void print (int my_arr [4][5]);
void zero_all (int my_arr [4][5]);
void zero_row (int a [4][5], int row);
int main(int argc, char *argv[])
{
int rect [4][5];
//Make function calls as directed by the comments in main.
// call initialize to initialize rect here
initialize(rect);
cout << "The initial array is: ";
// call print to print rect here
print(rect);
// call zero_row to zero all values in rect in row 1
zero_row(rect, 1);
cout << " The array with zeroed row 1 is: ";
// call print to print rect here
print(rect);
// call zero_row to zero all values in rect in row 3
zero_row(rect, 3);
cout << " The array with zeroed row 3 is: ";
// call print to print rect here
print(rect);
// call zero_all to zero all values in rect
zero_all(rect);
cout << " The array with all zeros is: ";
// call print to print rect here
print(rect);
cout << " ";
return 0;
}
// initialize sets all values in the array to counting numbers
void initialize (int a [4][5])
{
int count, row, col;
count = 1;
for (row = 0; row < 4; row++)
for (col = 0; col < 5; col++)
{
a [row][col] = count;
count++;
}
}
// prints the elements of the array in 2 dimensional form
void print (int my_arr [4][5])
{
int r, c;
//Fix the program so that the array is printed properly in 2 dimensions.
for (r = 0; r < 4; r++)
{
for (c = 0; c < 5; c++)
cout << setw (4) << my_arr [r][c];
cout<<endl;
}
}
//Write the function definition for zero_all.
//It sets all the values in its array parameter to zero.
//Write loops to do this.
// sets all values in the 2 D array to zero
void zero_all (int my_arr [4][5])
{
for(int i = 0; i < 4; i++)
for(int j = 0; j < 5; j++)
my_arr[i][j] = 0;
}
//Write the function definition for zero_row.
//It sets the values in its array parameter in the given row to zero.
//Write a loop to do this.
// sets all values of the given row in the 2 D array to zero
void zero_row (int a [4][5], int row)
{
for(int i = 0; i < 5; i++)
a[row][i] = 0;
}
Questions:
1. Explain clearly why it makes sense to put the linefeed in that particular spot in the print function where you placed it.
2. Switch the order of the two loops in the initialize function (col loop first and then row loop) and run your program. Explain what changed in the output and why.
3. Just before the final print in main, add the following assignment statement:
rect [1][6] = 20;
Run your program. Explain what happened.
Explanation / Answer
1. linefeed
void print (int my_arr [4][5])
{
int r, c;
//Fix the program so that the array is printed properly in 2 dimensions.
for (r = 0; r < 4; r++)
{
for (c = 0; c < 5; c++)
cout << setw (4)<<my_arr [r][c];
cout<<" "; //displays the elements of arrays row-wise,new line after each row
}
}
2.
void initialize (int a [4][5])
{
int count, row, col;
count = 1;
for (col = 0; col < 5; col++)
for (row = 0; row < 4; row++) // Switch the order of the two loops
{
a [row][col] = count;
count++;
}
}
output:
3.
int main(int argc, char *argv[])
{
int rect [4][5];
//Make function calls as directed by the comments in main.
// call initialize to initialize rect here
initialize(rect);
cout << "The initial array is: ";
// call print to print rect here
print(rect);
// call zero_row to zero all values in rect in row 1
zero_row(rect, 1);
cout << " The array with zeroed row 1 is: ";
// call print to print rect here
print(rect);
// call zero_row to zero all values in rect in row 3
zero_row(rect, 3);
cout << " The array with zeroed row 3 is: ";
// call print to print rect here
print(rect);
// call zero_all to zero all values in rect
zero_all(rect);
cout << " The array with all zeros is: ";
// call print to print rect here
rect [1][6] = 20; //added this statement
print(rect);
cout << " ";
return 0;
}
output:
This is because the order of columns and rows was switched .
in print ,row1 = 1 ,5 ,9 ,13,17,2
row2 = 6,10,14,18,3,7
7 will change to 20 by rect[1][6] = 20