Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Write a program that creates a two-dimensional array initialized with test

ID: 3598084 • Letter: J

Question

JAVA

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 its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the total of the values in the specified row.

• getColumnTotal. This method 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 method should return the total of the values in the specified column.

• getHighestInRow. This method 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 method should return the highest value in the specified row of the array.

• getLowestInRow. This method 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 method should return the lowest value in the specified row of the array.

There are 4 input.txt files. The biggest array u need is [6][9].

My main concern is how to get the code to read and open the 4 input.txt files. Run program >> Read one file >> terminates >> Run program >> open another file >> terminate. etc.

Explanation / Answer

#include <iostream>
#include <string>

#define ROWS 4
#define COLS 5

using namespace std;

int getTotal(int[][COLS], int, int);
float getAverage(int[][COLS], int, int);
int getRowTotal(int[][COLS], int, int);
int getColumnTotal(int[][COLS], int, int);
int getHighestInRow(int[][COLS], int, int);
int getLowestInRow(int[][COLS], int, int);

int main()
{
// Array with test data
int testArray[][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;
}

int getTotal(int array[][COLS], int rows, int cols)
{
int sum = 0;
for(int i = 0; i < rows; i++)
{
  for(int j = 0; j < cols; j++)
  {
   sum = sum + array[i][j];
  }
}
return sum;
}

float getAverage(int array[][COLS], int rows, int cols)
{
int count = rows*cols; /* number of items in 2D array */
int sum = getTotal(array, rows, cols);
float average = (float)sum /(float)count;
return average;
}

int getRowTotal(int array[][COLS], int the_row, int cols)
{
int sum = 0;
for(int i = 0; i < cols; i++)
{
  sum = sum + array[the_row][i];
}
return sum;
}

int getColumnTotal(int array[][COLS], int the_col, int rows)
{
int sum = 0;
for(int i = 0; i < rows; i++)
{
  sum = sum + array[i][the_col];
}
return sum;
}

int getHighestInRow(int array[][COLS], int the_row, int cols)
{
/* assume the first is highest and then change it as you check */
int highest = array[the_row][0];
for(int i = 0; i < cols; i++)
{
  if(array[the_row][i] > highest)
  {
   highest = array[the_row][i];
  }
}
return highest;
}

int getLowestInRow(int array[][COLS], int the_row, int cols)
{
/* assume the first is lowest and then change it as you check */
int lowest = array[the_row][0];
for(int i = 0; i < cols; i++)
{
  if(array[the_row][i] < lowest)
  {
   lowest = array[the_row][i];
  }
}
return lowest;
}