All code must be in C++ using G++ compiler write a program that performs the fol
ID: 3809608 • Letter: A
Question
All code must be in C++ using G++ compiler
write a program that performs the following Read 16 integer numbers from an input file Numbers.txt (You can manually create this file first and fill it with any sixteen integer values written one below the other so that reading them through code becomes easy) Store them in 4x4 two dimensional array (also known as matrix) named values. Set of 4 numbers should belong to each row as you keep reading the input file. Display the contents of the two dimensional (2D) array using nested for loops in the form of square matrix. (Always remember this) Create a function stats() that takes the two dimensional array as parameter from main() and computes Column 1 sum, Column 2 average, Row 3 minimum, and Row 4 maximum.Explanation / Answer
Program:-
#include<iostream>
#include<fstream>
using namespace std;
void stats(int values[4][4])
{
int sum1=0;
for(int i=0;i<4;i++)
sum1+=values[i][0]; //column 1 sum
cout<<"Column 1 sum is :"<<sum1<<endl;
int avg;
int sum2=0;
for(int i=0;i<4;i++)
sum2+=values[i][1]; //column 1 sum
avg=sum2/4; //column 2 average
cout<<"Column 2 average is :"<<avg<<endl;
int min=9999;
for(int i=0;i<4;i++)
if(min>values[i][2])
min=values[i][2]; // find minimum
cout<<"Column 3 minimum is :"<<min<<endl;
int max=-1;
for(int i=0;i<4;i++)
if(max<values[i][3])
max=values[i][3]; // find maximum
cout<<"Column 4 maximum is :"<<max<<endl;
}
int main()
{
fstream file("Numbers.txt"); //opening file
int values[4][4];
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
file>>values[i][j]; //reading file to array
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
cout<<values[i][j]<<" "; //printing values
cout<<endl<<endl;
}
stats(values);
return 0;
}
output:-
41 54 12 3
2 56 14 52
78 91 88 22
6 7 59 42
Column 1 sum is :127
Column 2 average is :52
Column 3 minimum is :12
Column 4 maximum is :52
Process exited normally.
Press any key to continue . . .
Numbers.txt:-
41
54
12
3
2
56
14
52
78
91
88
22
6
7
59
42
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.