0% found this document useful (0 votes)
6 views3 pages

Graded Lab 3

The document outlines a graded lab assignment for a programming fundamentals course, focusing on 2D arrays in C++. It requires students to write a C++ program that reads data from a file, calculates row-wise, column-wise, and diagonal-wise sums using user-defined functions. An example input and output are provided to illustrate the expected results.

Uploaded by

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

Graded Lab 3

The document outlines a graded lab assignment for a programming fundamentals course, focusing on 2D arrays in C++. It requires students to write a C++ program that reads data from a file, calculates row-wise, column-wise, and diagonal-wise sums using user-defined functions. An example input and output are provided to illustrate the expected results.

Uploaded by

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

NAME: AHMAD SHAHZAD

REG No. : L1F23BSCS0295


B-12
Programming Fundamentals
PF-B12
Graded Lab 3
Topic: 2D arrays,

Marks: 3+3+4=10

Task 1
Write a program in C++ that reads data from a file named “data.txt”. Create dynamic memory
according to the data. Now your task is to perform the following tasks. You have to use three user
define functions to perform the tasks.

I. Row wise Sum


II. Column wise Sum
III. Diagonal wise Sum

Example

data.txt
2 4 3 1
3 9 2 3
9 7 1 5
8 6 7 8

Output:
Sum row wise: 10, 17, 22, 22, 29
Sum col wise: 22, 26,13,17
Sum diagonal wise: 20

SOLUTION:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

void rowsum(int array[4][4]);

void columnsum(int array[4][4]);

void diagonalsum(int array[4][4]);

int main()
{
int array[4][4];

ifstream read;
read.open("data.txt");
while (!read.eof())
{
for (int i = 0; i<4; i++)
{
for (int j = 0; j<4; j++)
{
read >> array[i][j];
}
}
}

rowsum(array);

columnsum(array);

diagonalsum(array);

system("pause");
return 0;
}

void rowsum(int array[4][4])


{
int sum = 0;
cout << "Sum of Rows : ";
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
sum = sum + array[i][j];
}
cout << sum;
cout << " ";
}
cout << endl;
}

void columnsum(int array[4][4])


{
int sum = 0;
cout << "Sum of Columns : ";
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
sum = sum + array[j][i];
}
cout << sum;
cout << " ";
}
}

void diagonalsum(int array[4][4])


{
int sum = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (i == j)
{
sum = sum + array[i][j];
}
}
}
cout << endl;
cout << "Sum of Diagonal :" << sum << endl;
}

You might also like