Wednesday, December 5, 2012

Solution to Programing Exercise pointers

                                                    Solution to Programming Exercise Pointers
Guys i think the starting two programs  are quite easy and thus I am not providing the solutions to them
Ans 11.3

#include<stdio.h>
#include<conio.h>
int size;
void main()
{
clrscr();
void insert( int *ptr , int val);
int arr[10] , in , i ;
printf("\nEnter the size of the array:\t");
scanf("%d" , &size);
printf("\nEnter your sorted array:\t");
for( i = 0 ; i <  size ;  i++)
scanf("%d", &arr[i]);
printf("\nEnter the number you want to insert in the array:\t");
scanf("%d", &in);
insert(arr, in);
printf("\nThe array after insertion is:\t");
for ( i = 0 ; i < size ; i++ )
printf("%d " , arr[i] ) ;
getch();
}

void insert( int  *ptr , int val)
{ int pos , i ;
if ( val < *(ptr+0) )
pos = 0 ;
else
{ for( i = 0 ; i < size - 1 ; i++ )
{ if ( *(ptr+i) <= val && val < *(ptr+i+1) )
{ pos = i+1;
break;
}
}
if ( i == size-1 )
pos = size;
}
if ( size == 10)
printf("\nOverflow!");
for ( i = size ; i > pos ; i-- )
*(ptr + i) = *(ptr + (i - 1) ) ;
*(ptr + pos ) = val;
size++;
}

Ans 11.4

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int * add ( int *a[] , int *b[] );
int i , j , p[3][3] , q[3][3] , r[3][3];
printf("\nEnter a 3*3 Matrix:\n");
for( i = 0 ; i < 3 ; i++ )
for( j = 0 ; j < 3 ; j++ )
scanf("%d", p[i][j] ) ;
printf("\nEnter the second 3*3 Matrix;\n");
for( i =0 ; i < 3 ; i++)
for( j = 0 ; j < 3 ; j++ )
scanf( " %d " , q[i][j]  );
r = add( p , q );
printf("\nThe sum is:\t");
for ( i =0 ; i< 3 ; i++ )
for( j = 0 ; j < 3 ; j++ )
scanf( "%d" , r[i][j] );
getch();
}

int * add ( int *a[3] ,int *b[3] )
{
int  i , j , c[3][3] ;
for( i = 0 ; i < 3 ; i++ )
for( j = 0 ; j < 3 ; j++ )
c[i][j] = *( *(a + i ) + j )  + *( *(b + i ) + j );
       return c;
}

Ans 11.5

#include<stdio.h>
#include<conio.h>
void del ( char *ptr , char itm)
{
int i , pos , size;
for ( i = 0 , size = 0 ; * (ptr + i) != '\0' ; i++ , size++)
{
if( *(ptr + i ) == itm)
pos = i ;
}
for( i = pos ; i < size ; i++ )
*(ptr +i) = *(ptr+i+1);
*(ptr+size) = '\0';

}
void main()
{
clrscr();
char str[10] ,ch;
printf("\nEnter your string:\t");
gets(str);
printf("\nEnter the charcter:\t");
scanf("%c" , &ch);
del(str, ch ) ;
printf("\nYour string after deletion:\t%s", str);
getch();
}

Ans 11.6
#include<stdio.h>
#include<conio.h>
#include<iostream.h>

void main()
{
char * day_name( int no);
clrscr();
int n;
char * day;
printf("\nEnter the number of the day:\t");
scanf("%d" , &n ) ;
day = day_name( n );
printf("\nThe day is:\t%s" , day);
getch();
}
char * day_name ( int no )
{
static char * week_day[7] = { "Monday" , "Tuesday" , "Wednesday", "Thursday" , "Friday" , "Saturday", "Sunday"};
int i , pos = 0;
for ( i = 0 ; i < 7 ; i++ )
{
if( ( i+1 ) == no )
{ pos = i;
break ;
}
}
return week_day[pos];
}

Ans 11.7
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *name[5] ;
int i ;
void sort ( char  *str[] );
printf("\nEnter any five names:\t");
for ( i = 0 ; i < 5 ; i++ )
gets(name[i]);
sort ( name ) ;
printf("\nThe names in the alphabetical order are:\n");
for ( i = 4 ; i >= 0 ; i-- )
puts(name[i]);
getch();
}

void sort ( char *str[5] )
{
int i , j ;
char *tmp ;
for ( i = 0 ; i < 5 ; i++ )
{ for ( j = 0 ; j < (5-1) - i ; j++ )
{ if ( strcmp( str[i+1] , str[i] )  > 0 )
{ tmp = str[i];
str[i] = str[i+1];
str[i+1] = tmp;
}
}
}
}

Ans 11.9#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int a[5];
void reverse( int *p ) ;
printf("\nEnter the array:\t");
for (  i = 0 ; i <  5 ; i ++ )
sacnf("%d" , a[i] ) ;
reverse(a);
printf("\nThe reversed array is:\t");
for ( i = 0 i < 5 ; i++)
printf("%d" , a[i] );
getch();

}

void reverse ( int * p )
{
int i , tmp ;
for ( i = 0 , j = 4  ; i < 5/2 ; i++ ,  j-- )
{ tmp = *(p+i);
*(p+i) = *(p+j);
*(p+j) = tmp;
}
}

Ans 11.10
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int a[5] , b[5] , i , res;
int cmp(int *p , int *q );
printf("\nEnter the array:\t");
for ( i = 0 ; i < 5 ; i++ )
scanf("%d" , &a[i] ) ;
printf("\nEnter the second array:\t");
for ( i = 0 ; i < 5 ; i++ )
scanf( "%d" , &b[i]);
if ( cmp( a , b) == 1)
printf("\nThey are identical");
else
printf("\nThey are not identical");
getch();
}

int cmp ( int *p , int *q)
{
int  i , flag = 1;
for ( i = 0 ; i < 5 ; i++ )
{ if( *(p+i) != *(q+i) )
{ flag = 0;
}
}
return flag;
}



1 comment: