## Friday, January 27, 2012

### Tricks and puzzle code of array

Basic understanding of array
Array is nothing but a set of homogeneous element. It come in two flavors: one dimensional and multi-dimensional arrays That starts indexing usually from 0(Some programming language treat 1 as the first element of array).It contains a series of contagious memory location. In c programming language array indexing starts from 0,And the name of array contain the base address of array. The base address may differ during each and every run of same program. The last index of array must be one less than the number of element, if the indexing starts from 0.Array never occupy space in fragmented memory area. For that reason helps to access sequentially by tracing the memory location . Array in c language can initialize in two types
1)Compile time initialization
2)Run time initialization.
The total size of a particular array in c language can be measure using this formula
(number of element* size of each element) = Total size
For example
The the number of element is 5 and the elements are integer type in window 32 bit operating system environment then the array size is
5*2 byte=10 byte.
And the 10 byte will allocate in system memory contagiously. One great advantage of array is ,it is possible to access randomly. Accessible from any index.
Types of array in c language
1)Array of integer number(Integer array)
2)Array of float number(Float array)
3)Array of charater(Often called string)
4)Array of Structure
5)Array of Union
6)Array of pointer
7)Array of arrays(2 dimension array or n dimension 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;
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={10};

printf("%d",a);
printf("%d",0[a]);
printf("%d",*a);
printf("%d",*(&a));
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={10};
printf("%d",a);
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;
printf("%d",**q);
getch();
}
Ans:-20
Discussion:- here p is a array of pointer. Here p holding the first element’s address of array a, p holding the 2nd element address of array a. And p 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;
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));

getch();
}
And:-C++
Discussion:-Here p is a array of character pointer. p hold the address of C string. P hold the address of C++ string. p 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={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[]={{10,20},{30,40}};
printf("%d",*(*(a+1)+1));

getch();
}
And:-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,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);
getch();
}
Ans:-6
Discussion:-2[a]=3,and a=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={3,3,3};
getch();
}
Ans:-a
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={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.