Question

In: Computer Science

C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...

C++ ASSIGNMENT: Two-dimensional array

Problem

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

Solutions

Expert Solution

Solution:

#include<iostream>
using namespace std;
#define ROWS 4
#define COLS 5

int getTotal(int testArray[4][5], int r, int c)
{
   int i,j,sum=0;
  
   for(i=0;i<r;i++)
       for(j=0;j<c;j++)
           sum=sum+testArray[i][j];
  
   return sum;  
}
float getAverage(int testArray[4][5], int r, int c)
{
   int i,j,sum=0;
   float avg;
  
   for(i=0;i<r;i++)
       for(j=0;j<c;j++)
           sum=sum+testArray[i][j];
  
   avg=(float)sum/(float)(r*c);
   return avg;
}
int getRowTotal(int testArray[4][5], int row, int c)
{
   int sum=0,i;

   for(i=0;i<c;i++)
       sum=sum+testArray[row][i];
  
   return sum;
}
int getColumnTotal(int testArray[4][5], int col, int r)
{
   int sum=0,i;
  
   for(i=0;i<r;i++)
       sum=sum+testArray[i][col];

   return sum;
}
int getHighestInRow(int testArray[4][5], int row, int c)
{
   int max=testArray[row][0],i;

   for(i=0;i<c;i++)
       if(testArray[row][i]>max)
           max=testArray[row][i];

   return max;
  

}
int getLowestInRow(int testArray[4][5], int row, int c)
{

   int min=testArray[row][0],i;

   for(i=0;i<c;i++)
       if(testArray[row][i]<min)
           min=testArray[row][i];

   return min;
}

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:


Related Solutions

Write a program that creates a two-dimensional array initialized with test data. The program should have...
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. ▪ getTotal . This function should accept a 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 all the values in the array. ▪ getRowTotal ....
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
[C++ Coding question] write a program which inputs data to a two-dimensional array: - Input number...
[C++ Coding question] write a program which inputs data to a two-dimensional array: - Input number of rows r aand number of columns c - Create a two-dimensional array of integers int numbers[r][c] -input data for each element of numbers Your program must compute and display the largest number is each row and column of the number array
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
Write a program that uses an array of doubles initialized to whatever values you wish. Write...
Write a program that uses an array of doubles initialized to whatever values you wish. Write methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT