Programming Questions

Q:

Write a c program for merge sort.

Answer

#include
#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);

int main(){
  
    int merge[MAX],i,n;

    printf("Enter the total number of elements: ");
    scanf("%d",&n);

    printf("Enter the elements which to be sort: ");
    for(i=0;i<n;i++){
         scanf("%d",&merge[i]);
    }

    partition(merge,0,n-1);

    printf("After merge sorting elements are: ");
    for(i=0;i<n;i++){
         printf("%d ",merge[i]);
    }

   return 0;
}

void partition(int arr[],int low,int high){

    int mid;

    if(low<high){
         mid=(low+high)/2;
         partition(arr,low,mid);
         partition(arr,mid+1,high);
         mergeSort(arr,low,mid,high);
    }
}

void mergeSort(int arr[],int low,int mid,int high){

    int i,m,k,l,temp[MAX];

    l=low;
    i=low;
    m=mid+1;

    while((l<=mid)&&(m<=high)){

         if(arr[l]<=arr[m]){
             temp[i]=arr[l];
             l++;
         }
         else{
             temp[i]=arr[m];
             m++;
         }
         i++;
    }

    if(l>mid){
         for(k=m;k<=high;k++){
             temp[i]=arr[k];
             i++;
         }
    }
    else{
         for(k=l;k<=mid;k++){
             temp[i]=arr[k];
             i++;
         }
    }
  
    for(k=low;k<=high;k++){
         arr[k]=temp[k];
    }
}


Sample output:

Enter the total number of elements: 5
Enter the elements which to be sort: 2 5 0 9 1
After merge sorting elements are: 0 1 2 5 9

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1954
Q:

Can you suggest any other way of writing the following expression such that 30 is used only once?

a <= 20 ? b = 30 : c = 30 ;

Answer

*( ( a <= 20 ) ? &b : &c ) = 30;

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 1914
Q:

Write a program for matrix multiplication in c

Answer

#include <stdio.h>


int main() {


  int a[5][5], b[5][5], c[5][5], i, j, k, sum = 0, m, n, o, p;


  printf( "\nEnter the row and column of first matrix" );


  scanf( "%d %d", &m, &n );


  printf( "\nEnter the row and column of second matrix" );


  scanf( "%d %d", &o, &p );


  if(n!=o) {


      printf( "Matrix mutiplication is not possible" );


      printf( "\nColumn of first matrix must be same as row of second matrix" );


  }


  else {


      printf( "\nEnter the First matrix" );


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


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


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


      printf( "\nEnter the Second matrix" );


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


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


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


      printf( "\nThe First matrix is\n" );


      for( i=0; i<m; i++ ) {


      printf("\n");


      for( j=0; j<n; j++ ) {


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


      }


      }


      printf( "\nThe Second matrix is\n" );


      for( i=0; i<o; i++ ) {


      printf("\n");


      for( j=0; j<p; j++ ) {


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


      }       


      }


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


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


           c[i][j] = 0;


      for( i=0; i<m; i++ ) { //row of first matrix


      for( j=0; j<p; j++ )  {  //column of second matrix


           sum = 0;


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


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


           c[i][j] = sum;


      }


      }


  }


  printf( "\nThe multiplication of two matrix is\n" );


  for( i=0; i<m; i++ ) {


      printf("\n");


      for( j=0; j<p; j++ ) {


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


      }


  }


  return 0;


}

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1908
Q:

Point out the error, if any, in the following program.

main()

{

    int a = 10;

    void f();

    a = f();

    printf ( "\n %d", a );

}

void f()

{

    printf ( "\n Hi ");

}

Answer

In spite of defining the function f() as returning void, the program is trying to collect the value returned by f() in the variable a.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1904
Q:

Point out the error, if any, in the following program.

main()

{

    int ( *p )() = fun;

    ( *P ) ();

}

fun ()

{

    Printf ( "\nLoud and clear" );

Answer

Here we are initalising the function pointer p to the address of the function fun(). But during this initialisation the function has not been defined. Hence an error.


To eliminate this error add the prototype of the fun() before declaration of p, as shown below:


extern int fun();    or simply  int fun();

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1866
Q:

Point out the error, ifany, in the followingb code?

typedef struct

{

     int data;

     NODEPTR link;

} *NODEPTR;

 

Answer

A typedef defines a new name for a type, and in simpler cases like the one shown below you can define a new structure type and a typedef for it at the same time.


typedef struct


{


    char name[20];


    int age;


} emp;


However, in the structure defined in this question, there is an error because a typedef declaration cannot be used until it is defined. In the given code fragment the typedef declaration is not yet defined at he point where the link field is declared.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1856
Q:

What is the difference between the following declarations?

extern int fun();

int fun();

Answer

There is no difference except for the fact that the first one gives a hint that the function fun() is probably in another source file.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1849
Q:

What would be the output of the following program?

main()

{

    static int a[20];

    int i = 0;

    a[i] = i++;

    printf ("\n%d%d%d", a[0],  a[1], i);

}

Answer

0  0  1


That's what some of the compilers would give. But some other compiler may give a different answer. The reason is, when a single expression causes the same object to be modified and then inspected the behaviour is undefined.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1843