Question

In: Computer Science

Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one...

Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one hundred rows and fifty columns. Iterate through each element and assign it a random value between -72 and 75 inclusive. Have your random number seed be 25. Create functions that do the following: 1. A function called “sum” that returns the sum of all the elements in your 2D Array. 2. A function called “average” that return the average value of the elements in your 2D array. 3. A function called “column_average” that returns the average of a specified column in your 2D array. 4. A function called “row_average” that returns the average of a specified row in your 2D array. 5. A function called “sort_row” that sorts a specified row in your 2D array. 6. A function called "search" that does a linear search on a user specified row and returns the columns index where it was found. If it wasn't found, return -1. Make sure you pass in the value to be search for.. Create a menu system that enables the user to call each of the above functions. For 3 and 4 allow the user to enter the column or row they wish to see the averages of. For 5, allow the user to specify the row to be sorted and ask them if they would like to see the min and max element of that row. The user should be able to enter ‘Y’, ‘y’, ‘N’, or ‘n’. Make sure to validate all user input. If they entered an invalid value, don’t ask them for a valid input and just pass zero to your function. For 6, ask the user for the row and value (space separated) to be searched.

Solutions

Expert Solution

Sample output:

Program code to copy:

#include <iostream>

using namespace std;

int sum(int arr[][50]);
float average(int arr[][50]);
float column_average(int arr[][50], int col);
float row_average(int arr[][50], int row);
void sort_row(int arr[][50], int row);
int search(int arr[][50], int num, int row);

int main()
{
// an array with 100 rows and 50 columns.
int x[100][50];
int range = (75 + 75) + 1;
int choice, col, row, num, index;
char ch;
  
srand(25);
  
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 50; j++)
{
x[i][j] = rand()%range - 75;
}
}
  
while (1) {
cout<<"1. Sum\n";
cout<<"2. Average\n";
cout<<"3. Column Average\n";
cout<<"4. Row Average\n";
cout<<"5. Sort Row\n";
cout<<"6. Search Row\n";
cout<<"7. Exit\n";
cout<<"\n Your choice ?";
cin>>choice ;
switch ( choice) {
case 1 :
cout<< "\nThe sum is: " << sum(x) <<endl;
break ;   
case 2 :
cout<< "\nThe average is: " << average(x) <<endl;
break ;
case 3 :
cout<< "\nEnter column number: ";
cin>>col ;
if ((col < 0) || (col > 99))
col = 0;
cout << "\n The average of column " << col << " is " << column_average(x, col) << endl;
break;
case 4 :
cout<< "\nEnter row number: ";
cin>>row ;
if ((row < 0) || (row > 49))
row = 0;
cout << "\n The average of row " << row << "is " << row_average(x, row) << endl;
break;
case 5 :
cout<< "\nEnter row number: ";
cin>>row ;
if ((row < 0) || (row > 49))
row = 0;
sort_row(x, row);
cout << "Do you want to see the minimum and maximum elements of row " << row << "?\n";
cout << "Enter y or n : ";
cin >> ch;
while ((ch != 'Y') && (ch != 'y') && (ch != 'N') && (ch != 'n')){
cout << "Wrong input!! Enter y or n : ";
cin >> ch;
}
if ((ch == 'Y') || (ch == 'y')){
cout << "Maximum: " << x[row][49] << endl;
cout << "Minimum: " << x[row][0] << endl << endl;
}
break;
case 6:
cout<< "\nEnter row number & element to search: ";
cin>>row ;
cin>>num ;
if ((row < 0) || (row > 49))
row = 0;
if ((num < -75) || (num > 75))
num = 0;
index = search(x, num, row);
if(index == -1){
cout << "Element is not found." << endl;
}
else {
cout << "Element " << num <<" is found at index " << index << endl;
}
break;
case 7 :
exit(1) ;
}
}
return 0;
}

int sum(int arr[][50]){
int total = 0;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 50; j++)
{
total += arr[i][j];
}
}
return total;
}


