Searching an element in an Array :
Matrix Transpose
Matrix Multiplication
- int main()
- {
- int *array; //array
- int n; //array-length
- int x; //number to be searched
- printf ( "Enter the number of elements: " );
- scanf ("%d" , &n);
- array = (int*)malloc( n * sizeof(int));
- for(int i=0 ; i < n ; i++ ){
- scanf ("%d",&array[i]);
- }
- printf( "Enter the number to be searched: ");
- scanf( "%d" , &x);
- for(int i=0 ; i<n ; i++){
- if( x == array[i]){
- printf ("The number is found in the list at place %d",i+1);
- break;
- }
- }
- return 0;
- }
Sorting the elements in an arrray :
- int main()
- {
- int *array;
- int n;
- int i,j;
- printf ( "Enter the number of elements: " );
- scanf ("%d" , &n);
- array = (int*)malloc( n * sizeof(int));
- for( i=0 ; i<n ; i++ ){
- scanf("%d",&array[i]);
- }
- int temp;
- for( i=0 ; i<n ; i++){
- for( j=i ; j<n ; j++){
- if(a[i]>a[j]){
- temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
- }
- for( i=0 ; i<3 ; i++){
- printf(" %d",array[i]);
- }
- return 0;
- }
Binary Search:
- int main()
- {
- int *array;
- int n;
- int i,j,
- printf("Enter the number of elements: \n");
- scanf("%d",&n);
- array = (int*)malloc(n*sizeof(int));
- printf("Enter the elements one by one:\n");
- for(i=0; i<n ;i++){
- scanf("%d",&array[i]);
- }
- printf("Given array elements\n");
- for (i=0; i <n ; i++){
- printf("%d\n",array[i]);
- }
- /* Bubble sorting begins */
- int temp;
- for(i=0; i< n ; i++){
- for(j=0; j< (n-i-1) ; j++){
- if(array[j] > array[j+1]){
- temp = array[j];
- array[j] = array[j+1];
- array[j+1] = temp;
- }
- }
- }
- printf("Sorted array is...\n");
- for(i=0; i<n ; i++ ){
- printf("%d\n",array[i]);
- }
- printf("Enter the element to be searched\n");
- scanf("%d", &keynum);
- /* Binary searching begins */
- int keynum;
- int low,mid,high;
- low=1;
- high=n;
- do {
- mid= (low + high) / 2;
- if ( keynum < array[mid] )
- high = mid - 1;
- else if ( keynum > array[mid])
- low = mid + 1;
- } while( keynum!=array[mid] && low <= high);
- if( keynum == array[mid] )
- printf("SUCCESSFUL SEARCH\n");
- else
- printf("Search is FAILED\n");
- return 0 ;
- }
Variance :
- #include<math.h> //for sqrt();
- int main()
- {
- float elements[10],mean,SD,variance,sum;
- int i,n;
- printf("Enter the number of elements: ");
- scanf("%d",&n);
- printf ("Enter the entries: \n");
- for( i=0 , sum = 0; i<n ; i++){
- scanf ("%d",&elements[i]);
- sum += elements[i];
- }
- mean=(float)(sum)/(float)(n);
- float numerator;
- for( i=0 , numerator = 0 ; i<n ; i++ ){
- numerator+=(float)((mean-elements[i])*(mean-elements[i]));
- variance = float(numerator / n);
- SD=sqrt(variance);
- }
- printf("\nMean: %.2f\nVariance: %.2f\nStandard Deviation: .2f\n",mean,variance,SD);
- return 0;
- }
Matrix Transpose
- int main()
- {
- int matrix[5][5].n;
- int temp;
- int i,j;
- printf("Enter the no. of rows (or) columns: ");
- scanf("%d",&r);
- printf("\n\nEnter the entries: \n");
- for ( i=0 ; i<r ; i++)
- for( j=0 ; j<r ; j++)
- scanf ( "%d",&a[i][j] );
- for( i=0 ; i<r ; i++)
- for( j=i+1 ; j<r ; j++){
- temp=a[i][j];
- a[i][j]=a[j][i];
- a[j][i]=temp;
- }
- printf ("\n\n");
- }
- for(i=0;i<r;i++){
- for(j=0;j<r;j++){
- printf( " %d",a[i][j]);
- }
- printf ("\n");
- }
- return 0;
- }
- int main()
- {
- int a[5][5],b[5][5],c[5][5];
- int i,j,k;
- int r1,r2,c1,c2;
- printf("Enter the number of rows and columns of matrices A and B:");
- scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
- if( c1 == r2 ) {
- printf("Enter the entries for matrix A:\n");
- for( i=0 ; i<r1 ; i++)
- for( j=0 ; j<c1 ; j++)
- scanf("%d",&a[i][j]);
- printf("\nEnter the entries for matrix B:\n");
- for( i=0 ; i<r2 ; i++)
- for( j=0 ; j<c2 ; j++)
- scanf("%d",&b[i][j]);
- //matrix multiplication
- for( i=0 ; i<r1 ; i++){
- for( j=0 ; j<c2 ; j++){
- c[i][j] = 0;
- for( k=0 ; k<r2 ; k++ )
- c[i][j] += a[i][k] * b[k][j] ;
- }
- }
- printf("\n\nThe product matrix: \n");
- for( i=0 ; i<r1 ; i++){
- for( j=0 ; j<c2 ; j++)
- printf(" %d",c[i][j]);
- printf("\n");
- }
- }else
- printf("\n\nThe matrices cannot be multiplied......");
- return 0;
- }
No comments:
Post a Comment