22
More Questions 1) Write a C program to read a matrix A of size nXn and two arrays X and Y of size n and calculate XA-Y? popo

More Questions

  • Upload
    peri

  • View
    23

  • Download
    0

Embed Size (px)

DESCRIPTION

More Questions. 1) Write a C program to read a matrix A of size nXn and two arrays X and Y of size n and calculate XA-Y?. popo. More Questions. #include< conio.h > #include< stdio.h > void main() { int a[10][10],x[10],y[10], i,j,r,c,R [10][10],k; clrscr (); - PowerPoint PPT Presentation

Citation preview

Page 1: More Questions

More Questions• 1) Write a C program to read a matrix A of

size nXn and two arrays X and Y of size n and calculate XA-Y?

popo

Page 2: More Questions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• int a[10][10],x[10],y[10],i,j,r,c,R[10][10],k;• clrscr();• printf("enter order of the mat :- ");• scanf("%d%d",&r,&c);• if(r==c)• {• printf("enter matrix");• for(i=0;i<r;i++)• {• for(j=0;j<c;j++)• {• scanf("%d",&a[i][j]);• }• }

popo

Page 3: More Questions

More Questions• printf("given matrix\n");• for(i=0;i<r;i++)• {• for(j=0;j<c;j++)• {• printf("%d ",a[i][j]);• }• printf("\n");• }• printf("Enter first array :- ");• for(i=0;i<r;i++)• {• scanf("%d",&x[i]);• }• printf("Enter second array :- ");• for(i=0;i<r;i++)• {• scanf("%d",&y[i]);• }

popo

Page 4: More Questions

More Questions• //calculating X*A• for(i=0;i<1;i++)• {• for(j=0;j<c;j++)• {• R[i][j]=0;• for(k=0;k<r;k++)• {• R[i][j]=R[i][j]+x[k]*a[k][j];• }• }• }• //calculation of X*A - Y• for(i=0;i<1;i++)• {• for(j=0;j<c;j++)• {• R[i][j]=R[i][j]-y[j];• }• }•

popo

Page 5: More Questions

More Questions• printf(“\nresultant matrix “);• for(i=0;i<1;i++)• {• for(j=0;j<c;j++)• {• printf("%d ",R[i][j]);• }• printf("\n");• }• }• else• {printf(“Not a square matrix”);}• getch();• }

popo

Page 6: More Questions

More Questions• 2) Write a function that receives a sorted

array of integers and an integer value and insert the integer value in its correct place in the sorted array

popo

Page 7: More Questions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• void array(int [],int,int );• int a[10],i,j,t,n,no;• clrscr();• printf("Enter limit ");• scanf("%d",&n);• for(i=0;i<n;i++)• {• scanf("%d",&a[i]);• }• printf("given array :- ");• for(i=0;i<n;i++)• {• printf("%d ",a[i]);• }

popo

Page 8: More Questions

More Questions• //sorting• for(i=0;i<n;i++)• {• for(j=i+1;j<n;j++)• {• if(a[i]>a[j])• {• t=a[i];• a[i]=a[j];• a[j]=t;• }• }• }• printf("\nsorted array ");• for(i=0;i<n;i++)• {• printf("%d ",a[i]);• }• printf("\nEnter a no to insert");• scanf("%d",&no);• //passing sorted array to function• array(a,n,no);• getch();• }

popo

Page 9: More Questions

More Questions• void array(int a[10],int n,int no)• {• int i,pos=0;• //finding the position to insert• for(i=0;i<n;i++)• {• if(no>=a[i])• {• pos=i;• }• }• pos=pos+1;•

popo

Page 10: More Questions

More Questions• //inserting• for(i=n;i>pos;i--)• {• a[i]=a[i-1];• }• a[pos]=no;• printf("\nnew inserted array ");• for(i=0;i<=n;i++)• {• printf("%d ",a[i]);• }• }

popo

Page 11: More Questions

More Questions• 3) Write a C program to read an array of size

n and calculate its standard deviation given by

• √∑(xi-x)2/n

popo

Page 12: More Questions

More Questions• #include<conio.h>• #include<math.h>• #include<stdio.h>• void main()• {• int a[10],i,n,x,s=0; float sd;• clrscr(); printf("enter limit ");• scanf("%d",&n);• printf("Enter array ");• for(i=0;i<n;i++)• {• scanf("%d",&a[i]);• }• printf("given array :- ");• for(i=0;i<n;i++)• {• printf("%d ",a[i]);• s=s+a[i];• }• x=s/n; s=0;• for(i=0;i<n;i++)• {• s=s+pow((a[i]-x),2);• }• sd=sqrt(s/n);• printf("\nSD=%f",sd);• getch();• }

popo

Page 13: More Questions

More Questions• 4) Write a C program to read a data stored in

a file. Find the value of• ∑(xi2/n) –∑(xi/n)2 and write it in another file

popo

Page 14: More Questions

More Questions• #include<conio.h>• #include<math.h>• #include<stdio.h>• void main()• {• int a[10],i,n,no,s=0,k=0;• float z;• FILE *fp,*fp2;• clrscr();• fp=fopen("num.txt","w");• printf("Enter limit ");• scanf("%d",&n);• for(i=1;i<=n;i++)• {• printf("enter a no ");• scanf("%d",&no);• putw(no,fp);• }• fclose(fp);•

popo

Page 15: More Questions

More Questions• fp=fopen("num.txt","r");• for(i=0;i<n;i++)• {• a[i]=getw(fp);• }• fclose(fp);• printf("content of the array ");• for(i=0;i<n;i++)• {• printf("%d ",a[i]);• s=s+pow(a[i],2);• k=k+a[i];• }• z=(s/n)-pow((k/n),2);• printf("\nresult= %f",z);• fp2=fopen("second.txt","w");• fprintf(fp2,"%f",z);• fclose(fp2);• getch();• }

popo

Page 16: More Questions

Find biggest word in the given sentences

Page 17: More Questions

17

Find biggest word in the given sentences• #include<conio.h>• #include<string.h>• #include<stdio.h>• main()• {• int i=0,ss=0,big=0;• char s[100],c,temp[50],bigstr[50];• clrscr();• printf("enter a text ");• gets(s);• printf("GIven string : %s\n",s);

1

Page 18: More Questions

18

Find biggest word in the given sentences• for(i=0;s[i]!='\0';i++)• {• if(s[i]==' ')• {• temp[ss]='\0';• //printf(" %s %d\n",temp,ss);• if(ss>big)• {• big=ss;• strcpy(bigstr,temp);• }• ss=0;• }• else• {• temp[ss]=s[i];• ss++;• //printf("%c",s[i]);• }• }

2

Page 19: More Questions

19

Find biggest word in the given sentences• temp[ss]='\0';• //printf("%s %d",temp,ss);• if(ss>big)• {• big=ss;• strcpy(bigstr,temp);• }• printf("\n Big string %s",bigstr); • printf("\n Biggest string length %d",big);• getch();• }

3

Page 20: More Questions

Search the given sub string pattern in the main string

Page 21: More Questions

More Questions• #include<conio.h>• #include<string.h>• #include<stdio.h>• void main()• {• char str[20],sub[20],temp[20][20];• int i,j,n=0,k,l,f=1;• clrscr();• printf("enter main string");• gets(str);• printf("sub string ");• gets(sub);• for(i=0;str[i]!='\0';i++)• {• l=i;k=0;• for(j=0;sub[j]!='\0';j++)• {• temp[n][k]=str[l];• k++;l++;• }• temp[n][k]='\0';• n++;• }•

popo

Page 22: More Questions

More Questions• for(i=0;i<strlen(str)-strlen(sub)+1;i++)• {• //printf("\n%s",temp[i]);• if(strcmp(temp[i],sub)==0)• {• printf("\npattern found");• f=0;• }• }• if(f==1)• {• printf("\npattern not found");• }• getch();• }

popo