Write a C++ program to enter the values of a 4x3 array, then print the find the largest, smallest, average value in each row as shown in the following example: array and 10 7 largest=10, smallest=7, Average=8.33 6. 2 1 largest=6, smallest=1, Average=3 6. 1 largest=8, smallest=1, Average=5 3 6. 1 largest=6, smallest=1, Average=3.33
Write a C++ program to enter the values of a 4x3 array, then print the find the largest, smallest, average value in each row as shown in the following example: array and 10 7 largest=10, smallest=7, Average=8.33 6. 2 1 largest=6, smallest=1, Average=3 6. 1 largest=8, smallest=1, Average=5 3 6. 1 largest=6, smallest=1, Average=3.33
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
100%
With source code file pls .
To download it
Expert Solution
Code & Explanation
As per the rules we can't upload files. You have to copy it.
Here is the required code.
#include<iostream>
using namespace std;
int main() {
// declare an array of 4x3 i.e. 4 rows and 3 columns
int a[4][3];
// take user input
// outer for loop iterates for each row
for (int i = 0; i < 4; i++) {
// inner for loop iterates for each column in a row
for (int j = 0; j < 3; j++) {
// take the user input and store at ith row and jth column
cin >> a[i][j];
}
}
// insert a new line between input and output
cout << endl;
// now iterate over each row again
// this time to calculate largest, smallest and average
for (int i = 0; i < 4; i++) {
// initialize largest with the smallest integer
// choosing the smallest integer to compare with large values
int largest = INT_MIN;
// initialize smallest with the largest integer
int smallest = INT_MAX;
// initialize total to 0
double total = 0;
// iterate over each column in current row
for (int j = 0; j < 3; j++) {
// get the value at ith row and jth column
int current_num = a[i][j];
// add it to `total`
total += current_num;
// check if `current_num` is larger than `largest`
if (current_num > largest) {
// if yes, update largest
largest = current_num;
}
// check if `current_num` is smaller than `smallest`
if (current_num < smallest) {
smallest = current_num;
}
// print the value with a tab
cout << current_num << '\t';
}
// calculate average
double avg = total / 3;
// put 2 tabs
cout << "\t\t";
// print the largest value
cout << "largest=" << largest;
// print the smallest value
cout << ", smallest=" << smallest;
// print average
cout << ", Average=" << avg;
// insert a new line
cout << endl;
}
return 0;
}
Create a file main.cpp and paste the above code in it.
Please read the comments to understand the code line by line.
Step by step
Solved in 2 steps with 3 images
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY