(DS-1)

1A.Write a program to store the elements in 1-D array and perform the operations like searching, sorting and reversing the elements. [Menu Driven]

#include<stdio.h>

void create();

void print();

void sort();

void search();

void reverse();

int a[5],n,i;

int main()

{

int ch;

do

{

printf("\n 1.create \n 2.print \n 3.sort \n 4.search \n 5.reverse \n 6.exit \n");

printf("\n Enter an option :");

scanf("%d",&ch);

switch(ch)

{

case 1:create();

break;

case 2:print();

break;

case 3:sort();

break;

case 4:search();

break;

case 5:reverse();

break;

case 6:break;

}

}while(ch!=6);

return 0;

}

void create()

{

printf("\n Enter the no of element :");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("\n Enter element for index %d :",i);

scanf("%d",&a[i]);

}

}

void print()

{

printf("\n The array is :");

for(i=0;i<n;i++)

{

printf("\t %d",a[i]);

}

}

void search()

{

int val;

printf("\n enter the value for searching : ");

scanf("%d",&val);

for(i=0;i<n;i++)

{

if(a[i]==val)

{

printf("%d is found at index %d ",val,i);

break;

}

}

if(i==n)

{

printf("\n %d is not found",val);

}

}

void reverse()

{

int temp;

for(i=0;i<n/2;i++)

{

temp=a[i];

a[i]=a[n-i-1];

a[n-i-1]=temp;

}

}

void sort()

{

int j,temp;

for(i=0;i<n;i++)

{

for(j=i+1;j<n;j++)

{

if(a[i]>a[j])

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

}

1B.Write a program to store the elements in 1-D array and perform the operations like searching, sorting and reversing the elements. [Menu Driven]

#include<stdio.h>

void create();

void create2();

void print();

void sort();

void merge();

int a[5],b[5],c[10],n1,n2,i,k;

int main()

{

int ch;

do

{

printf("\n 1.create \n 2.create2 \n 3.print \n 4.sort \n 5.merge \n 6.exit \n");

printf("\n enter a option :");

scanf("%d",&ch);

switch(ch)

{

case 1:create();

break;

case 2:create2();

break;

case 3: print();

break;

case 4: sort();

break;

case 5: merge();

break;

case 6: break;

}

}while(ch!=6);

return 0;

}

void create()

{

printf("\n enter the no of element :");

scanf("%d",&n1);

for(i=0;i<n1;i++)

{

printf("\n enter the value for index %d :",i);

scanf("%d",&a[i]);

}

}

void create2()

{

printf("\n enter the no of element :");

scanf("%d",&n2);

for(i=0;i<n2;i++)

{

printf("\n enter the value for index %d:",i);

scanf("%d",&b[i]);

}

}

void print()

{

printf("\n The array is:");

for(i=0;i<k;i++)

{

printf("\t %d",c[i]);

}

}

void sort()

{

int j,temp;

for(i=0;i<k;i++)

{

for(j=i+1;j<k;j++)

{

if(c[i]>c[j])

{

temp=c[i];

c[i]=c[j];

c[j]=temp;

}

}

}

}

void merge()

{

for(i=0;i<n1;i++)

{

c[i]=a[i];

}

k=n1;

for(i=0;i<n2;i++)

{

c[k]=b[i];

k++;

}

}

1C.Write a program to perform the Matrix addition, Multiplication and Transpose Operation. [Menu Driven]

 #include<stdio.h>

void create();

void create2();

void add();

void multiplication();

void transpose();

int a[5][5],b[5][5],c[10][10],i,j,k,r1,r2,c1,c2;

int main()

{

int ch;

do

{

printf("\n 1.create \n 2.create2 \n 3.add \n 4.multiplication \n 5.transpose \n 6.Exit\n");

printf("\n Enter an option :");

scanf("%d",&ch);

switch(ch)

{

case 1:create();

break;

case 2:create2();

break;

case 3: add();

break;

case 4:multiplication();

break;

case 5: transpose();

break;

case 6: break;

}

}while(ch!=6);

return 0;

}

void create()

{

printf("\n Enter the no. of rows :");

scanf("%d",&r1);

printf("\n Enter the no. of columns :");

scanf("%d",&c1);

for(i=0;i<r1;i++)

{

for(j=0;j<c1;j++)

{

printf("\n Enter the value for index %d%d :",i,j);

scanf("%d",&a[i][j]);

}

}

}

void create2()

{

printf("\n Enter the no of rows:");

scanf("%d",&r2);

printf("\n Enter the no of columns:");

scanf("%d",&c2);

for(i=0;i<r2;i++)

{

for(j=0;j<c2;j++)

{

printf("\n Enter the value for index %d%d:",i,j);

scanf("%d",&b[i][j]);

}

}

}

void print()

{

printf("\nThe array is C:\n");

for(i=0;i<r1;i++)

{

for(j=0;j<c2;j++)

{

printf("\t%d",c[i][j]);

}

}

}

void add()

{

if(r1==r2&&c1==c2)

{

for(i=0;i<r2;i++)

{

for(j=0;j<c2;j++)

{

c[i][j]=a[i][j]+b[i][j];

}

}

}

printf("\nThe array is C:\n");

for(i=0;i<r1;i++)

{

for(j=0;j<c2;j++)

{

printf("\t%d",c[i][j]);

}

printf("\n");

}

}

void multiplication()

{

if(r1==r2&&c1==c2)

{

for(i=0;i<r2;i++)

{

for(j=0;j<c2;j++)

{

c[i][j]=0;

for(k=0;k<c1;k++)

{

c[i][j]+=a[i][k]*b[k][j];

}

}

}

}

printf("\n the array is C:\n");

for(i=0;i<r1;i++)

{

for(j=0;j<c2;j++)

{

printf("%d\t",c[i][j]);

}

printf("\n");

}

}

void transpose()

{

for(i=0;i<r1;i++)

{

for(j=0;j<c2;j++)

{

printf("%d\t",c[j][i]);

}

printf("\n");

}

}

Comments

Popular posts from this blog

python(BI)

(PP-7)Create a class called Numbers, which has a single class attribute called MULTIPLIER, and a constructor which takes the parameters x and y (these should all be numbers). i. Write a method called add which returns the sum of the attributes x and y. ii. Write a class method called multiply, which takes a single number parameter a and returns the product of a and MULTIPLIER. iii. Write a static method called subtract, which takes two number parameters, b and c, and returns b - c. iv. Write a method called value which returns a tuple containing the values of x and y. Make this method into a property, and write a setter and a deleter for manipulating the values of x and y.