Let Us C / Chapter 7 (The C Pre-processor)

                              Exercise [C]


(a) Write down macro definitions for the following:
1. To test whether a character entered is a small case letter or not.
2. To test whether a character entered is a upper case letter or not.
3. To test whether a character is an alphabet or not. Make use of the macros you defined in (1) and (2) above.
4. To obtain the bigger of two numbers.

Solution:


#include<stdio.h>
#include<conio.h>
#define UPPER(x) (x>=65 && x<=90)
#define SMALL(y) (y>=97 && y<=123)
#define ALPHABET(z) (z>=65 && z<=90 || z>=97 && z<=123)
#define BIGGER(a,b) (a>b)

void main() {

int i,d1,d2;
char ch,ch1;
clrscr();

printf("\t enter your choice: \n");
printf("\t===================\n\n");

printf("1: to test if character is small case letter or not\n\n");
printf("2: to test if character is upper case letter or not\n\n");
printf("3: to test if character is an alphabet or not\n\n");
printf("4: to find bigger of two number\n\n\n");

printf("choice: ");
scanf("%d",&i);

switch (i) {

case 1:

clrscr();

printf("enter any character\n\n");

scanf(" %c",&ch);

if SMALL(ch)
printf("\n\n it is a small case letter.\n");
else
printf("\n\nit is not a small case letter.\n");

break;

case 2:

clrscr();

printf("enter any character\n\n");

scanf(" %c",&ch);

if UPPER(ch)
printf("\n\nit is an upper case letter.\n");
else
printf("\n\nit is not an upper case letter.\n");

break;

case 3:

clrscr();

printf("enter any character\n\n");

scanf(" %c",&ch);

if ALPHABET(ch)
printf("\n\nit is an alphabet.\n");

else
printf("\n\nit is not an alphabet.\n");

break;

case 4:

clrscr();

printf("enter two numbers\n\n");

scanf("%d%d",&d1,&d2);

if BIGGER(d1,d2)

printf("\n\n%d is bigger\n",d1);

else
printf("\n\n%d is bigger \n",d2);

break;

default:

printf("\n\n\nwrong choice entered\n");


}

getch();
}

--------------------------------------------------------------------

(b) Write macro definitions with arguments for calculation of area and perimeter of a triangle, a square and a circle. Store these macro definitions in a file called “areaperi.h”. Include this file in your program, and call the macro definitions for calculating area and perimeter for different squares, triangles and circles.

Note: write these macro definitions in a new file and save it as "areaperi.h", compile it and then you can include it during inclusion of libraries as - include"areaperi.h".


#define TP(a,b,c) (a+b+c)                                /* perimeter of triangle */
#define TA(a,b,c,d) (d*((d-a)*(d-b)*(d-c)))      /* area of triangle *
#define PS(x) (4*x)                                          /* perimeter of square */
#define SA(x) (x*x)                                           /* area of square */
#define CP(x) (2*3.14*r)                                 /* perimeter of circle */
#define CA(x) (3.14*r*r)                                  /* area of circle */


Solution:

#include"areaperi.h"        /* inclusion of custom header file */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() {

float t1,t2,t3,hpt;      /* sides of triangle,half of perimeter */
float ss,r;                 /* side of square, radius of circle */
int ch;                 
float tp,ta,ta1,sp,sa,cp,ca;       /* perimeter & area of triangle,square,circle */


clrscr();

printf("\tFind area and perimeter of: \n");
printf("       *****************************\n\n");

printf("1: triangle\n\n");
printf("2: square\n\n");
printf("3: circle\n\n\n\n\n");

printf("enter your choice:  ");
scanf("%d",&ch);

switch(ch)

{

case 1:

/* triangle */

     clrscr();
     printf("enter side 1 of triangle:  ");
     scanf("%f",&t1);

     printf("\n\nenter side 2 of triangle:  ");
     scanf("%f",&t2);

     printf("\n\nenter side 3 of triangle:  ");
     scanf("%f",&t3);

     hpt=t1+t2+t3/2;

     tp=TP(t1,t2,t3);
     ta=TA(t1,t2,t3,hpt);

     ta1=sqrt(ta);

     printf("\n\n\tperimeter of triangle = %f",tp);
     printf("\n\tarea of triangle = %f",ta1);

     break;

case 2:

       /* square */

     clrscr();

     printf("\n\nenter side of a square:  ");
     scanf("%f",&ss);

     sp=PS(ss);
     sa=SA(ss);

     printf("\n\n\tperimeter of square = %f",sp);
     printf("\n\tarea of square = %f",sa);

     break;

case 3:

      /* circle */

     clrscr();

     printf("\n\nenter radius of circle: ");
     scanf("%f",&r);

     cp=CP(r);
     ca=CA(r);

     printf("\n\n\tperimeter of circle = %f",cp);
     printf("\n\tarea of circle = %f",ca);

     break;


default:

       exit();
}

getch();

}



-----------------------------------------------------------------------

(c) Write down macro definitions for the following:
1. To find arithmetic mean of two numbers.
2. To find absolute value of a number.
3. To convert a uppercase alphabet to lowercase.
4. To obtain the bigger of two numbers.

Solution:



#include<stdio.h>
#include<conio.h>
#define AM(x,y) ((x+y)/2)
#define ABS(x,y) (x<y)
#define LOWER(x)(122-(90-x))
#define BIG(x,y) (x>y)


void main() {

char ch,ch1;
int choice,a,b,abs,c=0;
float am;

clrscr();

printf("\tMacro Definitions to:\n");
printf("     ***************************\n\n");

printf("\n1: Find airthmetic mean of two numbers: \n\n");
printf("2: find absolute value of a number: \n\n");
printf("3: convert an uppercase character to lowercase: \n\n");
printf("4: obtain the bigger of two numbers: \n\n\n\n\n\n\n\n\n\n");


printf("enter your choice:  ");
scanf("%d",&choice);


switch(choice) {

case 1:

      /* finding arithmetic mean of two numbers */

      clrscr();

      printf("\nenter two numbers:\n ");
      scanf("%d%d",&a,&b);
      am=AM(a,b);        /* airthmetic mean */
      printf("\nairthmetic mean = %f ",am);
      break;


case 2:

      /* finding the absolute value of a number */

      clrscr();

      printf("\nenter a number: ");
      scanf("%d",&a);

      if ABS(a,c)

      abs=a*(-1);

      else
      abs=a;

      printf("\nabsolute value = %d",abs);
      break;


case 3:

      /* converting an uppercase character to equivalent lowercase */

      clrscr();

      printf("\nenter an uppercase character: ");
      scanf(" %c",&ch);

      ch1=LOWER(ch);

      printf("\nequivalent lowercase character = %c",ch1);
      break;

case 4:

      /* finding the bigger of two numbers */

      clrscr();

      printf("\nenter two numbers:\n ");
      scanf("%d%d",&a,&b);

      if BIG(a,b)
      printf("\n\n%d is bigger.\n",a);

      else
      printf("\n\n%d is bigger.\n",b);

      break;

default:

       exit();
}


getch();

}

-----------------------------------------------------------------------

(d) Write macro definitions with arguments for calculation of Simple Interest and Amount. Store these macro definitions in a file called “interest.h”. Include this file in your program, and use the macro definitions for calculating simple interest and amount.

Note: write these macro definitions in a new file and save it as "interest.h", compile it and then you can include it during inclusion of libraries as - include"interest.h".

#define INTEREST(x,y,z) (x*y*z/100)
#define AMOUNT(x,y) (x+y)

Solution:


#include<stdio.h>
#include<conio.h>
#include"interest.h"      /* inclusion of custom header file */

void main() {

int p,r,t,a;
float si;

clrscr();

printf("enter the principle:  ");
scanf("%d",&p);

printf("\n\nenter the rate:  ");
scanf("%d",&r);

printf("\n\nenter the time:  ");
scanf("%d",&t);

si=INTEREST(p,r,t);

a=AMOUNT(p,si);

printf("\n\n\n\n\t\tsimple interest = %f\n\n\t\tamount = %d",si,a);

getch();
}

_______________________________________________________________________

Let Us C / Chapter 8 (Arrays)

                                 Exercise [D]


(a) Twenty-five numbers are entered from the keyboard into an array. The number to be searched is entered through the keyboard by the user. Write a program to find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array.

Solution:



#include<stdio.h>
#include<conio.h>

void main() {

int i,arr[25],prsnt=0,num;
clrscr();

printf("Please enter 25 numbers: \n");

for(i=0;i<25;i++) {
scanf("%d",&arr[i]);
}

printf("\n\nPlease enter the number to be searched: ");
scanf("%d",&num);


for(i=0;i<25;i++) {

if(num==arr[i])
prsnt=prsnt+1;

}


if(prsnt==0) {
printf("\n\nNumber does not present in the array.\n");
}

else {
printf("\n\nNumber presents in the array.\n");
printf("\nNumber of times it appears = %d.\n",prsnt);
}

getch();

}

---------------------------------------------------------------------------------------------------------------

b) Twenty-five numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive, how many are negative, how many are even and how many odd.

Solution:


#include<stdio.h>
#include<conio.h>

void main() {

int i,arr[25],tz=0,tp=0,tn=0;
clrscr();

printf("Enter numbers in the array: \n");


for(i=0;i<25;i++) {
scanf("%d",&arr[i]);

if(arr[i]<0){
tn=tn+1;
}

if(arr[i]==0){
tz=tz+1;
}

if(arr[i]>0){
tp=tp+1;
}

}


printf("\n\n\nTotal zeros = %d\n",tz);
printf("Total positive numbers = %d\n",tp);
printf("Total negative numbers = %d\n",tn);

getch();

}

----------------------------------------------------------------------------------------------------------------

(c) Implement the Selection Sort, Bubble Sort and Insertion sort algorithms on a set of 25 numbers. (Refer Figure 8.11 for the logic of the algorithms)
− Selection sort
− Bubble Sort
− Insertion Sort

Solution:



----------------------------------------------------------------------------------------------------------------

(d) Implement the following procedure to generate prime numbers from 1 to 100 into a program. This procedure is called sieve of Eratosthenes.
step 1
Fill an array num[100] with numbers from 1 to 100
step 2
Starting with the second entry in the array, set all its multiples to zero.
step 3
Proceed to the next non-zero element and set all its multiples to zero.
step 4
Repeat step 3 till you have set up the multiples of all the non-zero elements to zero
step 5
At the conclusion of step 4, all the non-zero entries left in the array would be prime numbers, so print out these numbers.

Solution:


#include<stdio.h>
#include<conio.h>

void main() {

int i,j,a[100];
clrscr();

for(i=0;i<100;i++) {

a[i]=i+1;

}

printf("\n100 numbers in the array:\n\n");

for(i=0;i<100;i++) {

printf("%3d ",a[i]);

}

printf("\n\nafter implementing eratothene's sieve:\n\n");

for(i=2;i<100;i++) {

for(j=2;j<a[i];j++) {

if(a[i]%j==0)
a[i]=0;
}
}

i=a[0];
for(;i<100;i++) {


printf("%3d ",a[i]);

}

printf("\n\nprime numbers are: \n\n");

for(i=a[0];i<100;i++) {

if(a[i]!=0)

printf("%3d ",a[i]);

}

getch();
}


---------------------------------------------------------------------------------------------------------------

                                Exercise [I]


(a) Write a program to copy the contents of one array into another in the reverse order.

Solution:



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

int i,j,k,a1[5],a2[5];
clrscr();


for(i=1;i<=5;i++) {
scanf("%d",&a1[i]);
}

printf("\n\nThe elements you enterd are:\n");

for(i=1;i<=5;i++) {
printf(" %d",a1[i]);
}

printf("\n\nElements in reversed order:\n");

for(i=5,j=1;i>=1,j<=5;i--,j++) {

k=a1[i];
a2[j]=k;

printf(" %d",a2[j]);
}

getch();
}


---------------------------------------------------------------------------------------------------------------

(b) If an array arr contains n elements, then write a program to check if arr[0] = arr[n-1], arr[1] = arr[n-2] and so on.

Solution:


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

int arr[100];
int n,i,f=0;
clrscr();

printf("enter total elements of array(n):  ");
scanf("%d",&n);

printf("\n\nenter \"n\" elements of array: \n\n");

for(i=0;i<n;i++) {

scanf("%d",&arr[i]);

}

clrscr();

for(i=0;i<n;i++) {

if(arr[i]==arr[n-(i+1)]) {           /* if element is equal, according to the problem,
                         it will be printed */
f=f+1;

printf("element no: %d = %d ",i,arr[i]);
}

}

if(f==0)
printf("\n\nNo such element found.\n");

getch();

}


---------------------------------------------------------------------------------------------------------------

(c) Find the smallest number in an array using pointers.

Solution:



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

int i,n,*p,*s,a[100];
clrscr();

printf("enter how many numbers you want to save in array: ");
scanf("%d",&n);

printf("\n\nenter %d number in array:\n",n);

for(i=0;i<n;i++) {

scanf("%d",&a[i]);

}

clrscr();

printf("array you entered: \n\n");

for(i=0;i<n;i++) {

printf("%2d ",a[i]);

}

printf("\n\n");

p=&a[0];             /* first pointer points 0th element */

for(i=0;i<n;i++) {

s=&a[i];            /* second pointer points every element one by one */

if(*p>*s)
    /* if first is bigger than second */
*p=*s;               /* first becomes second */

s++;                 /* second is incremented to check with other elements */


}

printf("smallest digit in array is %d\n",*p);

getch();
}


----------------------------------------------------------------------------------------------------------------

(d) Write a program which performs the following tasks:
− initialize an integer array of 10 elements in main( )
− pass the entire array to a function modify( )
− in modify( ) multiply each element of array by 3
− return the control to main( ) and print the new array elements in main( )

Solution:



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


int i,j,a[10]={1,2,3,4,5,6,7,8,9,10};
modify();
clrscr();

printf("array before modification: \n\n");

for(i=0;i<10;i++) {

printf(" %d ",a[i]);

}

modify(a);    /* passing only the name of array */

printf("\n\n\narray after modification:\n\n");

for(i=0;i<10;i++) {     /* printing the array in main(); */

printf(" %d ",a[i]);

}

getch();

}

modify(int b[10]) {

int c;

for(c=0;c<10;c++) {

b[c]=b[c]*3;   /* multiplying each element with 3 */

}
return b[c];  /* returning the whole array to main(); */
}

----------------------------------------------------------------------------------------------------------

(e) The screen is divided into 25 rows and 80 columns. The characters that are displayed on the screen are stored in a special memory called VDU memory (not to be confused with ordinary memory). Each character displayed on the screen occupies two bytes in VDU memory. The first of these bytes contains the ASCII value of the character being displayed, whereas, the second byte contains the colour in which the character is displayed.
For example, the ASCII value of the character present on zeroth row and zeroth column on the screen is stored at location number 0xB8000000. Therefore the colour of this character would be present at location number 0xB8000000 + 1. Similarly ASCII value of character in row 0, col 1 will be at location 0xB8000000 + 2, and its colour at 0xB8000000 + 3.
With this knowledge write a program which when executed would keep converting every capital letter on the screen to small case letter and every small case letter to capital letter. The procedure should stop the moment the user hits a key from the keyboard.
This is an activity of a rampant Virus called Dancing Dolls. (For monochrome adapter, use 0xB0000000 instead of 0xB8000000).

Solution:


_______________________________________________________________________

                          Exercise [L]


(a) How will you initialize a three-dimensional array threed[3][2][3]? 
How will you refer the first and last element in this array?

Solution:


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

/* initialization of a 3 dimensional array */

int threed[3][2][3]={
     {
      {100,2,3},
      {1,2,3}
     },
      {
     {8,5,6},
     {4,5,6}
     },
      {
     {7,8,9},
     {7,8,200}
     }
     };
int *f,*l;
clrscr();


f=&threed[0][0][0];      /* reference to first element */

l=&threed[2][1][2];      /* reference to second element */


printf("\n\nfirst element = %d",*f);
printf("\n\nlast element = %d",*l);

getch();

}


---------------------------------------------------------------------------------------------------------------
(b) Write a program to pick up the largest number from any 5 row by 5 column matrix.

Solution:


#include<stdio.h>
#include<conio.h>

void main() {

int i,j,a[5][5];
clrscr();

printf("\nType the numbers to in matrix:\n");

for(i=0;i<5;i++) {

for(j=0;j<5;j++) {

scanf("%d",&a[i][j]);

}
 }

clrscr();

printf("matrix you entered is:\n\n");

for(i=0;i<5;i++) {

for(j=0;j<5;j++) {

printf(" %2d ",a[i][j]);

}

printf("\n");

}

/* finding the largest number */

for(i=0;i<5;i++) {

for(j=0;j<5;j++) {

if(a[0][0]<a[i][j])      /* if number is larger than first element */

a[0][0]=a[i][j];          /* larger number is placed as the first element */


}

}



printf("\n\nThe largest number in matrix is: %d",a[0][0]);

getch();

}

---------------------------------------------------------------------------------------------------------------

(c) Write a program to obtain transpose of a 4 x 4 matrix. The transpose of a matrix is obtained by exchanging the elements of each row with the elements of the corresponding column.

Solution:


#include<stdio.h>
#include<conio.h>
#define MAX 4

void main() {

int i,j,a[4][4],b[4][4];
clrscr();

printf("\nenter numbers in 5x5 matrix: \n\n");

for(i=0;i<MAX;i++) {

for(j=0;j<MAX;j++) {

scanf("%d",&a[i][j]);

}

}

clrscr();

printf("\nmatrix you entered is: \n\n");

for(i=0;i<MAX;i++) {

for(j=0;j<MAX;j++) {

printf("%2d ",a[i][j]);

}
printf("\n");
}

/* transpose of matrix */

for(i=0;i<MAX;i++) {

for(j=0;j<MAX;j++) {

b[j][i]=a[i][j];

}
}

printf("\n\n");

/* printing the transpose */

printf("Transpose of matrix is: \n\n");

for(i=0;i<MAX;i++) {

for(j=0;j<MAX;j++) {

printf("%2d ",b[i][j]);

}
printf("\n");
}

getch();
}


