Computer Programming And Utilization(CPU) - Jun - 2012

Question 2

(a) Explain the multiple (ladder) if-else with syntax and proper example.


#include<stdio.h>
#include<conio.h>
main()
{
 int mark;
 clrscr();
 printf("Enter Mark of your Subject : ");
 scanf("%d",&mark);
 if(mark < 0 || mark > 100)
  printf("\nInvalid Mark");
 else if(mark < 35)
  printf("\nYou are Fail!!");
 else if(mark < 55)
  printf("\nYou are pass with pass class");
 else if(mark < 65)
  printf("\nYou are pass with third class");
 else if(mark < 75)
  printf("\nYou are pass with second class");
 else if(mark < 85)
  printf("\nYou are pass with first class");
 else if(mark <= 100)
  printf("\nYou are pass with Distinction class");
 getch();
}
    

(b) Assume that you want to make the sum of 1 to 100. Give the necessary code to perform the same using (1) For loop (2) While loop (3) Do-while loop.


#include<stdio.h>
#include<conio.h>
main()
{
 int i=0,sum=0; 
 clrscr();
 printf("\nSum of 1 to 100.");
 printf("\n\nUsing For Loop");
 for(i=1;i<=100;i++)
  sum += i;
 printf(" - Sum is : %d",sum);
 i=0,sum=0;
 printf("\n\nUsing while Loop");
 while(i <= 100)
 {
  sum += i;
  i++;
 }
 printf(" - Sum is : %d",sum);
 i=0,sum=0;
 printf("\n\nUsing For Loop");
 do
 {
  sum += i;
  i++;
 }while(i <= 100);
 printf(" - Sum is : %d",sum);
 getch();
}
    

Question 3

(a) Write a program to find the minimum value from the array of 3x3.


#include<stdio.h>
#include<conio.h>
main()
{
 int number[3][3] = {{15,20,14},{6,5,2},{45,58,62}},minimum;
 int i,j;
 clrscr();
 minimum = number[0][0];
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   if(number[i][j] < minimum)
    minimum = number[i][j];
  }
 }
 printf("Minimum value from the array of 3 X 3 is : %d",minimum);
 getch();
}
    

(c) Explain the use of break statement with example.


#include<stdio.h>
#include<conio.h>
main()
{
 int i=0;
 clrscr();
 for(i=0;i<100;i++)
 {
  printf("i = %d\n",i);
  if(i == 10)
   break; 
 }
 getch();
}
    

Question 4

(a) What is function? Explain the function definition, function prototype and function call with example.


#include<stdio.h>
#include<conio.h>
int functionExample(int number); // function prototype
main()
{
 clrscr();
 functionExample(10); // function call
 functionExample(0);  // function call
 functionExample(-10); // function call
 getch();
}
int functionExample(int number) // function definition
{
 if(number == 0)
  printf("\nNumber is 0");
 else if(number > 0)
  printf("\nNumber is positive");
 else if(number < 0)
  printf("\nNumber is negative");
}
    

(c) What is call by value? Clear it with example.


#include<stdio.h>
#include<conio.h>
void printNumber(int);
main()
{
 int X = 5;
 clrscr();
 printf("Number X is = %d\n",X);
 printNumber(X);
 printf("Number X is = %d\n",X);   
 getch(); 
}
void printNumber(int X)
{
 X = 10;
 printf("Number X is = %d\n",X);
}
    

Question 5

(a) Write a program which accepts a long string and print only characters at positions which are multiple of 3.


#include<stdio.h>
#include<conio.h>
main()
{
 char *str,c;
 int i=0;
 clrscr();
 while( ( c = getchar() ) != EOF )
 {
  str[i] = c; i++;
 }
 str[i] = EOF;
 for(i=0;str[i] != EOF;i++)
 {
  if( (i%3) == 0)
   printf("%c",str[i]);
 }
 getch();
}
    

Question 6

(a) Write a function which accepts a string and returns the length of the string.


#include<stdio.h>
#include<conio.h>
main()
{
 char s[] = "jaydip panchal";
 int len;
 clrscr();
   len = getLength(s);
 printf("Length of String is : %d",len);
 getch(); 
}
int getLength(char s[])
{
 int i;
 for(i=0;s[i] != '\0';i++);
 return i; 
}
    

(b) What is structure? Define the structure and explain how to access the structure members.


#include<stdio.h>
#include<conio.h>
/* Defining Structure named student */
struct student
{
 char *name;
 int rollNo;
}
/* Defining Structure named student */
main()
{
 struct student s;
 clrscr();
 /* Accessing structure member name */
 s.name = "help2engg"; 
 printf("Student's name is : %s\n",s.name);
 /* Accessing structure member name */
 s.rollNo = "5";
 printf("Student's roll no is : %s",s.rollNo);
 getch();
}

    

(c) Show the use of the malloc() function.


#include <stdio.h>
#include <alloc.h>
void main(void)
{
 char *str;
    clrscr();
    /* allocate memory for string */
    if ((str = (char *) malloc(10)) == NULL)
    {
        printf("Not enough memory to allocate buffer\n");
        exit(1);  /* terminate program if out of memory */
    }
    /* copy "Hello" into string */
    str = "GTU";
    /* display string */
    printf("University is %s\n", str);
    getch();
}

    

Question 7

(b) Write a program to display the contents of a given file.


#include <stdio.h>
#include <conio.h>
int main()
{
 FILE *file1;
 char string [BUFSIZ];
 clrscr();
 file1= fopen ("FILE1" , "r");
 if (file1 == NULL) perror ("Error opening file");
 else
 {
  while (fgets (string , BUFSIZ, file1) != NULL)
  {
   puts (string);
  }
  fclose (file1);
 }
 getch();
 return 0;
}
    

1 comment: