Sunday, 6 April 2014

Arrays

Sorting an array by using strcmp


#include<stdio.h>
#include<string.h>
#include<string.h>
main()
{
char a[4][10],temp[10];
int i,j,k;
printf("enter 4 strings\n");
for(i=0;i<4;i++)
scanf("%s",a[i]);
for(i=0;i<4;i++)
for(j=i+1;j<4;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
printf("sorted array is:\n");
for(i=0;i<4;i++)
printf(".....%s....\n",a[i]);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi arr_sort.c
madan@madan-Lenovo-G570:~/madan$ cc arr_sort.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter 4 strings
welcome
madan
tech
world
sorted array is:
.....madan....
.....tech....
.....welcome....
.....world....
Sorting an array without using strcmp

#include<stdio.h>
#include<string.h>
main()
{
int a[5],i,j,n,temp;
n=sizeof(a)/sizeof(a[0]);
printf("enter an array of 5 elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array before sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("array after sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
OutPut:
enter an array of 5 elements:
43
32
33
12
22
array before sorting
43 32 33 12 22 array after sorting
12 22 32 33 43
Sorting a 2D-array

#include<stdio.h>
#include<string.h>
main()
{
int a[3][3],i,j,k,n,temp;
n=sizeof(a)/sizeof(a[0]);
printf("enter 2D array....\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf(" %d",&a[i][j]);
//printf("\n");
printf("array before sorting\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf(" %d ",a[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=j+1;k<n;k++)
{
if(a[i][j]>a[i][k])
{
temp=a[i][j];
a[i][j]=a[i][k];
a[i][k]=temp;
}
}
printf("array after sorting\n");
for(i=0;i<n;i++)
{
for(k=0;k<n;k++)
printf("%d ",a[i][k]);
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi sort_2D_Array.c
madan@madan-Lenovo-G570:~/madan$ cc sort_2D_Array.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter 2D array(3x3)....
9
8
7
6
5
4
3
2
1
array before sorting
 9  8  7 
 6  5  4 
 3  2  1 
array after sorting
7 8 9 
4 5 6 
1 2 3
Program to pass 1D_Array using malloc function
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void array1d(int **ptr,int m,int n)
{
int i,j;
printf("The resultant array is..\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",ptr[i][j]);
}
printf("\n");
}
}
main()
{
int **a,rows,cols,i,j;
printf("entyer size of 2d array...\n");
scanf("%d%d",&rows,&cols);
a=malloc(rows*sizeof(int*));
for(i=0;i<rows;i++)
a[i]=malloc(cols*sizeof(int));
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
array1d(a,rows,cols);
}
OutPut:
entyer size of 2d array...
3
3
9
8
7
6
5
4
3
2
1
The resultant array is..
 9  8  7 
 6  5  4 
 3  2  1
program to find memory locations of array elements
#include<stdio.h>
main()
{
int a[5]={12,32,22,14,54},i,n;
int bit,num;
n=sizeof(a)/sizeof(a[0]);
for(i=0;i<n;i++)
{
printf("a[%d]=%d at %u\n",i,a[i],&a[i]);
num=a[i];
bit=31;
while(bit>=0)
{
num&(1<<bit)?printf("1"):printf("0");
bit--;
}
printf("\n");
//printf("a[%d]=%d at %u\n",i,a[i],&a[i]);
}
}
OutPut:
a[0]=12 at 4080586848
00000000000000000000000000001100
a[1]=32 at 4080586852
00000000000000000000000000100000
a[2]=22 at 4080586856
00000000000000000000000000010110
a[3]=14 at 4080586860
00000000000000000000000000001110
a[4]=54 at 4080586864
00000000000000000000000000110110
program to find sum of two arrays
#include<stdio.h>
main()
{
int i,n;
int a[5]={12,23,42,11,12},b[5]={65,34,22,1,22},c[5]={0};
n=sizeof(a)/sizeof(a[0]);
//a[5]={12,23,42,11,12}
//b[4]={65,34,22,1,22}
for(i=0;i<n;i++)
{
c[i]=a[i]+b[i];
printf("a[%d]=%d b[%d]=%d c[%d]=%d\n",i,a[i],i,b[i],i,c[i]);
}
printf("\n");
}
OutPut:
adan@madan-Lenovo-G570:~/madan$ ./a.out
a[0]=12 b[0]=65 c[0]=77
a[1]=23 b[1]=34 c[1]=57
a[2]=42 b[2]=22 c[2]=64
a[3]=11 b[3]=1 c[3]=12
a[4]=12 b[4]=22 c[4]=34
program to print array elements with references
#include<stdio.h>
main()
{
int a[5]={1,2,3,4,5};
printf("%d\n",a[3]/a[1]);
printf("%d\n",a[6]);
printf("%d\n",a[-2]);
printf("%d\n",a[0]);
printf("%d\n",a[2]);
printf("%d\n",2[a]);
printf("%d\n",2[a]);
printf("%d\n",*(a+1));
printf("%d\n",*a+1);
}
OutPut:
2
0
4195718
1
3
3
3
2
2