Question

In: Computer Science

C++ Program: Create a 3x3 matrix of int values. Implement five functions, each expecting the matrix...

C++ Program:

Create a 3x3 matrix of int values. Implement five functions, each expecting the matrix as parameter input. The first function must use a nested loop to prompt a user to enter each value. Show the indices when prompting for input and populate the matrix with these values. The second function outputs the matrix and formats the output to align all columns and rows to accommodate values up to 1000. The third function finds and returns the minimum value in the matrix. The fourth function finds and returns the maximum value in the matrix. The fifth function calculates and returns the average of values in the matrix. Call each function from the main and output the minimum, maximum, and average values returned by the functions in the main.

Example:

Input
Value at 0, 0: 7
Value at 0, 1: 2
Value at 0, 2: 10 
Value at 1, 0: 1 
Value at 1, 1: 12 
Value at 1, 2: 2 
Value at 2, 0: 3 
Value at 2, 1: 14 
Value at 2, 2: 19

Matrix:
[  7,  2, 10],
[  1, 12,  2],
[  3, 14, 19];

Min: 1
Max: 19
Avg: 7

Solutions

Expert Solution

SOLUTION-
I have solve the problem in C++ code with comments and screenshot for easy understanding :)

CODE-

//c++ code
#include <iostream>
#include <iomanip>
using namespace std;
//function to read input from user
void readInputs(int (&matrix)[3][3])
{
cout << "Input:-"<<endl;
//reading inputs deom user
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
cout << "value at "<< i << "," << j << ": "; //values
cin >> matrix[i][j];
}
}
//function to print matrix
void printMatrix(int (&matrix)[3][3])
{
cout << "\nMatrix:"<<endl;
//printing matrix
for(int i=0;i<3;i++){
cout << "[";
for(int j=0;j<3;j++){
if(j<2)
cout << setw(4) << right << matrix[i][j] << ",";
else
cout << setw(4) << right << matrix[i][j];
}
if(i<2)
cout << "]," << endl;
else
cout << "];" << endl;
}
}
//find mazimum from matrix
int findMaximum(int (&matrix)[3][3])
{
int max = -9999;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
if(matrix[i][j] > max)
max = matrix[i][j];
}
return max;
}
//find Minimum from matrix
int findMinimum(int (&matrix)[3][3])
{
int min = 99999;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
if(matrix[i][j] < min)
min = matrix[i][j];
}
return min;
}
//find Average from matrix
int Average(int (&matrix)[3][3])
{
int sum = 0;
float avg;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
sum += matrix[i][j] ;
}
avg=sum/9;
return avg;
}
//main code
int main()
{
int matrix[3][3];
readInputs(matrix);
printMatrix(matrix);
//print min,max,avg for matrix element
cout << "\nMinimum : " << findMinimum(matrix) <<endl;
cout << "Maximum : " << findMaximum(matrix) <<endl;
cout << "Average : " << Average(matrix) <<endl;
  
return 0;
}


SCREENSHOT-


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
write a C++ Program for matrix operations which include: 1. constructor in the form-- matrix::matrix(int numRows,...
write a C++ Program for matrix operations which include: 1. constructor in the form-- matrix::matrix(int numRows, int numColumns) 2. Implement a setter method of the form: -- void matrix::setElement(int row, int col) 3 Implement a getter method of the form: -- float matrix::getElement(int row, int col) 4. Make a threaded version of the matrix/matrix addition method. 5. Make a threaded version of the matrix/scalar multiplication method. 6. Matrix/matrix addition methods of the form: -- matrix matrix::matrixAdd(matrix inMatrix) // non-threaded version...
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of...
IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of coins in a jar. The program prints out the total dollars and cents in the jar. The program prompts the user to enter the number of coins (quarters, dimes, nickels, and pennies). Print out the number of coins entered for each coin type on separate lines followed by the total amount of money in the jar as dollars and cents as shown below.
IN C++ Write a program that reads in int values from the user until they enter...
IN C++ Write a program that reads in int values from the user until they enter a negative number like -1. Once the user has finished entering numbers, print out the highest value they’ve entered, the lowest value they’ve entered, and the total number of numbers they’ve entered. The negative number they entered should not be taken as one of the values entered.
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases in which the initial unit is Fahrenheit or something not recognizable. Your program should incorporate Fahrenheit to Celsius, Fahrenheit to Kelvin and unknown initial units (display an error message for this last one). You must use functions to calculate Fahrenheit degrees.
For this program you will implement the following utility functions to test mastery of C strings....
For this program you will implement the following utility functions to test mastery of C strings. *******you MUST use these these function***** void removeBlanks(char *src, char *dest); void replaceChar(char *src, char oldChar, char newChar); char *flipCase(const char *src); Please read the description of these functions carefully. In the removeBlanks function you will implement a routine that takes a string in as src and outputs the same string into dest but removing any blank space character encountered. For example, if the...
Design and implement a C++ program with functions to calculate the pre-tax charge: If the customer...
Design and implement a C++ program with functions to calculate the pre-tax charge: If the customer subscribes to a phone plan called Plan200, then he is charged $5 for the first 200 minutes. For each additional minutes, the customer will be charged $0.10. If the customer subscribes to a phone plan called Max20, then he is charged $0.05 for each minute up to $20. (I.e., the customer never needs to pay more than $20.) If the customer is not subscribed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT