Array, Zero a row

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*

  CIS 251 Lab 3
  
  Two dimensional array code

*/

#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
    
    initialize (rect);
    
    cout << "The initial array is:\n";
    
    // call print to print rect here
    
    print (rect);
    
    // call zero_row to zero all values in rect in row 1
    
    //BBBBBBBBBBBBBBBBBBBBBBBBBBB
    zero_row (rect, 0);
    
    cout << "\n\nThe array with zeroed row 1 is:\n";
    
    // call print to print rect here
    
    print (rect);
    
    // call zero_row to zero all values in rect in row 3
    
    //BBBBBBBBBBBBBBBBBBBBBBBBBBBB
    
    cout << "\n\nThe array with zeroed row 3 is:\n";
    
    // call print to print rect here
   
    print (rect);
    
    // call zero_all to zero all values in rect
    
    zero_all (rect);
    
    cout << "\n\nThe array with all zeros is:\n";
   
    // call print to print rect here
   
    print (rect);
    
    cout << "\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}

// 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++)
     {
       cout << "\n";
       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])
{
     
     int row, col;
     int rect;
     
     for (row = 0; row < 4; row++)
       for (col = 0; col < 5; col++)
         my_arr[row][col] = 0;
     
     
     
 }
 
 
// sets all values of the given row in the 2 D array to zero 
void zero_row (int a [4][5], int row)
{
     for (row = 0; row < 4; row++)
         a[row][5]= 0;
 }



I can't seem to figure out how to zero out the whole row in the array. It has to zero out the first row in the first call. Then the 3rd row when its called again. Im guessing when you call it back in main, you tell it what row to zero out
your zero_row function zeroes all values in the fifth column instead.
You can do something like:
1
2
3
4
5
void zero_row (int a [4][5], int row)
{
     for (int col = 0; col < 5; ++col)
         a[row][col]= 0;
 }
Thank you!

I was thinking of it too simplified as we would see it rather than how the array would. So this still allows the columns to get their set values, but when you input a row when its called, it sets that row to 0. Thanks
Topic archived. No new replies allowed.