#include <cstdlib>
#include <iostream>
#include <iomanip>
usingnamespace 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];
// call initialize to initialize rect here
cout << "The initial array is:\n";
// call print to print rect here
// call zero_row to zero all values in rect in row 1
cout << "\n\nThe array with zeroed row 1 is:\n";
// call print to print rect here
// call zero_row to zero all values in rect in row 3
cout << "\n\nThe array with zeroed row 3 is:\n";
// call print to print rect here
// call zero_all to zero all values in rect
cout << "\n\nThe array with all zeros is:\n";
// call print to print rect here
cout << "\n\n";
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;
for (r = 0; r < 4; r++)
for (c = 0; c < 5; c++)
cout << setw (4) << my_arr [r][c];
}
// sets all values in the 2 D array to zero
void zero_all (int my_arr [4][5])
{
}
// sets all values of the given row in the 2 D array to zero
void zero_row (int a [4][5], int row)
{
}
Not even close. You may want to review your study materials on for-loops and arrays.
This line cout << setw (4) << my_arr [r][c]; says:
1. Print to standard output (cout) which in your case is the terminal on your computer screen.
2. Make the width of the print 4 columns wide ( setw(4) ).
3. The thing it should print is the item in the my_arr at row r and column c (zero based).
By changing the values of r and c, you can print different items stored in my_arr. The 'for' loops are responsible for iterating over all values of r and c, so that all the contents in my_arr get printed.
Using this same structure in the zero_all function, you can do something other than "print" each element. You can "set to zero" each element. Instead of the cout statement, which prints the element at my_arr[r][c], you can set the element at my_arr[r][c] to zero: my_arr[r][c] = 0;
So how would I write the zero_all and zero_row function definition? I referred back to my books and references, but it's really confusing. I'm not good with the terminology, so I'm sure how to make function calls. Is that the same as the void and int statements?
Let's use zero_all where it occurs in the code to make some sense of the terminology.
1. Line 8: This is the FUNCTION DECLARATION. This is where you promise the compiler that somewhere there's a function definition for a function called zero_all which takes an array of integers and returns nothing (void).
2. Line 31: You will replace this comment with a FUNCTION CALL. When your code is actually executing, this is where control of the program transfers from main to zero_all. The call will look like this: zero_all(rect); When you're sitting at your computer examining the code, your eyes should jump from this line to line 67.
3. Line 67: This is the FUNCTION DEFINITION. Here is where the code for the function resides.
Notice that all of this (except for line numbers) applies to print, because print is a function you define, too. The difference at the moment is that print actually has code inside its function body (lines 58-64). You can copy the code inside the print function body and paste it in the zero_all body. Since you don't want print and zero_all to do the same thing, you'll want to modify the zero_all code to set the array elements to zero instead of printing them, as I mentioned in my last post.