You are on page 1of 5

COMPUTER PROGRAMMING LAB

LAB: 06
NAME: ZONISH AHMED
ENROLLMENT NUMBER:02-
235222-022
Tasks:
01
Write a program to create a 2D array of size 3x3. The program takes input for each cell in the array and
then calculates and displays the sum of each row.

#include<iostream> using
namespace std; int main()
{
int ar[3][3]; int
sum[3] = { 0 };
int k = 0;

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout << "Enter the value at row " << i << " and column " << j << " : "; cin >>
ar[i][j];
}
}

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout << ar[i][j] << " ";
}
cout << endl;
}

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
sum[k] = sum[k] + ar[i][j];
}
cout << "Sum of "<<k<<" row : "<<sum[k]; cout <<
endl;
k++;
}
return 0;
}

Tasks: 02
Write a program that takes a 3x3 matrix as input and asks for a number entered and prints out its
position in the matrix. It displays not found if the number is not in the matrix.
#include<iostream> using
namespace std; int main()
{
int ar[3][3];
bool found = false;

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout << "Enter the value at row " << i << " and column " << j << " : ";
cin >> ar[i][j];
}
}

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout << ar[i][j] << " ";
}
cout << endl;
}
int value, R, C;

cout << "Enter a number to find : "; cin >> value;

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
if (value == ar[i][j])
{
found = true;
R = i;
C = j;
}
}

}
if (found == true)
{
cout << "Found! Position: [" << R << "] [" << C << "] " << endl;
}
else
{
cout << "Not Found! " << endl;
}
return 0;
}
Tasks: 03
Write a program which calculates the transpose of a 3x3 matrix.

#include<iostream> using
namespace std; int main()
{
int ar[3][3];
int t[3][3];

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout << "Enter the value at row " << i << " and column " << j << " : ";
cin >> ar[i][j];
}
}

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout << ar[i][j] << " ";
}
cout << endl;
}

cout << "Transposed: " << endl;

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
t[j][i] = ar[i][j];
}
}

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout << t[i][j] << " ";
}
cout << endl;
}
return 0;
}

You might also like