2 Replies - 157 Views - Last Post: 18 May 2019 - 04:17 PM Rate Topic: -----

#1 Bahaar Mubeen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-May 19

2D Array Operations, program that creates a two-dimensional array

Posted 18 May 2019 - 02:51 PM

#include <iostream>
#include <iomanip>
using namespace std;
const int ROWS = 4;
const int COLS = 5;
int getTotal(int[ROWS][COLS], int, int);
double getAverage(int[ROWS][COLS], int, int);
int getRowTotal(int[ROWS][COLS], int, int);
int getColumnTotal(int[ROWS][COLS], int, int);
int getHighestInRow(int x[ROWS][COLS], int, int);
int getLowestInRow(int x[ROWS][COLS], int, int);

int main()
{
	// Array with test data
	int testArray[ROWS][COLS] =	{ { 1, 2, 3, 4, 5 },{ 6, 7, 8, 9, 10},	{ 11, 12, 13, 14, 15 },	{ 16, 17, 18, 19, 20 }};
	int num[ROWS][COLS];
	//Copy values from num array to test array
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			num[i][j] = testArray[i][j];

			// Display the total of the array elements.
			cout << "The total of the array elements is "
				<< getTotal(testArray, ROWS, COLS)
				<< endl;

			// Display the average of the array elements.
			cout << "The average value of an element is "
				<< getAverage(testArray, ROWS, COLS)
				<< endl;

			// Display the total of row 0.
			cout << "The total of row 1 is "
				<< getRowTotal(testArray, 0, ROWS)
				<< endl;

			// Display the total of column 2.
			cout << "The total of col 2 is "

				<< getColumnTotal(testArray, 1, COLS)
				<< endl;

			// Display the highest value in row 2. 
			cout << "The highest value in row 2 is "
				<< getHighestInRow(testArray, 1, ROWS)
				<< endl;

			// Display the lowest value in row 2.
			cout << "The lowest value in row 2 is "
				<< getLowestInRow(testArray, 1, ROWS)
				<< endl;
		}
	}
	return 0;
}
int getTotal(int x[ROWS][COLS], int row, int col)
{
	int Total = 0;
	for (int i = 0; i<row; i++)
	{
		for (int j = 0; j<col; j++)
		{
			Total += x[i][j];
		}
	}
	return Total;
}

double getAverage(int x[ROWS][COLS], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			x[i][j] = x[i][j];
		}
	}
	double Average = getTotal(x, row, col) / (row*col);
	return Average;
}

// accept two-dimensional array as first argument and 
// integer as second argument - second argument is subscript 
// of a column in array; return total of values in column 
// specified by subscript 
int getRowTotal(int x[ROWS][COLS], int row, int COLS)

{
	int total = 0;

	for (int i = 0; i < COLS; i++)
	{
		total = total + x[row][i];
	}

	return total;
}
int getColumnTotal(int x[ROWS][COLS], int col, int ROWS)
{

	int colTotal = 0;
	for (int i = 0; i < ROWS; i++)
	{
		colTotal = colTotal + x[col][i];
	}
	return colTotal;
}

int getHighestInRow(int x[ROWS][COLS], int row, int COLS)
{
	int high = 0;
	int aux = 0;
	int total = 0;

	for (int i = 0; i < COLS; i++)
	{
		aux = total + x[row][i];
		if (aux > high)
		{
			high = aux;
		}
	}

	return high;
}
int getLowestInRow(int x[ROWS][COLS], int row , int COLS)
{
	int low = 0;
	int aux = 0;
	int total = 0;

	for (int i = 0; i < ROWS ; i++)
	{
		aux = total + x[row][i];
		if (aux > low)
		{
			low = aux;
		}
	}

	return low;

}

// THE OUTPUT IS NOT THE DESIRED ONE SINCE IT DISPLAYS THE MAX ROW ELEMENT AND MIN ROW ELEMENT THE SAME


This post has been edited by Skydiver: 18 May 2019 - 04:05 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: 2D Array Operations, program that creates a two-dimensional array

#2 jimblumberg   User is offline

  • member icon

Reputation: 5732
  • View blog
  • Posts: 17,558
  • Joined: 25-December 09

Re: 2D Array Operations, program that creates a two-dimensional array

Posted 18 May 2019 - 03:26 PM

Why are you calling your functions inside the loops?

You should consider increasing your compiler warning levels and fix all warnings and errors.

||=== Build: Debug in homework (compiler: gcc-8) ===|
/main.cpp||In function ‘int main()’:|
/main.cpp|17|warning: variable ‘num’ set but not used [-Wunused-but-set-variable]|
/main.cpp||In function ‘int getRowTotal(int (*)[5], int, int)’:|
/main.cpp|89|warning: declaration of ‘COLS’ shadows a global declaration [-Wshadow]|
/main.cpp|5|note: shadowed declaration is here|
/main.cpp||In function ‘int getColumnTotal(int (*)[5], int, int)’:|
/main.cpp|101|warning: declaration of ‘ROWS’ shadows a global declaration [-Wshadow]|
/main.cpp|4|note: shadowed declaration is here|
/main.cpp||In function ‘int getHighestInRow(int (*)[5], int, int)’:|
/main.cpp|112|warning: declaration of ‘COLS’ shadows a global declaration [-Wshadow]|
/main.cpp|5|note: shadowed declaration is here|
/main.cpp||In function ‘int getLowestInRow(int (*)[5], int, int)’:|
/main.cpp|129|warning: declaration of ‘COLS’ shadows a global declaration [-Wshadow]|
/main.cpp|5|note: shadowed declaration is here|
/main.cpp|129|warning: unused parameter ‘COLS’ [-Wunused-parameter]|
||=== Build finished: 0 error(s), 6 warning(s) (0 minute(s), 2 second(s)) ===|




And please use code tags when posting code.

:code:


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6918
  • View blog
  • Posts: 23,525
  • Joined: 05-May 12

Re: 2D Array Operations, program that creates a two-dimensional array

Posted 18 May 2019 - 04:17 PM

View PostBahaar Mubeen, on 18 May 2019 - 05:51 PM, said:

// THE OUTPUT IS NOT THE DESIRED ONE SINCE IT DISPLAYS THE MAX ROW ELEMENT AND MIN ROW ELEMENT THE SAME

Well, considering that the bodies of both of your getHighestInRow() and getLowestInRow() functions are exactly the same except of change the variable name high or low why would you expect the behavior to be different. A compiler will not care if you the variable name is high, low, or bitcoin.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1