Lab 04-SE1
Lab 04-SE1
Lab 04 – 25-09-2022
Task 0: Copy "data.txt" in your drive. Run following program. The answer should be 7842. Next, open the file
"data.txt" in notepad, change count to 8 instead of 5. Add three more numbers, one of which should be
greater than 7842. Run your program again to test, whether output is correct or not.
#include <iostream>
#include <fstream>
int main(){
int i, count, *x, max;
ifstream input("data.txt");
input >> count;
x = new int [count];
for (i = 0 ; i < count ; i++){
input >> x[i];
if (i == 0) max = x[i];
else if (max < x[i]) max = x[i];
}
cout << "Max: " << max << '\n' ;
delete []x;
input.close();
return 0;
}
Task 1: Copy three data files related to matrix. "matrix.dat" contains Meta data. The number on the top represents
the number of matrices. Next line have rows and columns of each matrix. "matrix1.dat" & "matrix2.dat" have elements
of matrices.
Open three files. Like one file opened in Task 0. Read count of matrices. Next read rows & columns of each matrix,
declare dynamic array to read values from both matrices. Add the results and display in matrix form (for each matrix).
After the addition, delete both dynamic arrays. At the end, close all three files.
Task 2: File "numbers.txt" contains numbers one to nine. First line has count of numbers. Open file, read count,
declare dynamic array, read all values in array and close the file. Declare a pointer array of size three and a simple
array of counts of three elements. Next count number of elements in range one to three and store at index zero of
counts array. Next, count number of elements in range four to six & store at index one of counts array. Similarly, count
number of elements in range seven to nine & store at index two of counts array.
Next, declare dynamic array for each pointer in pointer's array, according to the counts stored in the array in previous
step. Run a nested loop and store all elements previous stored in one-dimensional array from file into pointers 2D
array. Delete one dimension dynamic array. Pass pointer two-dimensional array to a function to print each category
of elements in single row.