float average(int arr[][50]){
int total = 0;
float average = 0;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 50; j++)
{
total += arr[i][j];
}
}
  
average = total/(100.0*50.0);
return average;
}


float column_average(int arr[][50], int col){
int total = 0;
float average = 0;
for (int i = 0; i < 100; i++)
{
total += arr[i][col];
}
  
average = total/100.0;
return average;
}


float row_average(int arr[][50], int row){
int total = 0;
float average = 0;
for (int i = 0; i < 50; i++)
{
total += arr[row][i];
}
  
average = total/50.0;
return average;
}

void sort_row(int arr[][50], int row){
int min, min_index;
for(int i=0; i < 50; i++){
min = arr[row][i];
for(int j=i; j < 50; j++){
if(min > arr[row][j]){
min = arr[row][j];
min_index = j;
}
}
if(i != min_index){
arr[row][min_index] = arr[row][i];
arr[row][i] = min;
}
}
cout << "Sorted row is: "<< endl;
for (int i = 0; i < 50; i++)
{
cout << arr[row][i] << " ";
}
cout << endl;
}

int search(int arr[][50], int num, int row){
int index = -1;
for (int i = 0; i < 50; i++)
{
if (num == arr[row][i]){
index = i;
break;
}
}
return index;
}


Related Solutions

In C programming Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL]. Then, loop through the 2d array...
In C programming Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL]. Then, loop through the 2d array and save the results in n0 and n1: for (i=0; i<MAX_ROW; i++)   for (j=0; j<MAX_COL;j++){        //calculate number of zeros around entry a[i][j]        n0[i][j] =        //calculate number of ones around entry a[i][j]        n1[i][j] =   } Set the MAX_ROW and MAX_COL to a small number and display all three 2d arrays on the screen to verify that the calculations are correct. You may still use command-line...
Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL]. Then, loop through the 2d array and save the...
Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL]. Then, loop through the 2d array and save the results in n0 and n1: for (i=0; i<MAX_ROW; i++)   for (j=0; j<MAX_COL;j++){        //calculate number of zeros around entry a[i][j]        n0[i][j] =        //calculate number of ones around entry a[i][j]        n1[i][j] =   } Set the MAX_ROW and MAX_COL to a small number and display all three 2d arrays on the screen to verify that the calculations are correct. You may still use command-line inputs as in...
Lab 7: 2D Arrays Project Goals The goals of this project are to: 1.       Get students...
Lab 7: 2D Arrays Project Goals The goals of this project are to: 1.       Get students familiar with 2D arrays 2.       Continue loop practice Important Notes: 1.        Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text of the problem from “Enter the number of rows in your array: ” to “Enter the number of rows: ”. Your assignment will be auto-graded and any change in formatting will...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in two separate parts). You are not allowed to use any of the built-in classes for this lab. If you find yourself needing to import anything at the top of your class, you are doing it wrong. Part A a) Create a class method called printArray2D that accepts a 2D integer array and prints out the values formatted such that each value occupies 4 spaces...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
This program needs to be in Java Exercise on Single Dimensional Arrays Declare an array reference...
This program needs to be in Java Exercise on Single Dimensional Arrays Declare an array reference variable arrayInt for an array of integers. Create the array of size 100, and assign it to arrayInt. (2 points) Write a method to populate the array with Random numbers between 1 to 25. Signature of the method is: populateArray( int[] ) which returns nothing. Call this method from main with arrayInt--> populateArray(arrayInt). (2 points) Write a method to print an array. Signature of...
in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data...
in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data from a file named “data.txt”. Suppose the data in the file are integers between 0 and 9. Write your main function to print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output. An example is given as follows. (25 points) Suppose the original matrix is 1...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array to test, thanks. get_location_of_min This function takes a matrix as a 2-D array of integers with NUM_COLS width, the number of rows in the matrix and two integer pointers. The function finds the location of the minimum value in the matrix and stores the row and column of that value to the memory location pointed to by the pointer arguments. If the minimum value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT