Question

In: Computer Science

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 . 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 of 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 of the array.

Demonstrate each of the functions in this program.

Sample run:

DocViewer

Page

of 3Zoom

Pages

#include <iostream>

using namespace std;

// Constant for the number of columns

const int COLS = 5;

// Function prototypes

int getTotal(int [][COLS], int);

double getAverage(int [][COLS], int);

int getRowTotal(int [][COLS], int);

int getColumnTotal(int [][COLS], int, int);

int getHighestInRow(int [][COLS], int);

int getLowestInRow(int [][COLS], int);

int main()

{

const int ROWS = 4; // Constant for the number of rows

// 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)

<< endl;

// Display the average of the array elements.

cout << "The average value of an element is "

<< getAverage(testArray, ROWS)

<< endl;

// Display the total of row 0.

cout << "The total of row 0 is "

<< getRowTotal(testArray, 0)

<< 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)

<< endl;

// Display the lowest value in row 2.

cout << "The lowest value in row 2 is "

<< getLowestInRow(testArray, 2)

<< endl;

system("PAUSE");

return 0;

}

// ********************************************************

// The getTotal function returns the total of all *

// the elements in the array. *

// ********************************************************

int getTotal(int array[][COLS], int rows)

{

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

}

// ********************************************************

// The getAverage function returns the averave value *

// of the elements in the array. *

// ********************************************************

double getAverage(int array[][COLS], int rows)

{

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

}

// ********************************************************

// The getRowTotal function returns the total of the *

// the elements in the specified row of the array. *

// ********************************************************

int getRowTotal(int array[][COLS], int rowToTotal)

{

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

}

// ********************************************************

// The getColTotal function returns the total of the *

// the elements in the specified column of the array. *

// ********************************************************

int getColumnTotal(int array[][COLS], int colToTotal, int rows)

{

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

}

// ********************************************************

// The getHighestInRow function returns the highest *

// value in the specified row. *

// ********************************************************

int getHighestInRow(int array[][COLS], int rowToSearch)

{

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

}

// ********************************************************

// The getLowestInRow function returns the lowest *

// value in the specified row. *

// ********************************************************

int getLowestInRow(int array[][COLS], int rowToSearch)

{

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

// IMPLEMENT THIS FUNCTION

}

Annotations

Solutions

Expert Solution

#include <iostream>

using namespace std;
// Constant for the number of columns
const int COLS = 5;

// Function prototypes
int getTotal(int [][COLS], int);
double getAverage(int [][COLS], int);
int getRowTotal(int [][COLS], int);
int getColumnTotal(int [][COLS], int, int);
int getHighestInRow(int [][COLS], int);
int getLowestInRow(int [][COLS], int);

int main() {
    const int ROWS = 4; // Constant for the number of rows
// 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)
         << endl;
// Display the average of the array elements.
    cout << "The average value of an element is "
         << getAverage(testArray, ROWS)
         << endl;
// Display the total of row 0.
    cout << "The total of row 0 is "
         << getRowTotal(testArray, 0)
         << 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)
         << endl;
// Display the lowest value in row 2.
    cout << "The lowest value in row 2 is "
         << getLowestInRow(testArray, 2)
         << endl;
    system("PAUSE");
    return 0;
}

// ********************************************************
// The getTotal function returns the total of all *
// the elements in the array. *
// ********************************************************
int getTotal(int array[][COLS], int rows) {
    int total = 0, count = 0;
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < COLS; ++j) {
            total += array[i][j];
        }
    }
    return total;
}

// ********************************************************
// The getAverage function returns the averave value *
// of the elements in the array. *
// ********************************************************
double getAverage(int array[][COLS], int rows) {
    double total = 0, count = 0;
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < COLS; ++j) {
            total += array[i][j];
            count += 1;
        }
    }
    return total / count;
}

// ********************************************************
// The getRowTotal function returns the total of the *
// the elements in the specified row of the array. *
// ********************************************************
int getRowTotal(int array[][COLS], int rowToTotal) {
    int total = 0;
    for (int i = 0; i < COLS; ++i) {
        total += array[rowToTotal][i];
    }
    return total;
}

// ********************************************************
// The getColTotal function returns the total of the *
// the elements in the specified column of the array. *
// ********************************************************
int getColumnTotal(int array[][COLS], int colToTotal, int rows) {
    int total = 0;
    for (int i = 0; i < rows; ++i) {
        total += array[i][colToTotal];
    }
    return total;
}

// ********************************************************
// The getHighestInRow function returns the highest *
// value in the specified row. *
// ********************************************************
int getHighestInRow(int array[][COLS], int rowToSearch) {
    int highestIndex = 0;
    for (int i = 0; i < COLS; ++i) {
        if (array[rowToSearch][i] > array[rowToSearch][highestIndex]) {
            highestIndex = i;
        }
    }
    return array[rowToSearch][highestIndex];
}

// ********************************************************
// The getLowestInRow function returns the lowest *
// value in the specified row. *
// ********************************************************
int getLowestInRow(int array[][COLS], int rowToSearch) {
    int lowestIndex = 0;
    for (int i = 0; i < COLS; ++i) {
        if (array[rowToSearch][i] < array[rowToSearch][lowestIndex]) {
            lowestIndex = i;
        }
    }
    return array[rowToSearch][lowestIndex];
}

Related Solutions

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...
Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:
IN JAVA Array Operations Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods: getTotal: This method should accept a one-dimensional array as its argument and return the total of the values in the array. getAverage: This method should accept a one-dimensional array as its argument and return the average of the values in the array. getHighest: This method should accept a...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
Write a program that adds and subtracts two polynomials. It creates an array of nodes and...
Write a program that adds and subtracts two polynomials. It creates an array of nodes and connects them into the freeStore. This implementation uses one array to store multiple array to store multiple polynomial instances and the free store. I need help to finish the LinkedListInArrayPolynomial class. Output should look like below: Forth test is linked list of terms in an array. linkInArray1 = 3x^11+4x^10+4x^4 linkInArray2 = 4x^19+5x^14-3x^12-78 sum of linkInArray1 and linkInArray2 = 4x^19+5x^14-3x^12+3x^11+4x^10+4x^4-78 linkInArray1 minus linkInArray2 = -4x^19-5x^14+3x^12+3x^11+4x^10+4x^4+78...
[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 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...
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])...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT