Programming Questions

Q:

If the following structure is written to a file using fwrite(), can fread() read it back successfully?

struct emp

{

    char *n;

    int age;

};

struct emp e = { "Sujay",15};

FILE *fp;

fwrite (&e, sizeof (e), 1, fp);

Answer

No, since the structure contains a char pointer while writing the structure to the disk using fwrite() only the value stored in the pointer n would get written. When this structure is read back the address would be read back but it is quite unlikely that the desired string would be present at this address in memory

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2670
Q:

Indicate what would the SWAP macro be expanded to on preprocessing. Would the code compile?

#define SWAP (a, b, c ) (c t; t = a, a = b, b = t; )

main()

{

    int x = 10, y = 20;

    SWAP (x, y, int );

    printf ( " %d%d ", x, y);

}

Answer

( int t ; t = a, a = b, b = t ;);


This code won't compile since declaration of t cannot occur within parentheses.


 

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2595
Q:

Point out the error, if any, in the while loop.

main()

{

  int i = 1;

  while ()

  {

     printf ( "%d", i++);

     if (i >10) 

     break ;

   }

}

Answer

The condition in the while loop is a must.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2574
Q:

public static void main string[] args Meaning is?

Answer

Here in this declaration public static void main string[] args, each keyword has its importance.


 


1. public - Here public is an access specifier which allows the main method to be accessible everywhere.


 


2. static - static helps the main method to get loaded without getting called by any instance/object.


 


3. void - void clarifies that the main method will not return any value.


 


4. main - It's the name of the method.


 


5. String[] args - Here we are defining a String array to pass arguments at the command line. args is the variable name of the String array.

Report Error

View answer Workspace Report Error Discuss

8 2546
Q:

Improve the following code using typedef.

struct node

{

      int data1;  float data2;

       struct node *left;

       struct node *right;

};

struct node *ptr;

ptr = (struct node *) malloc (sizeof (struct node) ); 

Answer

typedef struct node * treeptr


typedef struct node


{


         int data1;


         float data2;


         treeptr *left;


         treeptr *right;


}treenode;


treeptr ptr;


ptr = ( treeptr ) malloc ( sizeof ( treenode ) );

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2486
Q:

There is a mistake in the following code. Add a statement in it to remove it.

main()

{

     int a;

     a = f (10, 3.14) ;

     printf ( " %d ", a );

}

f (int aa, float bb)

{

    return ( ( float ) aa + bb );

}

Answer

Add the following function prototype in main ():


float f ( int, float );

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2476
Q:

Write a c program for insertion sort.

Answer

#include<stdio.h>
int main(){

  int i,j,s,temp,a[20];

  printf("Enter total elements: ");
  scanf("%d",&s);

  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);

  for(i=1;i<s;i++){
      temp=a[i];
      j=i-1;
      while((temp<a[j])&&(j>=0)){
      a[j+1]=a[j];
          j=j-1;
      }
      a[j+1]=temp;
  }

  printf("After sorting: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);

  return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting:  0 2 3 7 9

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2473
Q:

Write a c program to create dos command type.

Answer

#include
int main( int count,char * argv[] ) {
    int i;
    FILE *ptr;
    char *str;
    char ch;
    if( count == 1) {


         printf( "The syntax of the command is incorrect.\n" );
    }
    for( i=1;i<cout;i++ ){
         ptr=fopen(argv[i],"r");
         if(ptr==NULL){
             printf("The system cannot find the file specified.");
             if(count>2)
                 printf("\nError occurred while procesing : %s.\n",argv[i]);
         }
         else {
             if(count>2) {
                 printf("%s\n\n",argv[i]);
             }
             while((ch=getc(ptr))!=-1)
                 printf("%c",ch);
         }
         fclose(ptr);
    }
    return 0;
}


 


Save the above file as open.c, compile and execute the go to command mode (current working directory) and write: open xy.c (xy.c any file present in that directory)
To run the open command in all directories and drive you will have to give the path of current working directory in command mode. Write:
C:tc\bin>PATH c:\tc\bin
Now press enter key. Now your open command will work in all directory and drive.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2470