Thursday, 23 April 2015

IMPORTANT QUESTIONS

IMPORTANT QUESTIONS



Q1 [Remembering the C language]
Write a function/algorithm to evaluate the sum of {1, 4, 9, 16, . . . 100}.
Ans:
#include<stdio.h>
int square(int x){
       return x*x;
}
int main(){
       int sum;
       for(int i=0;i<=10;i++){
              sum=sum+square(i);
}
Printf(“Sum is %d”,sum);
}

Q2 [Recursion: A useful function, we will interact with, often in this course]
What does the following function return when it is called with 5 as its argument i.e. when called in main as xyz(5);
  if(x<=2)   return 1;
else return xyz(x-1)+ xyz(x-2)+ xyz(x-3);
}
Q3 [Linked List, a sample program: Explain the output of the following program]
#include<stdio.h>
#include<conio.h>
struct Node{
int data;
   Node *next;
   Node(int a){
       data=a;
       next=NULL;
   }
};
struct llist{
       Node *start;
   llist(){
       start=NULL;
   }
   void add(int x){
       Node *temp=new Node(x);
      if(start==NULL){
      start=temp;
      }
      else{
      temp->next=start;
         start=temp;
      }
   }
   void display(){
       Node *f=start;
      while(f!=NULL){
      printf("Value of the Node is: %d\n",f->data);
         f=f->next;
      }
   }
   void average(){
       Node *f=start;
      int total=0;
      int count=0;
      while(f!=NULL){
         total+=f->data;
         count++;
         f=f->next;
      }
      printf("\nAverage salary is: %d\n",total/count);
   }
};
int main(){
llist ls;
   int m;
   for(int i=0;i<5;i++){
       printf("Enter the value you want to add: ");
      scanf("%d",&m);
      ls.add(m);
   }
   ls.display();
   ls.average();
   getch();
}

Ans: This program consists of two functions other than the main function, one is display which displays every value in the link list and the other is average which uses a loop to calculate the sum and no of values in a link list and then display the average.
After execution, the program prompts the user to enter the value in the link list, once the user entered all the 5 values in link list, the ls.display and ls.average function will be called and the program displays sum and average.
Q4 [EE210 rocks as it’s too interesting: Refer to the following diagram and answer the questions]
Give the values of the following expressions
    1. ptr1->data    Ans: 22
    2. ptr2->next->data   Ans:75
    3. head->next->next->data  Ans: 34
Are the following expressions true or false
    1. head->next = = ptr1  True (
    2. ptr1->next->data= = 46 False
    3. ptr2->next = = NULL  False
Decide whether the syntax of each of the following statements is valid or invalid. If it is valid, mark it as such; if it is invalid, explain what is wrong.
    1. head->next = ptr1->next;  Valid
    2. ptr2 = ptr1->next->data;  Pointers should contain address information
Write one statement to do each of the following:
    1. Make head point to the node containing 34.  head->next->next->data;
    2. Make ptr2 point to the last node in the list.          Ptr->next
    3. Make head point to an empty list.            head->next= NULL;
    4. Set the data member of the node containing 34 to 45. Ptr1->next->data=45;
Q5 [Searching in a linked list: Is a given element member of the given linked list?]        (Hint: Q3 might be helpful)

Write a function/algorithm to find a value in a singly linked list of integers. Function arguments are two, the pointer to the head node and the integer to be searched.
void search(int start, int find){
       Node *f=start;
      while(f!=NULL){
        if (f==find)
              printf("The Number Has been Found”);
else  f=f->next;

       }

No comments:

Post a Comment