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 program that creates a two-dimensional array initialized with test data. Use any primitive data...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: -getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array. -getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array. -getRowTotal. This method should accept a two-dimensional array as...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: fillRandom. Accepts a reference to a two-dimensional array and fills it with random integers from 0 to 99 formatPrint. This method should accept a two-dimensional array and print it out row by row getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in...
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])...
In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
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 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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT