Você está na página 1de 6

NUST

School of Civil and Environmental Engineering

ASSIGNMENT #2

Lecturer
Asad Ali Shah
NUST
SCEE

Course: Fundamentals of Programming


Name: Syed Ali Hamza Naqvi
Reg#: 2008-NUST-BE-CE-135
Section: B
Class: CE- 2

Solve all questions; output should be clear and understandable. Cheating is not allowed if any cheating
is found both get zero marks. (20 Marks)
Q 1. Write a program that takes input from user in array of size 10. Sort this array in
ascending order once the user has finished entering the numbers. (5 Marks)
Note: Use only single 1D array and minimum size of array is 10.

Sample Output
Enter 10 integers for the Array:
5
4
1
0
8
13
11
2
7
9
Sorted Array:
0 1 2 4 5 7 8 9 11 13

Sol#
#include<iostream.h>
#include<conio.h>
void main()
{
int num[10],i,c,j,k,l;
cout<<"Enter 10 integers for the array:\n";
for(i=0;i<=9;i++)
cin>>num[i];
cout<<endl;
for(j=0;j<=9;j++)
{
for(i=0;i<=9-j;i++)
{
if(num[i]>num[i+1])
{
c=num[i];
num[i]=num[i+1];
num[i+1]=c;
}
//For checking purposes, I put these 2 loops to see how the program was working//
for(k=0;k<=9;k++)
cout<<num[k]<<"\t";
}
cout<<endl;
for(l=0;l<=9;l++)
cout<<num[l]<<"\t";

}
cout<<"Sorted array\n";
for(i=0;i<=9;i++)
cout<<num[i]<<"\t";
getch();
}

Q 2. Write a program that takes two 2x2 matrices as input. Multiply the two matrices and
store the result in the resultant matrix. Also display the result stored in the resultant
matrix. Go to the link below to check your results. (5 Marks)

http://joemath.com/applets/multmat/
Sample Output
Enter values for Matrix A
Value for Matrix A [0][0]
1
Value for Matrix A [0][1]
2
Value for Matrix A [1][0]
3
Value for Matrix A [1][1]
4

Enter values for Matrix B


Value for Matrix B [0][0]
5
Value for Matrix B [0][1]
6
Value for Matrix B [1][0]
7
Value for Matrix B [1][1]
8

The Resultant Matrix C is


19 22
43 50

Sol#
#include<iostream.h>
#include<conio.h>
void main()
{
int matA[2][2], matB[2][2],i,j,matC[2][2];
cout<<"Enter Matrix A \n";
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
cin>>matA[i][j];
cout<<"\n";
cout<<"Enter Matrix B\n";
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
cin>>matB[i][j];
cout<<"\n";
matC[0][0]=matA[0][0]*matB[0][0]+matA[0][1]*matB[1][0];
matC[0][1]=matA[0][0]*matB[0][1]+matA[0][1]*matB[1][1];
matC[1][0]=matA[1][0]*matB[0][0]+matA[1][1]*matB[1][0];
matC[1][1]=matA[1][0]*matB[0][1]+matA[1][1]*matB[1][1];
cout<<"Matric C is \n";
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
cout<<matC[i][j]<<"\t";
cout<<endl;
}
getch();
}

Q 3. Write program that rotates the matrix 180 degrees. The values for the matrix should
be acquired from the user first. Print matrix before and after rotation. (7 Marks)

e.g.
Matrix is:
1 2 3
4 5 6
7 8 9
Output is after rotation of 180 degree:
9 8 7
6 5 4
3 2 1

Hint: Try revolving the matrix 90 degrees first and then repeat the process once more.

Sol#
#include<iostream.h>
#include<conio.h>
void main()
{
int mat[3][3],i,j;
cout<<"Enter the numbers \n";
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
cin>>mat[i][j];
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
cout<<mat[i][j]<<"\t";
cout<<"\n";
}
//The matrix was possible to be rotated directly 180 degrees, this step is just additional as
required...//
cout<<"\n Matrix after 90 degrees rotation..\n";
for(i=0;i<=2;i++)
{
for(j=2;j>=0;j--)
cout<<mat[j][i]<<"\t";
cout<<"\n";
}
cout<<"\n Matrix after 180 degrees rotation..\n";
for(i=2;i>=0;i--)
{
for(j=2;j>=0;j--)
cout<<mat[i][j]<<"\t";
cout<<"\n";
}
getch();
}

Q 4. Get input in char array (minimum size is 10). write a program that will search total
no. of occurrence of char in array that user enter. The program should not be case
sensitive i.e. ‘A’ and ‘a’ are both considered equal. (3 Marks)

Sample Output:
Enter a String:
AsadAli
Character to search for:
A
The no. of occurrence of ‘A’ in the array is: 3

Sol#
#include<iostream.h>
#include<conio.h>
void main()
{
char name[10],a;
int i,count;
for(i=0;i<=9;i++)
name[i]='\0';
cout<<"Enter the string:\n";
cin>>name;
cout<<"Enter the character to be searched...\n";
cin>>a;
count=0;
for(i=0;i<=9;i++)
{
if(a==name[i]||a-32==name[i]||a+32==name[i])
count++;
}
cout<<"The occurance of '"<<a<<"' is "<<count<<" times";
getch();
}

Você também pode gostar