Functions and arrays

How would I write a function definition for zero_all, and write loops to do it also the function definition for zero_row.

How do I make it print in an 2D array?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <cstdlib>
#include <iostream>
#include <iomanip>
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];

    // 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)
{
 }
Do you understand what your print function (lines 57-64) is doing?
Not 100%, I know that it's printing it into an array, but I believe it's printing into 3D array?
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;
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.