Question

In: Computer Science

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 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. Demonstrate each of the methods in this program.

Solutions

Expert Solution

C++ CODE:

#include<iostream>

using namespace std;

int getTotal(int arr[3][4])
{  
   int total=0,i,j;  
  
   for(i=0;i<3;i++)
   {
       for(j=0;j<4;j++)
       {
           total+=arr[i][j];
       }
      
   }  
   return total;      
}

double getAverage(int arr[3][4])
{
   double average=0;
   int total=0,i,j;
  
   for(i=0;i<3;i++)
   {
       for(j=0;j<4;j++)
       {
           total+=arr[i][j];
       }
      
   }  
   average=total/(3.0 * 4);  
   return average;  
}

int getRowTotal(int arr[3][4],int r)
{
  
   int total=0,i,j;  
  
   for(i=0;i<3;i++)
   {
       for(j=0;j<4;j++)
       {
           if (i==r)
               total+=arr[i][j];
       }
      
   }  
   return total;  
  
}
int getColumnTotal(int arr[3][4],int c)
{
  
   int total=0,i,j;  
  
   for(i=0;i<3;i++)
   {
       for(j=0;j<4;j++)
       {
           if (j==c)
               total+=arr[i][j];
       }
      
   }  
   return total;  
  
}

int getHighestInRow(int arr[3][4],int r)
{
   int total=0,i,j;
   int max=INT_MIN;  
  
   for(i=0;i<3;i++)
   {
       for(j=0;j<4;j++)
       {
           if (i==r)
           {       if (arr[i][j]> max)      
                       {
                           max=arr[i][j];
                       }
              
           }
       }
      
   }  
   return max;  
  
}
int getLowestInRow(int arr[3][4],int r)
{
   int total=0,i,j;
   int min=INT_MAX;  
  
   for(i=0;i<3;i++)
   {
       for(j=0;j<4;j++)
       {
           if (i==r)
           {       if (arr[i][j]< min)      
                       {
                           min=arr[i][j];
                       }
              
           }
       }
      
   }  
   return min;  
  
}

int main()
{
   int arr[3][4]={
  
   {1,2,3,4},
  
   {2,3,4,5},
  
   {1,7,8,9},
  
   };
  
   int t=getTotal(arr);
   cout<<"total is "<<t;
  
   double a=getAverage(arr);
   cout<<"\nAverage is "<<a;
  
   int rt=getRowTotal(arr,0);
   cout<<"\nrow total is "<<rt;
  
   int ct=getColumnTotal(arr,0);
   cout<<"\ncolumn total is "<<ct;
  
   int m=getHighestInRow(arr,2);
   cout<<"\n Max value in row is "<<m;
  
   int mi=getLowestInRow(arr,2);
   cout<<"\n Min value in row is "<<mi;
  
      return 0;
}

SCREENSHOT FOR 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 ....
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 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 Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array 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. Create 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT