Anda di halaman 1dari 17

# Introduction to Computing

Lab Manual
Week 04 Lab 02

L1F12BSCS2353
Arrays 2D

## Faculty of Information Technology

UCP Lahore Pakistan

Objective ...........................................................................................................................................3
Things to remember: ..........................................................................................................................3

Objective

## To be able to write a C++ program using 2D arrays.

Things to remember:

Use meaningful variable names
Plan your code carefully on a piece of paper before you implement it

Write a C++ program with 2 dimensional integer array of size 4x4. Initialize the array with 0 and
get integer values from user in column wise order as shown below.
1st value

5th value

2nd value

6th value

3rd value

7th value

4th value

8th value

9th value
10th
value
11th
value
12th
value

13th
value
14th
value
15th
value
16th
value

## Print the array in reverse order as shown below

16th value, 15th value, 14th value, ............, 3rd value, 2nd value, 1st value

#include<iostream.h>
void main()
{
int ar[4][4];
int i,j;

for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<"Enter Numbers: ";
cin>>ar[j][i];

}
cout<<"Entered Numbers Sequence: " <<endl <<endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<ar[i][j];
}
cout<<endl;
}
cout<<"Numbers in Reverse Order: " <<endl <<endl;

for(i=3;i>=0;i--)
{
for(j=3;j>=0;j--)

{
cout<<ar[j][i]<<" ";
}
}

Continuing from part 1, take the transpose of the array and print the content of the array row
wise. Output should be as follow.
1st value, 2nd value, 3rd value, ................., 14th value, 15th value, 16th value.
#include<iostream.h>
void main()
{
int ar[4][4];
int i,j;

for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<"Enter Numbers: ";
cin>>ar[j][i];

## cout<<"Entered Numbers Sequence: " <<endl <<endl;

for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<ar[i][j];
}
cout<<endl;
}
cout<<"Numbers in Reverse Order: " <<endl <<endl;

for(i=3;i>=0;i--)
{
for(j=3;j>=0;j--)
{
cout<<ar[j][i]<<" ";
}
}
cout<<endl<<endl <<"Numbers in Correct Order: " <<endl <<endl;

for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{

cout<<ar[j][i]<<" ";
}
}

Write a C++ program to create an integer 2d array of size n x 8 where n is any positive integer of
your choice. Take n integer values from user and store these values into 1st row. No value
should be greater than 99999999. Split the integer into its digits and store each digit into the
corresponding column as shown below.
10101010
1
0
1
0
1
0
1
0

12345678
1
2
3
4
5
6
7
8

03306763
0
3
3
0
6
7
6
3

91029876
9
1
0
2
9
8
7
6

11111212
1
1
1
1
1
2
1
2

Write a C++ program, to create a name directory of friends, you are not allowed to use any of
the string functions. User can enter any name of length not more than 15 and maximum 100
names can be stored into the directory. Your program should maintain directory in a sorted
order. 1st column of the array is index. Look at the example below where name of friends are
Hassan, Ahmed, Waleed, Ahmer, Ubair, Zaid

2
3
4
5

A
H
W
Z

H
A
A
A

M
S
L
I

## Print the names on the screen.

E
S
E
D

R
A
E

N
D

#include<iostream.h>
#include<conio.h>
void main()
{
char name[100][15]={0};
int i,j,k=0,f=0;;
cout<<"Press '.' on the end of the name. And '!' if names Finised " <<endl <<endl;
for(i=0;i<100;i++)
{
if(k==0)
{
cout<<"Enter Name: ";
for(j=0;j<15;j++)
{

cin>>name[i][j];

if(name[i][j]=='.')
{
break;
}
if(name[i][j]=='!')
{
k=1;

break;
}

}
else
{
break;
}

cout<<endl;
for(i=0;i<100;i++)
{
if(f==0)
{
cout<<i<<" ";
for(j=0;j<15;j++)
{
if(name[i][j]=='!')
{
f=1;

break;
}
else

if(name[i][j]=='.')
{
cout<<endl;
}

else
{
cout<<name[i][j];
}

}
}
else
{
break;
}
cout<<endl;
}

Write a C++ program, to create 2 2d arrays of integer type of equal size of your choice. For
simplicity let us create 2 3x3 arrays. Use these arrays as matrices and apply matrix
multiplication. Store results into another array of same size and print the values.

#include<iostream.h>
void main()
{
int mat1[3][3];
int mat2[3][3];
int mat[3][3]={0};
int i,j;
cout<<endl<<"Enter Elements of 1st Matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat1[i][j];
}
}
cout<<endl<<"Enter Elements of 2nd Matrix: ";
for(i=0;i<3;i++)

{
for(j=0;j<3;j++)
{
cin>>mat2[i][j];
}
}
cout<<endl;

mat[0][0]=mat1[0][0]*mat2[0][0]+mat1[0][1]*mat2[1][0]+mat1[0][2]*mat2[2][0];
mat[0][1]=mat1[0][0]*mat2[0][1]+mat1[0][1]*mat2[1][1]+mat1[0][2]*mat2[2][1];
mat[0][2]=mat1[0][0]*mat2[0][2]+mat1[0][1]*mat2[1][2]+mat1[0][2]*mat2[2][2];

mat[1][0]=mat1[1][0]*mat2[0][0]+mat1[1][1]*mat2[1][0]+mat1[1][2]*mat2[2][0];
mat[1][1]=mat1[1][0]*mat2[0][1]+mat1[1][1]*mat2[1][1]+mat1[1][2]*mat2[2][1];
mat[1][2]=mat1[1][0]*mat2[0][2]+mat1[1][1]*mat2[1][2]+mat1[1][2]*mat2[2][2];

mat[2][0]=mat1[2][0]*mat2[0][0]+mat1[2][1]*mat2[1][0]+mat1[2][2]*mat2[2][0];
mat[2][1]=mat1[2][0]*mat2[0][1]+mat1[2][1]*mat2[1][1]+mat1[2][2]*mat2[2][1];
mat[2][2]=mat1[2][0]*mat2[0][2]+mat1[2][1]*mat2[1][2]+mat1[2][2]*mat2[2][2];

## cout<<"Resultant Matrix: ";

cout<<endl;
for(i=0;i<3;i++)

{
for(j=0;j<3;j++)
{
cout<<mat[i][j]<<" ";

}
cout<<endl;
}