I am having troubles trying to figure out a few things in an 2d array inside a class.
First thing I am having issues with is how to row operations;
int MultiArray::getRowSize()
{
return ROWSIZE;
}
will that work
and
int MultiArray::getRowToal(int row)
{
double sum = 0;
for( int col = 0; col < COLSIZE ; col++)
{
total+ = data[row] [col];
return total;
}
do I need another loop in that
and last
void MultyArray::display()
{
for (int i = 0; i < SIZE; i++)
{
cout << "[" << i << "] " << data[i] << endl;
}
}
what do I need to add to that to get the 2d to display?
Firstly, please use code tags when posting code, to make it readable.
First thing I am having issues with is how to row operations;
1 2 3 4
int MultiArray::getRowSize()
{
return ROWSIZE;
}
will that work
It will return the value of the variable called ROWSIZE. If that's what you want it to do then, yes, it will work. You might want to make the method const, since it doesn't change the state of the class. That way, it can be called on a const object, if needed.
1 2 3 4 5 6 7 8
int MultiArray::getRowToal(int row)
{
double sum = 0;
for( int col = 0; col < COLSIZE ; col++)
{
total+ = data[row] [col];
return total;
}
do I need another loop in that
As written, it will sum the values of a single row. If that's what you want it to do then, no, you don't need another loop. What were you thinking of using another loop for?
1 2 3 4 5 6 7
void MultyArray::display()
{
for (int i = 0; i < SIZE; i++)
{
cout << "[" << i << "] " << data[i] << endl;
}
}
what do I need to add to that to get the 2d to display?
This is where you're going to want to use another loop. Currently, you're looping over each row. Within each row, you want to loop over each value in the row and output it. At the end of each row, you'll probably want to output a line-end, just as you're doing now.
doesn't make a lot of sense. You're outputting the members of your array inside your inner loop, so trying to output data[row] here doesn't make sense. In fact, assuming that data is a normal 2-D array (since you haven't shown us the definition), it probably won't even compile.
You can always test this stuff by compiling and running it yourself.
Oh, and please use code tags when posting code here.
I have to put toghether the class for the array, then build the tester, I am just trying to get the class done. The first bit of code I put up worked in my last assingment to display a one dimensional array, I am just lost on how to use it to display a 2d array
[co//Program ID : MultiArray.h
//Written by : A great student
#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 rows the array can hold
const int COLSIZE = 5; // Numbe of columns the array can hold
class MultiArray
{
private:
int data[ROWSIZE][COLSIZE]; //2DArray holding ROWSIZE x COLSIZE int values
public:
MultiArray(); // Constructor. Initialize all elements to 0
void randomFill();
int getRowSize();
int getColSize();
int getTotal();
int getRowTotal(int);
double getAvg();
double getRowAvg(int);
int getLargest();
int getSmallest();
int countValues (int, int);
bool isNumberFound(int);
void display();
};
//function/method implementation.
MultiArray::MultiArray() // Constructor. Initialize all elements to 0
{ for (int r = 0; r < ROWSIZE; r++)
{
for (int c = 0; c < COLSIZE; c++)
{
data[r][c] = 0;
}
}
}
//continue to implement the other functions
void MultiArray::randomFill()
{
srand(static_cast<unsigned int>(time(NULL)));
}
int MultiArray::getRowSize()
{
return ROWSIZE;
}
int MultiArray::getColSize()
{
return COLSIZE;
}
int MultiArray::getTotal()
{
int total = 0;
for (row = 0; row < ROWSIZE ; row++)
{ for(col = 0 ; col < COLSIZE ; row++)
total += data[row] [col]
return total;
}
int MultiArray::getRowToal(int row)
{
double sum = 0;
for( int col = 0; col < COLSIZE ; col++)
{
total+ = data[row] [col];
return total;
}
}
double MultiArray::getAvg()
{
double total = 0;
double average ;
for (row = 0; row < ROWSIZE ; row++)
{
for(col = 0 ; col < COLSIZE ; row++)
{
total += data[row][col];
average = total/SIZE;
}
}
return average;
}
int MultiArray::getLargest()
{
int largest = [0] [0];
for( int row = 0; row < ROWSIZE ; row++)
{ for( int col = 0 ; col < COLSIZE, col++)
if(data[row] [col] > largest;
largest = data[row] [col];
}
return largest;
}
int MultiArray::getSmallest()
{
int smallest = [0] [0];
for (int row = 0; row < ROWSIZE ; row++)
{ for( int col = 0 ; col < COLSIZE, col++)
if(data[row] [col] < smallest;
smallest = data[row] [col];
}
return smallest;
}
int MultiArray::countValues(int lowRang ,int highRange)
{
int values = 0;
for (int i = 0; i < SIZE; i++)
{
if (data[i] >= lowRange && data[i] <= highRange)
{
values = values + 1;
}
else
{
values;
}
}
return values;
}
bool MultiArray::isNumberFound(int someNumber)
{
for (int i = 0; i < SIZE; i++)
{
if (data[i] == someNumber)
{
return true;
}
else
{
return false;
}
}
}
void MultiArray::display()
{
for (int row = 0; row < ROWSIZE; row++)
{
cout << "[" << row << "] " << data[row] << endl;
for( int col =0; col < COLSIZE ; col++)
{
cout << "[" << col << "[" << data[col] << endl;
}
}
}
//There are a few additional functions that are needed in the MultiArray. They arin bold and are
//indicated with **NEW** in the UML.
//There has been a significant change to the display function. It displays the array with a row number
//and then the total for each row and the average. Hint: you will use your new functions, getRowTotal
//and getRowAvg. See the example output. You don’t have to worry about the decimal places in the
//row calculations. But please at least line up your output as shown below.de][/code]