I just started studying programming at University level and this is the first course.
I just encountered the task below and I'm getting a little bit confused to say the least.
The task is the following "
The C++ program below reads from the file matrix.txt integers table inserting it in the 5x5 table (matrix [5][5]). The program prints the matrix on screen, calculates the sum of the elements and prints the sum on screen making use of functions print_matrix () and count_sum(). Your task is to make up the functions in question. The values situated on the rows are separated by space."
Does this mean I need to have the file "matrix.txt" or where am I supposed to get the values to the matrix from?
you should have been provided with the input file.
if you were not, you can make one in a simple text editor like notepad in windows, just put some values in it and keep going.
Well actually I am only supposed to provide the functions and upload them to my schools system so basically I shouldn't even need to have the .txt file .
The code I wrote in my original message was handed to me and now my task is to create functions void rint_matrix(int matrix[5][5]); and int calculate_sum(int matrix[5][5]);
#include <iostream>
#include <fstream>
usingnamespace std;
void print_matrix(int matrix[5][5]);
int calculate_sum(int matrix[5][5]);
int main(void)
{
int matrix[5][5];
int sum;
ifstream file("matrix.txt");
if (!file)
{
cout << "*** File cannot be opened!\n";
exit(1);
}
else
{
for (int y=0; y<5;y++)
{
for (int x=0;x<5;x++)
{
file >> matrix[y][x];
cout << y << ',' << x << '=' << matrix[y][x] << '\n';
}
}
file.close();
cout << "Matrix:" << endl;
print_matrix(matrix);
sum = calculate_sum(matrix);
cout << "Sum of elements: " << sum << endl;
}
}
void print_matrix(int matrix[5][5])
{
cout << "1 Fill this part out with a program\n";
}
int calculate_sum(int matrix[5][5])
{
int sum{0};
cout << "2 Fill this part out with a program\n";
return sum;
}
For both of these functions, you need a nested loop. Have a look at how this is done for reading from the file (L18-22 OP) and modify what's done in the inner-loop as needed.