You are on page 1of 10

CSL-113 : Computer Programming Lab

Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 1
Write a C++ Program that computes the sum of two matrices. Each matrix is of 2 rows and 2
columns and will be created from user input.

SOURCE CODE:
#include<iostream>
using namespace std;

int main()
{
int a1[2][2],a2[2][2],a3[2][2];

//first for is used to take input value of columns


//each nested for is used to take input values of second raw

//first or to take 4 values of first matrix

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

{
for(int j=0;j<2;j++)
{
cout<<"enter "<<"["<<i*1<<"]["<<j*1<<"]"<< " of matrix A"<<": ";
cin>>a1[i][j];
}
}
//cout to give distance between matix A and B

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

cout<<"\n\n\n";

//to take values of matri B

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

{
for(int j=0;j<2;j++)
{
cout<<"enter "<<"["<<i*1<<"]["<<j*1<<"]"<< " of matrix B"<<": ";
cin>>a2[i][j];
}
}

cout<<"\n\n\n";

//this for to add valus of matix A and B

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

{
for(int j=0;j<2;j++)
{
//cout<<"enter "<<"["<<i*1<<"]["<<j*1<<"]"<< " of matrix B"<<": ";
a3[i][j]=a1[i][j]+a2[i][j];
}
}

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

cout<<"A= "<<a1[0][0]<<" "<<a2[0][1]<<" + "<<"B= "<<a2[0][0]<<" "<<a2[0][1]<<" = C=


"<<a3[0][0]<<" "<<a3[0][1]<<"\n\n";
cout<<" "<<a1[1][0]<<" "<<a2[1][1]<<" "<<" "<<a2[1][0]<<" "<<a2[1][1]<<"
"<<a3[1][0]<<" "<<a3[1][1];

return 0;

OUTPUT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 2
Write a C++ program to calculate the result of three sections of a semester. Following are the rules for
result:
1. Three are 3 sections.
2. Each section has 8 students.
3. Each student takes 5 courses.

Marks for each subject of every student of each section must be taken from the user.

Calculate the result of every student of each section as follows:


1. Obtained marks (Sum of all courses marks). Max. marks of each subject are 100
2. Percentage

Source code
#nclude<iostream>
Using namespace std;

int main()
{

float a1[5][8],a2[8][5],a3[8][5];

cout<<"ENTER MARKS FOR FIRST SECTION \n\n";

for(int i=0;i<8;i++)
{
cout<<"\n\n";
for(int j=0;j<5;j++)
{
cout<<"enter marks of "<<j+1<<" subject of student "<<i+1<<":"<<" ";
cin>>a1[i][j];
}
}

cout<<"\n\nENTER MARKS FOR SECOND SECTION\n\n";

for(int i=0;i<8;i++)
{
cout<<"\n\n";
for(int j=0;j<5;j++)
{
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

cout<<"enter marks of "<<j+1<<" subject of student "<<i+1<<":"<<" ";


cin>>a2[i][j];
}
}

cout<<"\n\nENTER MARKS FOR THIRD SECTION \n\n";

for(int i=0;i<8;i++)
{
cout<<"\n\n";
for(int j=0;j<5;j++)
{
cout<<"enter marks of "<<j+1<<" subject of student "<<i+1<<":"<<" ";
cin>>a3[i][j];
}
}

cout<<"\n\n";
cout<<"OBTAIND MARKS OF STUDENTS ARE";

cout<<"\n\nMARKS OBTAINEDAND PERCENTAGE OF STUDENTS OF FIRST SECTION\n\n";

cout<<"obtained marks of student 1 = "<<a1[0][0]+a1[0][1]+a1[0][2]+a1[0][3]+a1[0][4]<<" and percentage


="<<(a1[0][0]+a1[0][1]+a1[0][2]+a1[0][3]+a1[0][4])*0.2<<" %";
cout<<"\nobtained marks of student 2 = "<<a1[1][0]+a1[1][1]+a1[1][2]+a1[1][3]+a1[1][4]<<" and percentage
="<<(a1[1][0]+a1[1][1]+a1[1][2]+a1[1][3]+a1[1][4])*0.2<<" %";
cout<<"\nobtained marks of student 3 = "<<a1[2][0]+a1[2][2]+a1[2][2]+a1[2][3]+a1[2][4]<<" and percentage
="<<(a1[2][0]+a1[2][2]+a1[2][2]+a1[2][3]+a1[2][4])*0.2<<" %";
cout<<"\nobtained marks of student 4 = "<<a1[3][0]+a1[3][1]+a1[3][2]+a1[3][3]+a1[3][4]<<" and percentage
="<<(a1[3][0]+a1[3][1]+a1[3][2]+a1[3][3]+a1[3][4])*0.2<<" %";
cout<<"\nobtained marks of student 5 = "<<a1[4][0]+a1[4][1]+a1[4][2]+a1[4][3]+a1[4][4]<<" and percentage
="<<(a1[4][0]+a1[4][1]+a1[4][2]+a1[4][3]+a1[4][4])*0.2<<" %";
cout<<"\nobtained marks of student 6 = "<<a1[5][0]+a1[5][1]+a1[5][2]+a1[5][3]+a1[5][4]<<" and percentage
="<<(a1[5][0]+a1[5][1]+a1[5][2]+a1[5][3]+a1[5][4])*0.2<<" %";
cout<<"\nobtained marks of student 7 = "<<a1[6][0]+a1[6][1]+a1[6][2]+a1[6][3]+a1[6][4]<<" and percentage
="<<(a1[6][0]+a1[6][1]+a1[6][2]+a1[6][3]+a1[6][4])*0.2<<" %";
cout<<"\nobtained marks of student 8 = "<<a1[7][0]+a1[7][1]+a1[7][2]+a1[7][3]+a1[7][4]<<" and percentage
="<<(a1[7][0]+a1[7][1]+a1[7][2]+a1[7][3]+a1[7][4])*0.2<<" %";

cout<<"\n\nMARKS OBTAINED AND PERCENTAGE OF STUDENTS OF SECOND SECTION\n\n";

cout<<"obtained marks of student 1 = "<<a2[0][0]+a2[0][1]+a2[0][2]+a2[0][3]+a2[0][4]<<" and percentage


="<<(a2[0][0]+a2[0][1]+a2[0][2]+a2[0][3]+a2[0][4])*0.2<<" %";
cout<<"\nobtained marks of student 2 = "<<a2[1][0]+a2[1][1]+a2[1][2]+a2[1][3]+a2[1][4]<<" and percentage
="<<(a2[1][0]+a2[1][1]+a2[1][2]+a2[1][3]+a2[1][4])*0.2<<" %";
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

cout<<"\nobtained marks of student 3 = "<<a2[2][0]+a2[2][2]+a2[2][2]+a2[2][3]+a2[2][4]<<" and percentage


="<<(a2[2][0]+a2[2][2]+a2[2][2]+a2[2][3]+a2[2][4])*0.2<<" %";
cout<<"\nobtained marks of student 4 = "<<a2[3][0]+a2[3][1]+a2[3][2]+a2[3][3]+a2[3][4]<<" and percentage
="<<(a2[3][0]+a2[3][1]+a2[3][2]+a2[3][3]+a2[3][4])*0.2<<" %";
cout<<"\nobtained marks of student 5 = "<<a2[4][0]+a2[4][1]+a2[4][2]+a2[4][3]+a2[4][4]<<" and percentage
="<<(a2[4][0]+a2[4][1]+a2[4][2]+a2[4][3]+a2[4][4])*0.2<<" %";
cout<<"\nobtained marks of student 6 = "<<a2[5][0]+a2[5][1]+a2[5][2]+a2[5][3]+a2[5][4]<<" and percentage
="<<(a2[5][0]+a2[5][1]+a2[5][2]+a2[5][3]+a2[5][4])*0.2<<" %";
cout<<"\nobtained marks of student 7 = "<<a2[6][0]+a2[6][1]+a2[6][2]+a2[6][3]+a2[6][4]<<" and percentage
="<<(a2[6][0]+a2[6][1]+a2[6][2]+a2[6][3]+a2[6][4])*0.2<<" %";
cout<<"\nobtained marks of student 8 = "<<a2[7][0]+a2[7][1]+a2[7][2]+a2[7][3]+a2[7][4]<<" and percentage
="<<(a2[7][0]+a2[7][1]+a2[7][2]+a2[7][3]+a2[7][4])*0.2<<" %";

cout<<"\n\nMARKS OBTAINED AND PERCENTAGE OF STUDENTS OF THIRD SECTION\n\n";

cout<<"obtained marks of student 1 = "<<a3[0][0]+a2[0][1]+a3[0][2]+a3[0][3]+a3[0][4]<<" and percentage =


"<<(a3[0][0]+a2[0][1]+a3[0][2]+a3[0][3]+a3[0][4])*0.2<<" %";
cout<<"\nobtained marks of student 2 = "<<a3[1][0]+a2[1][1]+a3[1][2]+a3[1][3]+a3[1][4]<<" and percentage
="<<(a3[1][0]+a2[1][1]+a3[1][2]+a3[1][3]+a3[1][4])*0.2<<" %";
cout<<"\nobtained marks of student 3 = "<<a3[2][0]+a2[2][2]+a3[2][2]+a3[2][3]+a3[2][4]<<" and percentage
="<<(a3[1][0]+a2[1][1]+a3[1][2]+a3[1][3]+a3[1][4])*0.2<<" %";
cout<<"\nobtained marks of student 4 = "<<a3[3][0]+a2[3][1]+a3[3][2]+a3[3][3]+a3[3][4]<<" and percentage
="<<(a3[3][0]+a2[3][1]+a3[3][2]+a3[3][3]+a3[3][4])*0.2<<" %";
cout<<"\nobtained marks of student 5 = "<<a3[4][0]+a2[4][1]+a3[4][2]+a3[4][3]+a3[4][4]<<" and percentage
="<<(a3[4][0]+a2[4][1]+a3[4][2]+a3[4][3]+a3[4][4])*0.2<<" %";
cout<<"\nobtained marks of student 6 = "<<a3[5][0]+a2[5][1]+a3[5][2]+a3[5][3]+a3[5][4]<<" and percentage
="<<(a3[5][0]+a2[5][1]+a3[5][2]+a3[5][3]+a3[5][4])*0.2<<" %";
cout<<"\nobtained marks of student 7 = "<<a3[6][0]+a2[6][1]+a3[6][2]+a3[6][3]+a3[6][4]<<" and percentage
="<<(a3[6][0]+a2[6][1]+a3[6][2]+a3[6][3]+a3[6][4])*0.2<<" %";
cout<<"\nobtained marks of student 8 = "<<a3[7][0]+a2[7][1]+a3[7][2]+a3[7][3]+a3[7][4]<<" and percentage
="<<(a3[7][0]+a2[7][1]+a3[7][2]+a3[7][3]+a3[7][4])*0.2<<" 3%";

return 0;
}

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Output

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 3
Write a C++ program, that read 12 integer values from user, store values in Matrix of 4 X 3. Create
another Matrix of 4 X 3, divide each element of Matrix1 by five, and store the result in the Matrix2.

Print Matrix A, with heading shown, correctly spaced.


Print Matrix B, with heading shown, correctly spaced.

Source code
#include<iostream>
using namespace std;

int main()
{
float a1[4][3],a2[4][3];

for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
{
cout<<"enter value of row "<<i+1<<" and column "<<j+1<<" : ";
cin>>a1[i][j];
}
}

cout<<"\n\n MATRIX A - ORIGNAL\n\n";

cout<<" "<<a1[0][0]<<" "<<a1[0][1]<<" "<<a1[0][2]<<endl;


cout<<" "<<a1[1][0]<<" "<<a1[1][1]<<" "<<a1[1][2]<<endl;
cout<<" "<<a1[2][0]<<" "<<a1[2][1]<<" "<<a1[2][2]<<endl;
cout<<" "<<a1[3][0]<<" "<<a1[3][1]<<" "<<a1[3][2]<<endl;

for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
{
a2[i][j]=a1[i][j]/5;
}
}

cout<<"\n\n MATRIX A - DIVIDED BY 5\n\n";

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

cout<<" "<<a2[0][0]<<" "<<a2[0][1]<<" "<<a2[0][2]<<endl;


cout<<" "<<a2[1][0]<<" "<<a2[1][1]<<" "<<a2[1][2]<<endl;
cout<<" "<<a2[2][0]<<" "<<a2[2][1]<<" "<<a2[2][2]<<endl;
cout<<" "<<a2[3][0]<<" "<<a2[3][1]<<" "<<a2[3][2]<<endl;

return 0;
}

Output

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 07:Array(Multi-dimentional)

You might also like