Instructions: C++ a) b) c) d) e) Use scanf function to accept the input from the
ID: 3706125 • Letter: I
Question
Instructions: C++ a) b) c) d) e) Use scanf function to accept the input from the user. Use floating point inputs wherever possible. All programs must use arrays and functions. Tabulate your results. Need hard copy of () source code and (ii) output for each program. Write a program that can calculate the sum of three equally sized matrices, [A]+ [B]+ [C]. The input specifications are these: Read the input from a file with the first line of the file being the number of rows and columns of each matrix. The rest of the file has the elements of the matrices. The output specification is to print the sum matrix. [Hint: Use a function named sum matrices to accept the 3 matrices and print the sum matrix inside the function] 1.Explanation / Answer
Code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iterator>
#include <vector>
using namespace std;
void writeFile(){
ofstream myfile;
myfile.open ("example.txt");
myfile << "3 3";
myfile << " 1 2 3 4 5 6 7 8 9";
myfile << " 11 12 13 4 5 6 7 8 9";
myfile << " 21 22 23 4 5 6 7 8 9";
myfile.close();
}
void sum(int row, int col, int **mat1, int **mat2, int **mat3){
int i= 0, j = 0, sum = 0;
cout << "calc sum ";
for(; i < row; i++){
for(; j <col; j++){
sum += *(*(mat1 + i) + j) + *(*(mat2 + i) + j) + *(*(mat3 + i) + j);
}
}
cout << "sum is " << sum;
}
int main() {
writeFile();
std::ifstream infile("example.txt");
std::string line;
int i = 0;
int row, col;
int **mat1, **mat2, **mat3;
while (std::getline(infile, line))
{
int a, b;
//cout << line << endl;
int j = 0, k, l, n;
if(i == 0){
string arr[2];
stringstream ssin(line);
while (ssin.good() && i < 2){
ssin >> arr[i];
cout << arr[i] << endl<<endl;
++i;
}
i = 1;
row = stoi(arr[0]);
col = stoi(arr[1]);
} else if(i == 1){
i = 0;
string arr[row * col];
stringstream ssin(line);
while (i < (row * col) && ssin.good()){
ssin >> arr[i];
cout << arr[i] << " " << i << " " << endl;
++i;
}
n = 0;
for(k = 0; k < row; k++){
for(l = 0; l < col; l++){
*(*(mat1 + k) + l) = stoi(arr[n]);
n++;
}
}
i = 2;
} else if(i == 2){
i = 0;
j = 0;
string arr[row * col];
stringstream ssin(line);
while (ssin.good() && i < (row * col)){
ssin >> arr[i];
cout << arr[i] << endl<<endl;
++i;
}
n = 0;
for(k = 0; k < row; k++){
for(l = 0; l < col; l++){
*(*(mat2 + k) + l) = stoi(arr[n]);
n++;
}
}
i = 3;
} else if(i == 3){
i = 0;
j = 0;
string arr[row * col];
stringstream ssin(line);
while (ssin.good() && i < (row * col)){
ssin >> arr[i];
cout << arr[i] << endl<<endl;
++i;
}
n = 0;
for(k = 0; k < row; k++){
for(l = 0; l < col; l++){
*(*(mat3 + k) + l) = stoi(arr[n]);
n++;
}
}
}
}
sum(row, col, mat1, mat2, mat3);
}
Explanations:
1. First I create a sample file with space as delimiter
2. Read that file
3. Every line is splitted by space using stringstream and loaded into array
4. Matrices are created from array
5. Passed double pointer to method and calculated sum