Skip to main content

Tricks and puzzle code of array



I am going to post some critical code and situation of program ,it may occur during working with array in c programming language

Q1)Output of the program.

#include<stdio.h>
#include<conio.h>
main()
{
int a[10];
printf("%u",a);
getch();
}

Ans:-It will print the base address of array.

Discussion:-The name of array contain the base address of the array.

Q2) Output of the program.

#include<stdio.h>
#include<conio.h>
main()
{
int a[5]={10};

printf("%d",a[0]);
printf("%d",0[a]);
printf("%d",*a);
printf("%d",*(&a[0]));
getch();
}

Ans:-10 10 10 10

Discussion:-In first statement normally print first array element.
In second statement, the notation is same as first printf notation.
In third statement, a[] contain the base address and * is called the value at address operator. So it will print value at first address of array.

Q3) Output of the program.


#include<stdio.h>
#include<conio.h>
main()
{
int a[5]={10};
printf("%d",a[2]);
getch();
}

Ans:-0

Discussion:-If we partially initialize a array then the remaining element will fill up automatically by zero.



Q4) Output of the program.


#include<stdio.h>
#include<conio.h>
main()
{
int a[]={10,20,30};

int *p[]={a,a+1,a+2};
int **q=&p[1];
printf("%d",**q);
getch();
}

Ans:-20

Discussion:- here p is a array of pointer. Here p[0] holding the first element’s address of array a, p[1] holding the 2nd element address of array a. And p[2] holding the 3rd element. And here q is a pointer of pointer. And q is holding the address of 2nd element of array. And we are printing value at the second element of array a.

Q5) Output of program.

#include<stdio.h>
#include<conio.h>
main()
{
const int b=5;
int a[b];
printf("%d",sizeof(a));
getch();
}

Ans:-Window 7 and dev c++ platform 20.
         Window Xp and TC platform 10;

Discussion:-If we declare a variable as constant then we can use the variable as array size like int a[b]; Here b define the size. But if we use a variable the the code is erroneous. Like int a[p]; Here p is a normal variable.


Q6)Output of the program.

#include<stdio.h>
#include<conio.h>
main()
{
int a[4][4];
printf("%d",sizeof(a));
getch();
}

Ans:-32

Discussion:-There are four row and four column in the 2d array. So total 16 integer element. And as each element takes 2 byte in xp ,TC environment. So the size of array is 32.

Q7)Output of the program.

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

char *p[]={"C","C++","Java"};
printf("%s",(p[1]));


getch();
}

Ans:-C++

Discussion:-Here p is a array of character pointer. p[0] hold the address of C string. P[1] hold the address of C++ string. p[2] hold the address of Java string.


Q8)Output of the program.

#include<stdio.h>
#include<conio.h>
void show(int *p)
{

for(int i=0;i<5;i++)
printf("%d ",*p++);

}
main()
{
int a[5]={10,20,30,40,50};
show(a);
getch();
}

Ans:-10 20 30 40 50.

Discussion:-Here base address of array is passing through show function. and p is a formal pointer parameter. and show function print the values of array.

Q9)Output of the program.


#include<stdio.h>
#include<conio.h>
main()
{
int a[][2]={{10,20},{30,40}};
printf("%d",*(*(a+1)+1));

getch();
}

Ans::-40

Discussion:-a contain the base address of 2d array. So it will print 40.

Q10)Work of program.

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

int a[5][5],i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
a[i][j]=a[j][i];

getch();
}

Ans:-It will make the matrix a , symmetric.

Q11)Output of the program.


#include<stdio.h>
#include<conio.h>
main()
{
static int a[]={1,2,3};
printf("%d",2[a]+a[2]);
getch();
}

Ans:-6

Discussion:-2[a]=3,and a[2]=3; So it will print 6.

Q12)

#include<stdio.h>
#include<conio.h>
main()
{
char a[]="abc";
char *p="abc";

puts(a);
puts(p);

getch();
}

Ans:-abc abc

Discussion:-here a is a character array and a hold the base address. And p is a pointer of array. As puts() function takes an address as argument. Then abc will print twice.



Q13)The last element of the array.


#include<stdio.h>
#include<conio.h>
main()
{
int a[10]={3,3,3};
getch();
}


Ans:-a[9]

Discussion:-There are 10 element

Q14)Output of the program.

#include<stdio.h>
#include<conio.h>
main()
{
static char hello[]=NULL;
printf("%s",hello);
getch();
}

Ans:- Syntax error.

Discussion:-We cannot initialize character array by NULL, Only in case of pointer assignment NULL assignment is possible.


Q15)Result of the program.


#include<stdio.h>
#include<conio.h>
main()
{
int a[3]={10,20,30};
for(int i=0;i<3;i++)
printf("%d", *(a+i));
getch();
}

Ans:-10 20 30

Discussion:-a contain the base address of array. And for each iteration of loop the value of i will increment 1.But the value of a will actually increment by 2(In TC compiler ,because the scale factor of integer is 2.)so *(a +2) ,ie it will print the next value of a array.

Comments

Popular posts from this blog

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

C Questions

C Questions
C Questions

Note : All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

void main()
{
int const * p=5; printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
main()
{
char s[ ]="man"; int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm
aaaa nnnn
Explanation

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…