----------------------------------------------------------------------------------------------------------------

(d) Very often in fairs we come across a puzzle that contains 15 numbered square pieces mounted on a frame. These pieces can be moved horizontally or vertically. A possible arrangement of these pieces is shown below:

 1    4   15    7
 8   10    2   11
14    3    6   13
12    9    5  

As you can see there is a blank at bottom right corner. Implement the following procedure through a program:

Draw the boxes as shown above. Display the numbers in the above order. Allow the user to hit any of the arrow keys (up, down, left, or right).
If user hits say, right arrow key then the piece with a number 5 should move to the right and blank should replace the original position of 5. Similarly, if down arrow key is hit, then 13 should move down and blank should replace the original position of 13. If left arrow key or up arrow key is hit then no action should be taken.
The user would continue hitting the arrow keys till the numbers aren’t arranged in ascending order.
Keep track of the number of moves in which the user manages to arrange the numbers in ascending order. The user who manages it in minimum number of moves is the one who wins.
How do we tackle the arrow keys? We cannot receive them using scanf( ) function. Arrow keys are special keys which are identified by their ‘scan codes’. Use the following function in your program. It would return the scan code of the arrow key being hit. Don’t worry about how this function is written. We are going to deal with it later. The scan codes for the arrow keys are:
up arrow key – 72 down arrow key – 80 left arrow key – 75 right arrow key – 77
/* Returns scan code of the key that has been hit */ 
#include "dos.h" 
getkey( ) 
union REGS i, o ;
while ( !kbhit( ) ) ;
 i.h.ah = 0 ; 
int86 ( 22, &i, &o ) ;
 return ( o.h.ah ) ;
 }

Solution:


#include<stdio.h>
#include<conio.h>
#include<dos.h>

/* returns scan code of the key that has been hit */
getkey()
{
union REGS i,o;
while(!kbhit() )
;
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}


void main() {

int i,j,a[16]={1,4,15,7,8,10,2,11,14,3,6,13,12,9,5,0};
int temp,h,moves=0,won=0;

clrscr();


/****************************************************/


do {
 /**************/
 /* to move up */
 /**************/

if(h==72) {


for(i=0;i<16;i++) {
if(a[i]==0){
if(a[0]==0 || a[1]==0 || a[2]==0 || a[3]==0) {
break;
}
temp=a[i];
a[i]=a[i-4];
a[i-4]=temp;
moves=moves+1;
break;
}
}
 }
 /****************/
 /* to move left */
 /****************/

if(h==75) {

for(i=0;i<16;i++) {

if(a[i]==0){
if(a[0]==0 || a[4]==0 || a[8]==0 || a[12]==0) {
break;
}
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
moves=moves+1;
break;
}
}
 }

 /****************/
 /* to move down */
 /****************/

if(h==80) {
for(i=0;i<16;i++) {
if(a[i]==0){
if(a[12]==0 || a[13]==0 || a[14]==0 || a[15]==0) {
break;
}
temp=a[i];
a[i]=a[i+4];
a[i+4]=temp;
moves=moves+1;
break;
}
}
 }
/*****************/
/* to move right */
/*****************/

if(h==77) {

for(i=0;i<16;i++) {

if(a[i]==0) {

if(a[3]==0 || a[7]==0 || a[11]==0 || a[15]==0 ) {
break;
}

temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
moves=moves+1;
break;
}
 }
  }

/***********************************************************/

       /**********************************/
       /* printing the puzzle with boxes */
       /**********************************/

printf("\n%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",218,196,196,196,194,196,196,196,194,196,196,196,194,196,196,196,191);
for(i=0;i<=15;i++) {

printf("%c",179);

if(a[i]==0) {
printf("%c  ",32);     /* printing a blank space in the puzzle */
}
if(a[i]!=0)

printf(" %2d",a[i]);

if(a[i]==a[3] || a[i]==a[7] || a[i]==a[11] || a[i]==a[15])
printf("%c",179);

if(i==3||i==7||i==11)
printf("\n%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",195,196,196,196,197,196,196,196,197,196,196,196,197,196,196,196,180);

if(a[0]==1 && a[1]==2 && a[2]==3 && a[3]==4 && a[4]==5 && a[5]==6
&&a[6]==7 && a[7]==8 && a[8]==9 && a[9]==10 && a[11]==12 && a[12]==13
&& a[13]==14 && a[14]==15 && a[15]==0 ) {

won=1;
}

 }
printf("\n%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",192,196,196,196,193,196,196,196,193,196,196,196,193,196,196,196,217);

/***************************************************/
if(won==1) {
printf("\n\n\tCongratulations! you have won.");
break;
}
      /**********************************/
      /* to print instructions for user */
      /**********************************/

printf("\n\n\n\n\n\n  Total Moves: %d\t\t\t\t  Use arrow keys to move blank:\n\n",moves);
printf("\t\t\t\t\t\t  %c to move up\n",30);
printf("\t\t\t\t\t\t  %c to move down\n",31);
printf("\t\t\t\t\t\t  %c to move left\n",17);
printf("\t\t\t\t\t\t  %c to move right\n",16);

/****************************************************/

     /**********************/
     /* to take user input */
     /**********************/

h=getkey();
clrscr();

/****************************************************/

}while(h==72 || h==75 || h==77 ||h==80);


getch();

}


