Using c++ language Write a program that does the following Read 25 integer numbe
ID: 3762595 • Letter: U
Question
Using c++ language
Write a program that does the following
Read 25 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 5x5 two dimensional array (also known as matrix) named Values. Set of 5 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 a form of square matrix. (Always remember this)
Create a function arrayStats() that takes the two dimensional array as parameter from main() and computes the following stats.
Output Format:
Column 1 and 2: Sum = Average= [Elements from col 1 & 2 combined]
Row 4 and 5: Min = Max = [Elements from row 4 & 5 combined]
Array: Sum = Average= [This includes all the array elements]
Note:
You can use as many loops as you wish.
Column 1 and 2 are columns with column indices 0 and 1 respectively
Row 4 and 5 are rows with row indices 3 and 4 respectively
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
void arrayStats(int Values[5][5])
{
int sum = 0;
double avg;
for(int i = 0; i < 5; i++)
for(int j = 0; j < 2; j++)
sum += Values[i][j];
avg = (double)sum/10;
int min, max;
min = max = Values[0][3];
for(int i = 0; i < 5; i++)
for(int j = 3; j < 5; j++)
{
if(Values[i][j] < min)
min = Values[i][j];
if(Values[i][j] > max)
max = Values[i][j];
}
cout<<"The sum of the first 2 columns is: "<<sum<<endl;
cout<<"The average of the first 2 columns is: "<<avg<<endl;
cout<<"The minimum of last 2 columns is: "<<min<<endl;
cout<<"The maximum of last 2 columns is: "<<max<<endl;
}
int main()
{
ifstream ip;
ip.open("Numbers.txt");
int Values[5][5];
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
ip>>Values[i][j];
cout<<"The Square matrix read from the file is: "<<endl;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
cout<<Values[i][j]<<" ";
cout<<endl;
}
arrayStats(Values);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.