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

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...
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...
Develop a python program to - Create a 2D 10x10 numpy array and fill it with...
Develop a python program to - Create a 2D 10x10 numpy array and fill it with the random numbers between 1 and 9 (including 0 and 9). - Assume that you are located at (0,0) (upper-left corner) and want to move to (9,9) (lower-right corner) step by step. In each step, you can only move right or down in the array. - Develop a function to simulate random movement from (0,0) to (9,9) and return sum of all cell values...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
Need this C++ code to be modified to work in C, still using 2d arrays... #include...
Need this C++ code to be modified to work in C, still using 2d arrays... #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; //Implementation of main function int main() { srand(time(NULL)); //Declaration of output,result,i,j,k ,figure as integer type and //assign flog with 0 int output[5][5], result[5], i, j, k, figure = 0; //Display statement cout << "The classic BINGO cards contains 25 squares arranged in five vertical" << endl; cout << "columns and five side to side rows. Each...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT