Monday 14 March 2011

FAQ'S

1. can we convert all c++ program to c program with less logical deviation?
Depends
cpp is just like c with advanced (OOPS) features........
Still it depends on the complexity of the program ! If we are using many advanced concepts of cpp in the code like inheritance polymorphism and most importantly virtual functions then it may affect our logic at a good extent ..........
But if the program is a simple one and straight forward then there will not be any big changes in the logic !

2.
Write a program to find prime numbers using pointers2)Write a program to check armstrong numbers using pointers
ans:

3. Explain following program In the below program ,in function add() node * q is local but it acts as static ie I mean it works correctly how is it possible?

#include<iostream.h>
#include<conio.h>
class demo
{

   public:
           struct node

          {
             int data;
             node *link;
             }*p;

           demo()
          {
             p=NULL;

           }

         int add(int x)
        {
          node *q,*r;

         if(p==NULL)
        { 

           r=new node;
           r->data=x;
           r->link=NULL;
           q=r;
           p=q;
        }
       else
      {
         r=new node;
         r->data=x;
         r->link=NULL;
        q->link=r;
        q=q->link;
        }

       return(0);
    }

void display()
{
   node *q;
   q=p;
 while(q!=NULL)
 {
   cout<data<<"t";
   q=q->link;
  }
  }
};

void main()
{
demo obj;

clrscr();
obj.add(3);
obj.add(5);
obj.add(4);
obj.add(6);
obj.add(7);
obj.display();

getch()
}


3.What is static array? and how it differs from char *ptr?

 ans: 


A static array is an array with fixed number of dimentions and each dimention will have fixed length. You can not change the dimentions of the Static array during runtime.ex: int arr[10][20] It has Two dimentions. Each fixed one to 10 other to 20.Well it differs from (char *) in mutiple ways. A startic array can be of any datatype and it will hold data not just address. To access the dataelemnets in the static array we use array index which in turn is a pointer. For example if I say a[2][3] I am reffering to the location (2 3) in the two dimention array. Now the dimention could be 3 or 4 or more. In all the cases we can retrieve information by array index.On the other hand (char *) is just a pointer to a character(Of datatype char). It has got nothing to do with arrays
4. How does the 3-D array works?
A 3-D array can be considered as an array of a 2-Dimensional array and thats very simple to understand. Consider an example ,say we have an array declaration int A[2][3][2],thats a 3-D array.We need to develop a decoding scheme to interpret the meaning of such declarations.
Read it like this: int (A[2])[3][2] (Note the braces) .'A' is an array of 2 elements and these 2 elements are not simple integers as it would have been in the case if 'A' was a 1-D array.Here ,the two elements that 'A' is made up of a 2-D array of dimensions 3*2 .i.e A 3-D array in this case is nothing but a collection of two 2-D arrays in our example.
in other words ,we say that 'A' is made of 2 pages ,3 rows and 2 columns each,in all we have total=2*3*2=12 elements in our array
but this is just a logical explanation ,physically an array be it any dimensional ,is always stored sequentially as memory is Linear

let us take an example
main()
{
int a[2][2][2];
int i j k;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
{
printf( i= dnj= dnk= dnnn i j k);
printf( enter the value to store at the specified postionn );
scanf( d a[i][j][k]);
}
getch();
}
if u compile and execute the above prg it shows the values of the variables i j k and where it stores. by this u can make that how the three dimensional array works





 

No comments:

Post a Comment