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)

Zoho Puzzle Questions With Answers

Measuring Time Logic Puzzle You are given with two ropes with variable width. However if we start burning both the ropes, they will burn at exactly same time i.e. an hour. The ropes are non-homogeneous in nature. You are asked to measure 45 minutes by using these two ropes.

How can you do it?

Please note that you can’t break the rope in half as it is being clearly stated that the ropes are non-homogeneous in nature.
Answer & Explanation Solution: 45 minutes

Explanation :
All you have to do is burn the first rope from both the ends and the second rope from one end only simultaneously. The first rope will burn in 30 minutes (half of an hour since we burned from both sides) while the other rope would have burnt half. At this moment, light the second rope from the other end as well. Where, the second rope would have taken half an hour more to burn completely, it will take just 15 minutes as we have lit it from the other end too.

Thus you have successfully calculated 30+15 = 45 minutes …

Hackerrank > SQL > Basic Select

Select
01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
SELECT*FROM city;
---------------------------------------------------------------------------------
02-Select by ID
Given a City table, whose fields are described as