3 Replies - 1185 Views - Last Post: 27 October 2013 - 04:08 PM Rate Topic: -----

#1 techdude33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-October 13

Write a program that create a two-dimensional array

Posted 27 October 2013 - 02:16 PM

Write a program that create a two-dimensional array initialized with test data. The program should have the following functions:

getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array.
getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array.
getRowTotal - This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the total of the values in the specified row.
getColumnTotal - This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The function should return the total of the values in the specified column.
getHighestInRow - This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the highest value in the specified row in the array.
getLowestInRow - This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the lowest value in the specified row in the array.
Use the main method below to test the program.
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 }
};

// 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 0 is "
<< getRowTotal(testArray, 0, COLS)
<< endl;

// Display the total of column 2.
cout << "The total of col 2 is "
<< getColumnTotal(testArray, 2, ROWS)
<< endl;

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

// Display the lowest value in row 2.
cout << "The lowest value in row 2 is "
<< getLowestInRow(testArray, 2, COLS)
<< endl;

return 0;
}
Output sample

The total of the array elements is 210
The average value of an element is 10.5
The total of row 0 is 15
The total of col 2 is 42
The highest value in row 2 is 15
The lowest value in row 2 is 11

so far i came up with this:

#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 getColumnTotal(int [ROWS][COLS], int);
int getHighestInRow( int x[ROWS][COLS], 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 0 is "
<< getRowTotal(testArray, 0, COLS)
<< endl;

// Display the total of column 2.
cout << "The total of col 2 is "
<< getColumnTotal(testArray, 2, ROWS)
<< endl;

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

// Display the lowest value in row 2.
cout << "The lowest value in row 2 is "
<< getLowestInRow(testArray, 2, COLS)
<< 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 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 colTotal = 0;

}

int getHighestInRow( int x[ROWS][COLS], int row)
{ 
	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[][COLS], int row)
 {
	 	int low= 0; 
	int aux = 0; 
	int total=0;

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

return low; 

 }


how do i fix this? i keep getting errors say it cant take 3 arguments. im lost on what to fix.

This post has been edited by jimblumberg: 27 October 2013 - 02:33 PM
Reason for edit:: Added missing code tags, please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: Write a program that create a two-dimensional array

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Write a program that create a two-dimensional array

Posted 27 October 2013 - 03:34 PM

Please post the complete error messages, exactly as they appear in your development environment.



Jim
Was This Post Helpful? 0
  • +
  • -

#3 techdude33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-October 13

Re: Write a program that create a two-dimensional array

Posted 27 October 2013 - 03:41 PM

1>------ Build started: Project: Lab8, Configuration: Debug Win32 ------
1> Lab8.cpp
1>c:\users\sean\documents\visual studio 2012\projects\lab8\lab8\lab8.cpp(44): error C2660: 'getRowTotal' : function does not take 3 arguments
1>c:\users\sean\documents\visual studio 2012\projects\lab8\lab8\lab8.cpp(49): error C2660: 'getColumnTotal' : function does not take 3 arguments
1>c:\users\sean\documents\visual studio 2012\projects\lab8\lab8\lab8.cpp(54): error C2660: 'getHighestInRow' : function does not take 3 arguments
1>c:\users\sean\documents\visual studio 2012\projects\lab8\lab8\lab8.cpp(59): error C3861: 'getLowestInRow': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1>------ Build started: Project: Lab8, Configuration: Debug Win32 ------
1> Lab8.cpp
1>c:\users\sean\documents\visual studio 2012\projects\lab8\lab8\lab8.cpp(44): error C2660: 'getRowTotal' : function does not take 3 arguments
1>c:\users\sean\documents\visual studio 2012\projects\lab8\lab8\lab8.cpp(49): error C2660: 'getColumnTotal' : function does not take 3 arguments
1>c:\users\sean\documents\visual studio 2012\projects\lab8\lab8\lab8.cpp(54): error C2660: 'getHighestInRow' : function does not take 3 arguments
1>c:\users\sean\documents\visual studio 2012\projects\lab8\lab8\lab8.cpp(59): error C3861: 'getLowestInRow': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Write a program that create a two-dimensional array

Posted 27 October 2013 - 04:08 PM

So what exactly don't you understand about your first error message? Your function was defined with two parameters, why are you trying to call it with three parameters?

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1