0% found this document useful (0 votes)
46 views7 pages

Arrays Programs

Uploaded by

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

Arrays Programs

Uploaded by

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

Arrays

Q-46. Twenty-five numbers are entered from the keyboard into an


array. The number to be searched is entered through the keyboard by the
user. Write a program to find if the number to be searched is present in the
array and display its location index in the array.

#include<stdio.h>
using namespace std;
int main()
{
int i ,T,a[25];
for(i=0;i< 25;i++)
{
printf("Enter the numbers=");
scanf("%d",&a[i]);
}
printf("Enter the value to be searched :");
scanf("%d", &T);
for(i=0;i< 25;i++)
{
if(a[i] == T)
printf("Index Location ==> %d",i+1);
}
return(0);
}

Q-47. Write a program which receive the data of two 3 x 3 matrices


from the user, then add them and store the result in a third matrix of 3 x 3.

#include<stdio.h>
using namespace std;
int main()
{
int i,j,A[3][3],B[3][3],C[3][3];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the Value Matrix A ="); scanf("%d",&A[i][j]);
printf("Enter the Value Matrix B ="); scanf("%d",&B[i][j]);
C[i][j]=A[i][j]+B[i][j];
}
}

for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(" C[%d][%d] = %d\n",i,j,C[i][j]);
return(0);
}

Q-48. Write a program which receive the data of two 3 x 2 matrix and 3
x 3 matrix from the user, then multiply them and store the result in a third
matrix of order 2 x 3.

// this is a matrix multiplication program.


#include<stdio.h>
using namespace std;
int main()
{
int i,j,k,A[2][3],B[3][3],C[2][3];
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
printf("Enter Values for Matrix A=");
scanf("%d",&A[i][j]);
}

for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf("Enter Values for Matrix B=");
scanf("%d",&B[i][j]);
}

for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
C[i][j]=0;
for(k=0;k<3;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%d\t",C[i][j]);
printf("\n");
}
return(0);
}

Q-49. Implement the Selection Sort algorithms on a set of 15 numbers.

#include<iostream.h>
using namespace std;
int main()
{
System(“CLS”);
int K,A[15],N=15,TEMP,MIN,J,LOC;
for(K=0;K<15;K++)
{
printf(“Enter A[ %d ]=”,K);
scanf(“%d”, &A[K]);
}
for(K=0;K<15;K++)
{
MIN=A[K];
LOC=K;
for(J=K+1;J<N;J++)
{
if(MIN>A[J])
{
MIN=A[J];
LOC=J;
}
}
TEMP=A[K];
A[K]=A[LOC];
A[LOC]=TEMP;
}
for(K=0;K<=14;K++)
printf(“Enter A[ %d ]= %d\n”,K,A[K]);
retrun(0);
}
Q-50. Implement the Bubble Sort algorithms on a set of 15 numbers.

// this is a bubble sort program.


#include<stdio.h>
using namespace std;
int main()
{
int i,j,t,a[15];
for(i=0;i<=14;i++)
{
printf("Enter the numbers=");
scanf("%d",&a[i]);
}
for(i=0;i<=14;i++)
{
for(j=0;j<(14-i);j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
for(i=0;i<=14;i++)
printf("%d\n",a[i]);
return(0);
}

Q-51. Implement the Insertion sort algorithms on a set of 15 numbers.


#include<stdio.h>
using namespace std;
int main()
{
int i, A[15], j, key;
for(i=0;i<15;i++)
{
printf("Enter the numbers="); scanf("%d",&A[i]);
}
for(j=1;j<15;j++)
{
key=A[j];
i=j-1;
while(i>=0 && A[i]>key)
{
A[i+1]=A[i];
i=i-1;
}
A[i+1]=key;
}
for(i=0;i<15;i++)
printf("%d\n",A[i]);
return(0);
}

Week # 16

Strings

Q-52. Write a program that will accept two strings and store them into
two separate character arrays then copy a string into a third character
array using function strcpy() and concatenate the second string with the
third string using function strcat(). Display the third string.

#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char N[20],T[20];
int C,L;
printf("Enter your name please : ");
scanf("%s",&N);
printf("N==%s\n\n",N);
strcpy(T,N);
printf("T==%s\n\n",T);
strcat(T," ");
strcat(T,"baloch");
printf("string contenation==%s\n\n",T);
return(0);
}

Q-53. Write a program that will input a string and store it into a
character array. Then find the length of that string using strlen() function
and display this length.

#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char N[20],T[20];
int C,L;
printf("Enter your name please : ");
scanf("%s",&N);
printf("N==%s\n\n",N);
L=strlen(N);
printf("string length==%d\n\n",L);
return(0);
}

Q-54. Write a program that will input two string and store them into
two separate character array. Then compare these two strings using
function strcmp() and display the larger string.

#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char N[20],T[20];
int C,L;
printf("Enter first string please : ");
scanf("%s",&N);
printf("Enter second string please : ");
scanf("%s",&T);
if (strcmp(N,T)>0);
printf("first string is larger==%s\n\n",N);
else
printf("Second string is larger==%s\n\n",T);
return(0);
}

You might also like