displaying 2D array correctly

Hello fellow programmers. I am trying to display a 2D array that displays randomly generated numbers that fill out in rows and columns. At the end of each row it should have the total and average displayed. I have written up all of the code and it compiles. I am not asking for the whole solution. Trust me, I have done my own work. I simply can't get the columns to display. I just have one long row. I have a header and tester file. Here is what my header file looks like. The area that I need help with is the function called "void MultiArray::display()"

This is my code.

#include <iostream>
#include <iomanip>
#include <cstdlib> // Needed to use rand() and srand()
#include <time.h> // Needed for time functions

using namespace std;

const int ROWSIZE = 10; // Number of elements in the array
const int COLSIZE = 5;

class MultiArray

{
private:

int data[ROWSIZE][COLSIZE]; // Array holding SIZE double values

public:

MultiArray(); // Constructor. Initialize all elements of data[] to 0

void randomFill();
int getRowSize();
int getColSize();
int getTotal();
int getRowTotal(int r);
double getAvg();
double getRowAvg(int r);
int getLargest();
int getSmallest();
int countValues (int lowRange, int highRange);
bool isNumberFound(int someNumber);
void display();

};

//********************implement functions here*******************

//constructor. fills data[] array with 0s
MultiArray :: MultiArray()
{
for (int r = 0; r < ROWSIZE; r++)
{
for (int c = 0; c < COLSIZE; c++)
{
data[r][c] = 0;
}
}
}

void MultiArray::randomFill()
{
//seed the random number generator
srand(static_cast<unsigned int>(time( NULL )));

for(int r = 0; r < ROWSIZE; r++)
{
for(int c = 0; c < COLSIZE; c++)
{
data[r][c] = rand() % 101;
}
}
}

int MultiArray::getRowSize()
{ return ROWSIZE; }

int MultiArray::getColSize()
{ return COLSIZE; }

int MultiArray::getTotal()
{
int sum = 0;

for(int r = 0; r < ROWSIZE; r++)
{
for(int c = 0; c < COLSIZE; c++)
{
sum += data[r][c];
}
}

return sum;
}

int MultiArray::getRowTotal(int r)
{
int sum = 0;
for(int r = 0; r < ROWSIZE; r++)
{
sum += data[r][0];
}
return sum;
}

double MultiArray::getAvg()
{
double average = 0.0;
double sum = 0.0;

for (int r = 0; r < ROWSIZE; r++)
{
for(int c = 0; c < COLSIZE; c++)
{
sum += data[r][c];
average = sum/(ROWSIZE+COLSIZE);
}
}

return average;
}

double MultiArray::getRowAvg(int r)
{
double average = 0.0;
double sum = 0.0;

for (int r = 0; r < ROWSIZE; r++)
{
sum += data[r][0];
average = sum/(ROWSIZE);
}

return average;
}

int MultiArray::getLargest()
{
int max = data[0][0];

for(int r = 0; r < ROWSIZE; r++)
{
for(int c = 0; c < COLSIZE; c++)
{
if(data[r][c] > max)
max = data[r][c];
}
}

return max;
}

int MultiArray::getSmallest()
{
int min = data[0][0];

for(int r = 0; r < ROWSIZE; r++)
{
for(int c = 0; c < COLSIZE; c++)
{
if(data[r][c] < min)
min = data[r][c];
}
}

return min;
}

int MultiArray::countValues(int lowRange, int highRange)
{
int amount = 0;

for(int r = 0; r < ROWSIZE; r++)
{
for(int c = 0; c < COLSIZE; c++)
{
if((data[r][c] >= lowRange) && (data[r][c] <= highRange))
amount++;
}
}

return amount;
}

bool MultiArray::isNumberFound(int someNumber)
{
bool found = false;

for(int r = 0; r < ROWSIZE; r++)
{
for(int c = 0; c < COLSIZE; c++)
{
if (data[r][c] == someNumber)
found = true;
}
}

return found;
}

void MultiArray::display()
{
//your code goes here to display the array

for(int r = 0; r < ROWSIZE; r++)
{
for(int c = 0; c < COLSIZE; c++)
{
cout << "[" << r << "]" << data[r][c] << endl;
}
}

}


And this is my code that gets generated:

Exam Scores
-----------
[0]75
[0]55
[0]55
[0]9
[0]95
[1]23
[1]60
[1]90
[1]52
[1]85
[2]90
[2]57
[2]81
[2]59
[2]61
[3]51
[3]7
[3]74
[3]22
[3]72
[4]72
[4]68
[4]28
[4]59
[4]19
[5]78
[5]59
[5]16
[5]47
[5]1
[6]40
[6]1
[6]75
[6]76
[6]58
[7]78
[7]12
[7]92
[7]2
[7]24
[8]1
[8]86
[8]76
[8]63
[8]57
[9]3
[9]27
[9]73
[9]80
[9]91


The minimum value is : 1
The maximum value is : 95
The total is : 2635
The average is : 175.7
The number of A's : 5

Sorry, you DO NOT have a perfect exam score!

Would you like to try again? (Y/N):


*****The output that should be displayed is like this:

Exam Scores
-----------
[0] 67 81 50 29 27 Total:254 Avg:50.8
[1] 58 4 0 6 37 Total:105 Avg:21
[2] 29 4 85 50 12 Total:180 Avg:36
[3] 88 50 38 51 44 Total:271 Avg:54.2
[4] 7 64 67 52 32 Total:222 Avg:44.4
[5] 97 2 28 76 43 Total:246 Avg:49.2
[6] 82 82 99 53 0 Total:316 Avg:63.2
[7] 69 47 36 19 54 Total:225 Avg:45
[8] 33 23 88 61 86 Total:291 Avg:58.2
[9] 45 51 75 92 45 Total:308 Avg:61.6
The minimum value : 0
The maximum value : 99
The total is : 2418
The average is : 48.36
Number of A's : 3
Sorry, you DO NOT have a perfect exam score.
Do you want to try again? (Y or N)




Any help is very much appreciated.
Thank you.
Welcome to Cplusplus!

First off, code tags!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void MultiArray::display()
{

for(int r = 0; r < ROWSIZE; r++)
{
  int total=0;
  cout << '[' << r<< ']';
    for(int c = 0; c < COLSIZE; c++)
    {
    cout << data[r][c];// no << endl; here
    total+=data[r][c];
    }
  cout << " total: "<<total<<" average: " <<(float)(total/ROWSIZE)<<endl;
}

}
Last edited on
That helps me a lot. But I feel like my professor wants us to use the functions "getRowTotal" and "getRowAvg" inside our display function to show the total and average.

is that even possible?


Thank you.
Yep it is.

Just think about it a little, I'll leave it to you to figure it out.

Just think about what result the function gives you, and where you want this result to be outputted.
Topic archived. No new replies allowed.