0% found this document useful (0 votes)
31 views

Lab 04-SE1

The document provides instructions for 3 tasks related to file input/output and dynamic memory allocation in C++. Task 1 involves reading data from multiple files on matrix metadata and elements, allocating dynamic arrays to store the values, adding the matrices and printing the results. Task 2 involves reading numbers from a file into a dynamic array, counting the frequencies of numbers in different ranges, allocating a 2D array based on the counts, and storing the numbers from the 1D array into the 2D array. Task 3 prints the elements of the 2D array by category in a function.

Uploaded by

Musadaq Hanif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lab 04-SE1

The document provides instructions for 3 tasks related to file input/output and dynamic memory allocation in C++. Task 1 involves reading data from multiple files on matrix metadata and elements, allocating dynamic arrays to store the values, adding the matrices and printing the results. Task 2 involves reading numbers from a file into a dynamic array, counting the frequencies of numbers in different ranges, allocating a 2D array based on the counts, and storing the numbers from the 1D array into the 2D array. Task 3 prints the elements of the 2D array by category in a function.

Uploaded by

Musadaq Hanif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Object Oriented Programming BSSEF22

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>

using namespace std;

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.

You might also like