-----------------------------------------------------------------------------------------------------------------

(e) Those readers who are from an Engineering/Science background may try writing programs for following problems.
(1) Write a program to add two 6 x 6 matrices.
(2) Write a program to multiply any two 3 x 3 matrices.
(3) Write a program to sort all the elements of a 4 x 4 matrix.
(4) Write a program to obtain the determinant value of a 5 x 5 matrix.

Solution:


--------------------------------------------------------------------------------------------------------------

(j) Write a program that interchanges the odd and even components of an array.

Solution:



#include<stdio.h>
#include<conio.h>

void main() {

int i,j,a[6],even,temp;
clrscr();

printf("enter the numbers: \n\n");

for(i=0;i<6;i++) {

scanf("%d",&a[i]);

}

clrscr();

printf("\narray without exchanging even and odd numbers:\n\n");

for(i=0;i<6;i++) {

printf("%2d ",a[i]);

}

printf("\n\narray after exchanging even and odd numbers: \n\n");

for(i=0;i<6;i++) {


for(j=i+1;j<6;j++) {

/* if one element is even and another after that is odd \
,they will be exchanged */

if((a[i]%2)!=0 && (a[j]%2)==0) {

temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}

for(i=0;i<6;i++) {

printf("%2d ",a[i]);

}

getch();
}

--------------------------------------------------------------------------------------------------------------

(k) Write a program to find if a square matrix is symmetric.

Solution:


#include<stdio.h>
#include<conio.h>

void main() {

int i,j,r,c,sym;
int a[100][100],b[100][100];


clrscr();

printf(" enter number of rows of matrix: ");
scanf("%d",&r);

printf("\n\nenter number of columns of matrix: ");
scanf("%d",&c);

clrscr();

printf("enter the elements in matrix: \n");

for(i=0;i<r;i++) {

for(j=0;j<c;j++) {

scanf("%d",&a[i][j]);
}

}

clrscr();

printf("\nmatrix you entered is\n\n");

for(i=0;i<r;i++) {

for(j=0;j<c;j++) {

printf("%d ",a[i][j]);

}
printf("\n");
}

printf("\n\ntranspose of matrix is \n\n");

for(i=0;i<r;i++) {

for(j=0;j<c;j++) {

b[j][i]=a[i][j];

}
}

for(i=0;i<r;i++) {

for(j=0;j<c;j++) {

printf("%d ",b[i][j]);

}
printf("\n");
}

/* finding if square matrix is equal to it's transpose to be symmetric */

for(i=0;i<r;i++) {

for(j=0;j<c;j++) {

if(a[i][j]!=b[i][j])

sym=1;


}

}

if(sym==1)
printf("\nSquare matrix is not symmetric.\n");

else
printf("\nSquare matrix is symmetric.\n");

getch();

}

----------------------------------------------------------------------------------------------------------------

(l) Write a function to find the norm of a matrix. The norm is defined as the square root of the sum of squares of all elements in the matrix.

Solution:


#include<stdio.h>
#include<conio.h>
#include<math.h>

void main() {

int i,j,r,c,a[100][100];
int norm=0,sum=0;

clrscr();

printf("\nEnter the number of rows: ");
scanf("%d",&r);

printf("\n\nEnter the number of coloumns: ");
scanf("%d",&c);

clrscr();

printf("Enter elements of %d x %d array: \n\n",r,c);

for(i=0;i<r;i++) {

for(j=0;j<c;j++) {

scanf("%d",&a[i][j]);

}
}

clrscr();

printf("\nmatrix you entered is: \n\n");

for(i=0;i<r;i++) {

for(j=0;j<c;j++) {

printf("%2d ",a[i][j]);

}
printf("\n");
}

/* norm of the matrix */

for(i=0;i<r;i++) {

for(j=0;j<c;j++) {

sum=sum+(a[i][j]*a[i][j]);

}

}

norm=sqrt(sum);

printf("\nNorm of matrix = %d",norm);

getch();
}

----------------------------------------------------------------------------------------------------------------

(m) Given an array p[5], write a function to shift it circularly left by two positions. Thus, if p[0] = 15, p[1]= 30, p[2] = 28, p[3]= 19 and p[4] = 61 then after the shift p[0] = 28, p[1] = 19, p[2] = 61, p[3] = 15 and p[4] = 30. Call this function for a (4 x 5 ) matrix and get its rows left shifted.

Solution:



#include<stdio.h>
#include<conio.h>

void main() {

int i,j,p[]={15,30,28,19,61};
int a[4][5];

clrscr();

printf("Array before shift:\n\n");
for(i=0;i<5;i++) {

printf("%2d ",p[i]);

}

func(p);

printf("\n\nArray after shift:\n\n");

for(i=0;i<5;i++) {

printf("%2d ",p[i]);

}

printf("\n\n\nenter the elements of 4x5 matrix: \n\n");

for(i=0;i<4;i++) {

for(j=0;j<5;j++) {

scanf("%d",&a[i][j]);

}

}

clrscr();

printf("matrix you enterd before shift: \n\n");

for(i=0;i<4;i++) {

for(j=0;j<5;j++) {

printf("%2d ",a[i][j]);

}

printf("\n");
}

printf("\n\nafter shift:\n\n");

/* shift the rows of matrix */


for(i=0;i<4;i++) {

func(a[i]);

}

for(i=0;i<4;i++) {

for(j=0;j<5;j++) {

printf("%2d ",a[i][j]);

}

printf("\n");

}

getch();

}

func(int q[5])  {

int a,t1,t2,t3;

t1=q[0];
t2=q[1];

q[0]=q[2];
q[1]=q[3];
q[2]=q[4];
q[3]=t1;
q[4]=t2;

return q[5];

}
----------------------------------------------------------------------------------------------------------

(n) A 6 x 6 matrix is entered through the keyboard and stored in a 2-dimensional array mat[7][7]. Write a program to obtain the Determinant values of this matrix.

Solution:


------------------------------------------------------------------------------

(o) For the following set of sample data, compute the standard deviation and the mean.
-6, -12, 8, 13, 11, 6, 7, 2, -6, -9, -10, 11, 10, 9, 2

Solution:



#include<stdio.h>
#include<conio.h>
#include<math.h>

void main() {

int a[15]={-6,-12,8,13,11,6,7,2,-6,-9,-10,11,10,9,2};
int i,j;
float temp,sd,sum=0,mean,x;

clrscr();

printf("\ndata set: \n\n");

for(i=0;i<15;i++) {

printf(" %3d ",a[i]);

}

printf("\n");


for(i=0;i<15;i++) {

sum=sum+a[i];          /* adding all the numbers */

}

mean=sum/15;           /* calculating the mean */



/* computing standard deviation */

for(i=0;i<15;i++) {

a[i]=pow((a[i]-mean),2);
x=x+a[i];
}

temp=x/15;
sd=sqrt(temp);

printf("\n\n\t\tmean= %f\n\t\tstandard deviation = %f\n",mean,sd);

getch();
}

--------------------------------------------------------------------------------------------------------------

(p) The area of a triangle can be computed by the sine law when 2 sides of the triangle and the angle between them are known.
Area = (1 / 2 ) ab sin ( angle )
Given the following 6 triangular pieces of land, write a program to find their area and determine which is largest,
Plot No.
  a                
b             
angle


137.4         
80.9           
0.78

155.2         
92.62         
0.89

149.3         
97.93        
1.35
160.0        
100.25         
9.00

155.6        
68.95           
1.25

149.7        
120.0           
1.75

Solution:



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

float a[6][3]={ {137.4, 80.9, 0.78},
{155.2, 92.62, 0.89},
{149.3, 97.93, 1.35},
{160.0, 100.25, 9.00},
{155.6, 68.95, 1.25},
{149.7, 120.0, 1.75} };

float big=0,area;
int sr=0,i;
clrscr();

for(i=0;i<6;i++) {

area=(1.0/2.0)*a[i][0]*a[i][1]*sin(a[i][2]);

if(area>big) {
big=area;
sr=i;
}

}

printf("\n\nPlot no. %d is the biggest.\n",sr);
printf("\nArea of plot no. %d = %f\n",sr,big);



getch();

}


-
------------------------------------------------------------------------------------------------------------

(q) For the following set of n data points (x, y), compute the correlation coefficient r,

x                      
y
34.22          
102.43
39.87          
100.93 

41.85            
97.43
43.23            
97.81
40.06            
98.32
53.29            
98.32
53.29           
100.07
54.14             
97.08
49.12             
91.59
40.71            
94.85
55.15             
94.65

Solution:


#include<stdio.h>

#include<conio.h>
#include<math.h>

void main() {

float a[][2]={ {34.22,102.43},
{39.87,100.93},
{41.85,97.43},
{43.23,97.81},
{40.06,98.32},
{53.29,98.32},
{53.29,100.07},
{54.14,97.08},
{49.12,91.59},
{40.71,94.85},
{55.15,94.65} };

int i,n=0;
float x2,y2,x,y,x_y,n_x2,n_y2,r;
clrscr();

for(i=0;i<11;i++) {

x2= x2 + ( a[i][0] * a[i][0] );  /* computing square of x */

y2= y2 + ( a[i][1] * a[i][1] );  /* computing square of y */

x= x + a[i][0];         /* computing total of x */

y= y + a[i][1];         /* computing total of y */

x_y= x_y + ( a[i][0] * a[i][1] ); /* computing total of x * y */

n++;

}

n_x2= n * x2;

n_y2= n * y2;

r=   ( x_y - x*y )/sqrt((n_x2-x2) * (n_y2-y2));


printf("      sum of square of x = %f\n\n",x2);
printf("      sum of square of y = %f\n\n",y2);
printf("                sum of x = %f\n\n",x);
printf("                sum of y = %f\n\n",y);
printf("            sum of x * y = %f\n\n",x_y);
printf("            sum of n*x2  = %f\n\n",n_x2);
printf("            sum of n*y2  = %f\n\n",n_y2);

printf("\n\n\nCorrelation cofficient = %f\n",r);

getch();

}

-------------------------------------------------------------------------------------------------------------

(r) For the following set of point given by (x, y) fit a straight line given by
y = a + bx

x            
y
3.0        
1.5
4.5        
2.0
5.5        
3.5
6.5        
5.0
7.5        
6.0
8.5        
7.5
8.0        
9.0
9.0        
10.5
9.5        
12.0
10.0      
14.0

Solution:


#include<stdio.h>
#include<conio.h>
#include<math.h>

void main() {

float data[][2]= { {3.0,1.5},
{4.5,2.0},
{5.5,3.5},
{6.5,5.0},
{7.5,6.0},
{8.5,7.5},
{8.0,9.0},
{9.0,10.5},
{9.5,12.0},
{10.0,14.0} };

int i,n=0;
float sx,sy,x2,y2,xy,a,b,Y;

clrscr();

for(i=0;i<10;i++) {

sx = sx + data[i][0];

sy = sy + data[i][1];

x2= x2 + ( data[i][0] * data[i][0] );

y2= y2 + ( data[i][1] * data[i][1] );

xy = xy + ( data[i][0] * data[i][1] );

n++;

}

printf(" sum of x = %f\n",sx);
printf(" sum of y = %f\n",sy);
printf(" sum of x2 = %f\n",x2);
printf(" sum of y2 = %f\n",y2);
printf(" sum of x*y = %f\n",xy);
printf(" total number = %d\n",n);


b = ( (n*xy) - (sx*sy) ) / ( n*x2 - (sx*sx) );

a = (sy/n) - b*(sx/n);

Y= a + b*sx ;


printf("\n\nvalue of a = %f\n\n",a);

printf("value of b = %f\n\n",b);

printf(" Y = %f \n\n",Y);

getch();

}

-------------------------------------------------------------------------------------------------------------

(s) The X and Y coordinates of 10 different points are entered through the keyboard. Write a program to find the distance of last point from the first point (sum of distance between consecutive points).

Solution:



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

float a[10][2],sx,sy;
int i;

clrscr();

for(i=0;i<10;i++) {

printf("Enter coordinates of point number %d:\n\n",i+1);

printf("Value of X coordinate:  ");
scanf("%f",&a[i][0]);

printf("\nValue of Y coordinate:  ");
scanf("%f",&a[i][1]);


clrscr();
}


for(i=0;i<10;i++) {

if(i>0 && i<10-1) {

sx = sx + a[i][0];

sy = sy = a[i][1];

}
}

printf(" First coordinate:  X = %f\tY = %f\n\n",a[0][0],a[0][1]);

printf("  Last coordinate:  X = %f\tY = %f\n\n",a[9][0],a[9][1]);

printf("\nDistance between them:  X = %f\tY = %f\n",sx,sy);

getch();
}
______________________________________________________________________

Let Us C / Chapter 7 (The C Pre-processor)

                               Exercise [C] (a) Write down macro definitions for the following: 1. To test whether a character ente...