E.BALAGURUSAMY SOLUTION PROGRAMMING IN ANSI C: CHAPTER-7


7.1 Write a program for fitting a straight line through a set of points (xi,

yi),i=1,2,3….n. The straight line equation is:

Y =mx+c

and the values of m and c are given by:

m=((n ∑(xi,yi))- (∑xi)(∑yi) )/( n(∑xi2)-(∑xi)2)

c=1/n(∑yi -m(∑xi))

All summations are from 1 to n.

Answer:

#include

#include

void main()

{

int i,n=10,v1,v2,x[10],y[10];

int total_x,total_y,total_xy,total_x2;

float m,c,temp,temp1;

clrscr();

printf(“Enter the values for x: “);

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

{

scanf(“%d”,&v1);

x[i]=v1;

}

printf(“Enter the values for y: “);

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

{

scanf(“%d”,&v2);

y[i]=v2;

}

total_x=total_y=total_xy=total_x2=0;

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

{

total_x=total_x+x[i];

total_y=total_y+y[i];

total_xy=total_xy+(x[i]*y[i]);

total_x2=total_x2+(x[i]*x[i]);

}

temp= total_x*total_y;

temp1=total_x*total_x;

m=((n*total_xy)-(temp))/((n*total_x2)-temp1);

c=((total_y)-(m*total_x))/n;

printf(” \nThe equation of the straight line is: “);

printf(” Y=%fX+%f”,m,c);

getch();

}

Output

Enter the values for x:

1 2 3 4 5 6 7 8 9 10

Enter the values for y:

1 2 3 4 5 6 7 8 9 10

The equation of the straight line is:

Y=1.00000X+0.000000

7.2 The daily maximum temperature recorded in 10 cities during the month of January (for all 31 days) have been tabulated as follows:

City

1 2 3 4 5 6 ……………………………10

Day

1

2

3

.

.

.

.

31

7.2. Write a program to read the table elements into a two-dimensional array temperature, and to find the city and day corresponding to

a) the highest temperature

b) the lowest temperature

Answer:

//Write a program to read the table elements into a two-dimensional array temperature, and to find the city and day corresponding to

//a) the highest temperature

//b) the lowest temperature

// Date : 16/03/2010

#include

#include

void main()

{

int Temp[2][2];

int i,j,City1,City2,MaxTemp,MinTemp;

clrscr();

printf(“Enter temperature:–\n\n”);

for(i=0;i<2;i++) { printf(“For City %d ->\n”,i);

for(j=0;j<2;j++) { printf(“For Day %d ->”,j);

scanf(“%d”,&Temp[i][j]);

}

}

clrscr();

printf(“Temperature Matix :— \n”);

printf(” City \n “);

for(i=0;i<2;i++)

printf(“%d “,i+1);

printf(“\n Day\n”);

for(i=0;i<2;i++)

{

printf(” %d “,i+1);

for(j=0;j<2;j++)

{

printf(” %d”,Temp[i][j]);

}

printf(“\n”);

}

MinTemp=MaxTemp=Temp[0][0];

City1=0;

City2=0;

for(i=0;i<2;i++)

{

for(j=0;j<2;j++)

{

if(MaxTemp<Temp[i][j]) { MaxTemp=Temp[i][j]; City1=j+1; } if(MinTemp>Temp[i][j])

{

MinTemp=Temp[i][j];

City2=j+1;

}

}

}

printf(“\n\nHighest Temperature of City %d is %d\n”,City1,MaxTemp);

printf(“Lowest Temperature of City %d is %d\n”,City2,MinTemp);

getch();

}

 

 

7.3 An election is contested by 5 candidates. The candidate are numbered are 1 to 5

and the voting is done by marking the candidate number on the ballot paper. Write

a program to read the ballots and count the votes cast for each candidate using an

array variable count. In case, a number, read is outside the range 1 to 5,the ballot

should be considered as a ‘spoilt ballot’ and the program should also count the

number of spoilt ballots.

 

Program:–

/* An election is contested by 5 candidates.

The candidate are numbered are 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a program to read the ballots and count the votes cast for each candidate using an array variable count. In case, a number, read is outside the range 1 to 5,the ballot should be considered as a ‘spoilt ballot’ and the program should also count the number of spoilt ballots. */

// Date March 16,2010

#include

#include

void main()

{

int i,vote[5],c1=0,c2=0,c3=0,c4=0,c5=0,count=0,count_sp=0,v;

clrscr();

printf(“Enter your votes for 5 candidates:”);

for(i=1;i<=5;i++)

{

scanf(“%d”,&v);

vote[i]=v;

}

for(i=1;i<=5;i++)

{

if(vote[i]==1)

c1=c1+1;

else

{

if(vote[i]==2)

c2=c2+1;

else

{

if(vote[i]==3)

c3=c3+1;

else

{

if(vote[i]==4)

c4=c4+1;

else

if(vote[i]==5)

c5=c5+1;

}

}

}

}

printf(” votes to candidate1=%d”,c1);

printf(” \nvotes to candidate2=%d”,c2);

printf(“\n votes to candidate3=%d”,c3);

printf(” \nvotes to candidate4=%d”,c4);

printf(” \nvotes to candidate5=%d”,c5);

for(i=1;i<=5;i++)

{

if(vote[i]<=5)

count=count+1;

else

count_sp=count_sp+1;

}

printf(” The number of valid votes is:%d”,count);

printf(” \nThe number of spoilt votes is:%d”,count_sp);

getch();

}

Output

Enter your votes for 5 candidates:

1

3

1

8

2

Votes to Candidate 1: 2

Votes to Candidate 2: 1

Votes to Candidate 3:1

Votes to Candidate 4:0

Votes to Candidate 5:0

The number of valid votes is: 4

The number of spoilt votes is: 1

7.51 The annual examination results of 10 students are tabulated as follows:

Roll No. Subject1 Subject2 Subject3

.

.

.

.____________________________________________________________________

Write a program to read the data and determine the following:

(a) Total marks obtained by each student.

(b) The highest marks in each subject and the Roll No. of the student who secured it.

(c) The student who obtained the highest total marks.

 

Program:–

/* The annual examination results of 10 students are tabulated as follows:

Roll No. Subject1 Subject2 Subject3

.

.

.

.

Write a program to read the data and determine the following:

(a) Total marks obtained by each student.

(b) The highest marks in each subject and the Roll No. of the student who secured it.

(c) The student who obtained the highest total marks.

*/

// Date March 16,2010

#include

#include

#define MAX 10

void main()

{

int i,roll,m1,m2,m3,sub1[MAX],sub2[MAX],sub3[MAX];

int total_sub1,total_sub2,total_sub3,total[MAX];

int max,max1,max2,max3,roll1,roll2,roll3;

clrscr();

printf(“Enter the marks for subject1 of all the students: “);

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

scanf(“%d”,&sub1[i]);

printf(“Enter the marks for subject2 of all the students: “);

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

scanf(“%d”,&sub2[i]);

printf(“Enter the marks for subject3 of all the students: “);

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

scanf(“%d”,&sub3[i]);

total_sub1=total_sub2=total_sub3=0;

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

{

total_sub1=total_sub1+sub1[i];

total_sub2=total_sub2+sub2[i];

total_sub3=total_sub3+sub3[i];

total[i]=sub1[i]+sub2[i]+sub3[i];

}

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

{

printf(“The total marks obtained by the student%d is =%d\n”,i+1,total[i]);

}

max1=sub1[0];

max2=sub2[0];

max3=sub3[0];

max=total[0];

roll1=0;

roll2=0;

roll3=0;

roll=0;

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

{

if(max1<sub1[i])

{

max1=sub1[i];

roll1=i+1;

}

if(max2<sub2[i])

{

max2=sub2[i];

roll2=i+1;

}

if(max3<sub3[i])

{

max3=sub3[i];

roll3=i+1;

}

if(max<total[i])

{

max=total[i];

roll=i+1;

}

}

printf(“\nThe highest marks in subject1 is %d and the roll number is %d”,max1,roll1);

printf(“\nThe highest marks in subject2 is %d and the roll number is %d”,max2,roll2);

printf(“\nThe highest marks in subject3 is %d and the roll number is %d”,max3,roll3);

printf(“\n The highest total marks is %d and the roll number is %d “,max,roll);

getch();

}

 

 

7.6 Given are one dimensional arrays A and B which are sorted in ascending

order. Write a program to merge them into a single sorted array C that contains

every item form array A and B, in ascending order.

 

 

Program:–

// Given are one dimensional arrays A and B which are sorted in ascending

// order. Write a program to merge them into a single sorted array C that contains

// every item form array A and B, in ascending order.

//Date: 16/03/2010

#include

#include

#define MAX 50

void main()

{

int a[MAX],b[MAX],c[MAX];

int ax,bx,cx,n,m,mn;

clrscr();

ax=bx=cx=0;

printf(“Enter no. of elements of array : “);

scanf(“%d %d”,&n,&m);

printf(“Enter elements of first array :\n”);

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

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

printf(“Enter elements of Second array :”);

for(i=0;i<m;i++)

scanf(“%d”,&b[i]);

mn=m+n;

while(ax
{

if(a[ax]<b[bx])

{

c[cx]=a[ax];

ax++;

}

else

{

c[cx]=b[bx];

bx++;

}

cx++;

}

if(ax==n)

{

while(bx<m)

{

c[cx]=b[bx];

bx++;

cx++;

}

}

else

{

while(ax<n)

{

c[cx]=a[ax];

ax++;

cx++;

}

}

//sorted array

printf(“the sorted array is : \n”);

for(i=0;i<mn;i++)

printf(“%d “,c[i]);

getch();

}

7.7 Write a program that will read the values of elements of A and B and produce the

product matrix C.

 

 

 

Program:–

// Write a program that will read the values of elements of A and B and produce the

// product matrix C.

//Date: 16/03/2010

#include

#include

#define MAX 10

void main()

{

int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX];

int i,j,k,row,col;

clrscr();

printf(“Enter row of matrix”);

scanf(“%d”,&row);

printf(“Enter column of matrix”);

scanf(“%d”,&col);

printf(“Enter first matrix\n”);

for(i=0;i<row;i++)

for(j=0;j<col;j++)
scanf(“%d”,&a[i][j]);

printf(“\nEnter second matrix \n”);

for(i=0;i<row;i++)

for(j=0;j<col;j++)
scanf(“%d”,&b[i][j]);

printf(“\nFirst matrix is : \n”);

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)
printf(“%d “,a[i][j]);

printf(“\n”);

}

printf(“\nSecond matrix is\n”);

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)
printf(“%d “,b[i][j]);

printf(“\n”);

}

for(i=0;i<row;i++)

for(j=0;j<col;j++)
{ c[i][j]=0;

for(k=0;k<col;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);

}

printf(“\nMultiplication is\n”);

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)
printf(“%d “,c[i][j]);

printf(“\n”);

}

getch();

}

 

 

7.8 Write a program that fills a five-by-five as follows:

• Upper left triangle with +1s

• Lower right triangle with -1s

• Right to left diagonal with zeros

Display the contents of the matrix using not more than two printf statements.

 

 

 

Program:–

//Write a program that fills a five-by-five as follows:

//• Upper left triangle with +1s

//• Lower right triangle with -1s

//• Right to left diagonal with zeros

//Display the contents of the matrix using not more than two printf statements.

// Date : 16/03/2010

#include

#include

void main()

{

int A[5][5];

int a,i,k,j;

clrscr();

a=3;

for(i=0;i<=3;i++)

{

for(j=0;j<=a;j++)

{

A[i][j]=+1;

}

a–;

}

j=4;

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

{

A[i][j]=0;

j–;

}

a=4;

for(i=1;i<=4;i++) { for(j=4;j>=a;j–)

{

A[i][j]=-1;

}

a–;

}

printf(“Array is:–\n\n”);

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

{

for(j=0;j<=4;j++) printf(“%d “,A[i][j]); printf(“\n”); } getch(); } 7.9 Write a program to implement selection sort. Algorithm:– Step 1: Read Array A. Step 2: For k=0 to 9 repeat Step 3 to Step 8. Step 3: Compute Small=A[k] & Loc=k. Step 4: For i=0 to 9 repeat Step 5 to Step 7 otherwise go to Step 7. Step 5: Check Small>A[i] then go to Step 6 otherwise go to Step 4.

Step 6: Compute Small=A[i] & Loc=i.

Step 7: Compute A[Loc]=A[k], A[k]=Small.

Step 8: Display Sorted Array A.

Flowchart:–

Program:–

//Write a program to implement selection sort.

// Date : 16/03/2010

#include

#include

void main()

{

int A[10];

int i,k,Small,Loc;

clrscr();

printf(“Enter Elements of Array:—\n”);

for(i=0;i<=9;i++)

scanf(“%d”,&A[i]);

for(k=0;k<=9;k++)

{

Small=A[k];

Loc=k;

for(i=k;i<=9;i++) if(Small>A[i])

{

Small=A[i];

Loc=i;

}

A[Loc]=A[k];

A[k]=Small;

}

printf(“Sorted Array is:–\n\n”);

for(i=0;i<=9;i++)

printf(“%d “,A[i]);

getch();

}

7.10 Write a program to implement Binary Search algorithm.

 

 

 

Program:–

//Write a program to implement Binary Search algorithm.

// Date : 16/03/2010

#include

#include

void main()

{

int Str[10];

int i,Beg,End,Mid,Item;

clrscr();

Beg=0;

End=9;

Mid=(Beg+End)/2;

printf(“Enetr Any Sorted Array:–\n”);

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

scanf(“%d”,&Str[i]);

printf(“Enter Item Which U want to Search:–\n”);

scanf(“%d”,&Item);

while((Item!=Str[Mid])&&(Beg<=End))

{

if(Item<Str[Mid]) End=Mid-1; else Beg=Mid+1; Mid=(Beg+End)/2; } if(Beg>End)

printf(“Item Not Found\n”);

else

printf(“%d Found At Index %d\n”,Item,Mid);

getch();

}

 

7.11 Write a program that will compute the length of a given character string.

 

 

Program:–

//Write a program that will compute the length of a given character string.

// Date : 16/03/2010

#include

#include

void main()

{

char Str[50];

int i,Len;

clrscr();

Len=0;

printf(“Enter a String:—\n”);

scanf(“%[^\n]s”,&Str);

for(i=0;Str[i]!=”;i++)

Len=Len+1;

printf(“Length of String is %d”,Len);

getch();

}

7.12 Write a program that will count the number occurrences of a specified character in a

given line of text.

 

 

 

Program:–

//Write a program that will count the number occurrences of a specified character in a

// given line of text.

// Date : 16/03/2010

#include

#include

void main()

{

char Str[50],CheckChar;

int i,Count,Len;

clrscr();

Count=0;

printf(“Enter a String:—\n”);

scanf(“%[^\n]s”,&Str);

Len=strlen(Str);

fflush(stdin);

printf(“Enter a charatcer:–\n”);

scanf(“%c”,&CheckChar);

for(i=0;i<=Len;i++)

if(CheckChar==Str[i])

Count=Count+1;

printf(“Number of occurences of %c is %d”,CheckChar,Count);

getch();

}

 

 

7.13 Write a program to read a matrix of size m*n and print its transpose.

 

 

Program:–

//Write a program to read a matrix of size m*n and print its transpose.

// Date : 16/03/2010

#include

#include

#define MAX 10

void main()

{

int A[MAX][MAX],C[MAX][MAX];

int Row,Col,i,j;

clrscr();

printf(“Enter Number of Rows:–\n”);

scanf(“%d”,&Row);

printf(“Enter Number of Column:–\n”);

scanf(“%d”,&Col);

printf(“Enter Matrix:—\n”);

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)
scanf(“%d”,&A[i][j]);

clrscr();

printf(“Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)
printf(“%d “,A[i][j]);

printf(“\n”);

}

for(i=0;i<Col;i++)
for(j=0;j<Row;j++)

C[i][j]=A[j][i];

printf(“Transpose of Matrix:—\n”);

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

for(j=0;j<Row;j++)

printf(“%d “,C[i][j]);

printf(“\n”);

}

getch();

}

7.3 An election is contested by 5 candidates. The candidate are numbered are 1 to 5

      and the voting is done by marking the candidate number on the ballot paper. Write

      a program to read the ballots and count the votes cast for each candidate using an

      array variable count. In case, a number, read is outside the range 1 to 5,the ballot

      should be considered as a ‘spoilt ballot’ and the program should also count the

      number of spoilt ballots.

 

 

 

 

 

 

 

Algorithm:–

 

Step1.  Initialize c1,c2, c3,c4,c5,count,count_sp with 0.

Step2. For i=0 to i=5, repeat step3.

Step3. Enter the value of v and store in vote[i]

Step4. For i=0 to i=5, repeat from step5 till step14 and for i>5,go to step15.

Step5. Check if vote[i]==1,if true, go to step6,if false go to step7.

Step6.Calculate c1=c1+1

Step7. Check if vote[i]==2,if true, go to step8,if false go to step9.

Step8.Calculate c2=c2+2

Step9.Check if vote[i]==3,if true, go to step10,if false go to step11.

Step10. Calculate c3=c3+2

Step 11.Check if vote[i]==4,if true, go to step12,if false go to step13.

Step12.Calculate c4=c4+2

Step13.Check if vote[i]==5,if true, go to step14,if false go to step15.

Step14.Calculate c5=c5+2

Step15. Display c1,c2,c3,c4 and c5.

Step16. For i=0 to i=5, repeat from step 17 to step, and i>5,go to step 20

Step17.Check if vote[i]<5,if true go to step18 and if false, go to step 19.

Step18.count=count+1 and go to step 20.

Step19.count_sp=count_sp+1 and go to step 20.

Step20. Display count and count_sp

Step21. Stop.

Flowchart:–

 

Program:–

 

/* An election is contested by 5 candidates.

The candidate are numbered are 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a program to read the ballots and count the votes cast for each candidate using an array variable count.  In case, a number, read is outside the range 1 to 5,the ballot should be considered as a ‘spoilt ballot’ and the program should also count the number of spoilt ballots. */

// Date March 16,2010

#include<stdio.h>

#include<conio.h>

void main()

{

int i,vote[5],c1=0,c2=0,c3=0,c4=0,c5=0,count=0,count_sp=0,v;

clrscr();

printf(“Enter your votes for 5 candidates:”);

for(i=1;i<=5;i++)

{

scanf(“%d”,&v);

vote[i]=v;

}

for(i=1;i<=5;i++)

{

if(vote[i]==1)

c1=c1+1;

else

{

if(vote[i]==2)

c2=c2+1;

else

{

if(vote[i]==3)

c3=c3+1;

else

{

if(vote[i]==4)

c4=c4+1;

else

if(vote[i]==5)

c5=c5+1;

}

}

}

}

printf(” votes to candidate1=%d”,c1);

printf(” \nvotes to candidate2=%d”,c2);

printf(“\n votes to candidate3=%d”,c3);

printf(” \nvotes to candidate4=%d”,c4);

printf(” \nvotes to candidate5=%d”,c5);

for(i=1;i<=5;i++)

{

if(vote[i]<=5)

count=count+1;

else

count_sp=count_sp+1;

}

printf(” The number of valid votes  is:%d”,count);

printf(” \nThe number of spoilt votes is:%d”,count_sp);

getch();

}

Output

Enter your votes for 5 candidates:

1

3

1

8

2

Votes to Candidate 1: 2

Votes to Candidate 2: 1

Votes to Candidate 3:1

Votes to Candidate 4:0

Votes to Candidate 5:0

The number of valid votes is:  4

The number of spoilt votes is: 1

 

5.1 The annual examination results of 10 students are tabulated as follows:

 

Roll No.              Subject1          Subject2                          Subject3

.

.

.

.____________________________________________________________________

 

Write a program to read the data and determine the following:

(a)   Total marks obtained by each student.

(b)    The highest marks in each subject and the Roll No. of the student who secured it.

(c)     The student who obtained the highest total marks.

Algorithm:–

 

Step1.  Declare

Step2.  For i=0 to i<10, Enter sub1[i]

Step3. For i=0 to i<10, Enter sub2[i]

Step4. For i=0 to i<10, Enter sub3[i]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

 

/* The annual examination results of 10 students are tabulated as follows:

Roll No.              Subject1                Subject2                           Subject3

.

.

.

.

Write a program to read the data and determine the following:

(a)            Total marks obtained by each student.

(b)            The highest marks in each subject and the Roll No. of the student who secured it.

(c)            The student who obtained the highest total marks.

*/

// Date March 16,2010

#include<stdio.h>

#include<conio.h>

#define MAX 10

void main()

{

int i,roll,m1,m2,m3,sub1[MAX],sub2[MAX],sub3[MAX];

int total_sub1,total_sub2,total_sub3,total[MAX];

int max,max1,max2,max3,roll1,roll2,roll3;

clrscr();

printf(“Enter the marks for subject1 of all the students: “);

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

scanf(“%d”,&sub1[i]);

printf(“Enter the marks for subject2 of all the students: “);

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

scanf(“%d”,&sub2[i]);

printf(“Enter the marks for subject3 of all the students: “);

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

scanf(“%d”,&sub3[i]);

total_sub1=total_sub2=total_sub3=0;

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

{

total_sub1=total_sub1+sub1[i];

total_sub2=total_sub2+sub2[i];

total_sub3=total_sub3+sub3[i];

total[i]=sub1[i]+sub2[i]+sub3[i];

}

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

{

printf(“The total marks obtained by the student%d is =%d\n”,i+1,total[i]);

}

max1=sub1[0];

max2=sub2[0];

max3=sub3[0];

max=total[0];

roll1=0;

roll2=0;

roll3=0;

roll=0;

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

{

if(max1<sub1[i])

{

max1=sub1[i];

roll1=i+1;

}

if(max2<sub2[i])

{

max2=sub2[i];

roll2=i+1;

}

if(max3<sub3[i])

{

max3=sub3[i];

roll3=i+1;

}

if(max<total[i])

{

max=total[i];

roll=i+1;

}

}

printf(“\nThe highest marks in subject1 is %d and the roll number is %d”,max1,roll1);

printf(“\nThe highest marks in subject2 is %d and the roll number is %d”,max2,roll2);

printf(“\nThe highest marks in subject3 is %d and the roll number is %d”,max3,roll3);

printf(“\n The highest total marks is %d and the roll number is %d “,max,roll);

getch();

}

 

7.6 Given are one dimensional arrays A and B which are sorted in ascending

      order. Write a program to merge them into a single sorted array C that contains

      every item form array A and B, in ascending order.

 

 

 

 

 

Algorithm:–

 

Step 1: Read m, n, Array a & Array b.

Step 2: Store 0 to ax, bx and cx.

Step 3: Compute mn=m+n

Step 4: Repeat Step 5 to Step 8 while ax<n && bx<m otherwise go to Step 9

Step 5: Check a[ax]<b[bx] then go to Step 6 otherwise go to Step 7

Step 6: Compute c[cx]=a[ax], ax=ax+1

Step 7: Compute c[cx]=b[bx], bx=bx+1

Step 8: Compute cx=cx+1

Step 9: Check ax==n then go to Step 10 otherwise go to Step 12

Step 10: Repeat Step 11 while bx<m

Step 11: Compute c[cx]=b[bx], bx=bx+1, cx=cx+1.

Step 12: Repeat Step 13 while ax<n

Step 13: Compute c[cx]=a[ax], ax=ax+1, cx=cx+1.

Step 14: Display Sorted Array c.

Flowchart:–

 

 

 

Program:–

 

// Given are one dimensional arrays A and B which are sorted in ascending

//      order. Write a program to merge them into a single sorted array C that contains

//      every item form array A and B, in ascending order.

//Date: 16/03/2010

#include<stdio.h>

#include<conio.h>

#define MAX 50

void main()

{

int a[MAX],b[MAX],c[MAX];

int ax,bx,cx,n,m,mn;

clrscr();

ax=bx=cx=0;

printf(“Enter no. of elements of array : “);

scanf(“%d %d”,&n,&m);

printf(“Enter elements of first array :\n”);

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

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

printf(“Enter elements of Second array :”);

for(i=0;i<m;i++)

scanf(“%d”,&b[i]);

mn=m+n;

while(ax<n && bx<m)

{

if(a[ax]<b[bx])

{

c[cx]=a[ax];

ax++;

}

else

{

c[cx]=b[bx];

bx++;

}

cx++;

}

if(ax==n)

{

while(bx<m)

{

c[cx]=b[bx];

bx++;

cx++;

}

}

else

{

while(ax<n)

{

c[cx]=a[ax];

ax++;

cx++;

}

}

//sorted array

printf(“the sorted array is : \n”);

for(i=0;i<mn;i++)

printf(“%d “,c[i]);

getch();

}

7.7 Write a program that will read the values of elements of A and B and produce the

       product matrix C.

 

 

 

Algorithm:–

 

Step 1: Read row, col, Array a and Array b.

Step 2: Display Array a and b.

Step 3: For i=0 to row repeat Step 4 to Step 7

Step 4: For j=0 to col repeat Step 5 to Step 7

Step 5: Store 0 to c[i][j]

Step 6: For k=0 to col repeat Step 7

Step 7: Compute c[i][j]=c[i][j]+(a[i][k]*b[k][j])

Step 8: Display c.

 

 

 

 

 

 

 

 

Flowchart:–

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

 

// Write a program that will read the values of elements of A and B and produce the

// product matrix C.

//Date: 16/03/2010

#include<stdio.h>

#include<conio.h>

#define MAX 10

void main()

{

int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX];

int i,j,k,row,col;

clrscr();

printf(“Enter row of matrix”);

scanf(“%d”,&row);

printf(“Enter column of matrix”);

scanf(“%d”,&col);

printf(“Enter first matrix\n”);

for(i=0;i<row;i++)

for(j=0;j<col;j++)

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

printf(“\nEnter second matrix \n”);

for(i=0;i<row;i++)

for(j=0;j<col;j++)

scanf(“%d”,&b[i][j]);

printf(“\nFirst matrix is : \n”);

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

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

printf(“\n”);

}

printf(“\nSecond matrix is\n”);

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

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

printf(“\n”);

}

for(i=0;i<row;i++)

for(j=0;j<col;j++)

{   c[i][j]=0;

for(k=0;k<col;k++)

c[i][j]=c[i][j]+(a[i][k]*b[k][j]);

}

printf(“\nMultiplication is\n”);

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

printf(“%d “,c[i][j]);

printf(“\n”);

}

getch();

}

 

 

 

 

 

7.8 Write a program that fills a five-by-five as follows:

  • Upper left triangle with +1s
  • Lower right triangle with -1s
  • Right to left diagonal with zeros

       Display the contents of the matrix using not more than two printf statements.

 

 

 

 

 

Algorithm:–

 

Step 1: Store Upper left triangle with +1s

Step 2: Store Lower right triangle with -1s

Step 3: Store Right to left diagonal with zeros

Step 4: Display A

 

 

 

 

 

 

 

 

 

 

Flowchart:–

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

 

//Write a program that fills a five-by-five as follows:

//•         Upper left triangle with +1s

//•         Lower right triangle with -1s

//•         Right to left diagonal with zeros

//Display the contents of the matrix using not more than two printf statements.

// Date : 16/03/2010

#include<stdio.h>

#include<conio.h>

void main()

{

int A[5][5];

int a,i,k,j;

clrscr();

a=3;

for(i=0;i<=3;i++)

{

for(j=0;j<=a;j++)

{

A[i][j]=+1;

}

a–;

}

j=4;

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

{

A[i][j]=0;

j–;

}

a=4;

for(i=1;i<=4;i++)

{

for(j=4;j>=a;j–)

{

A[i][j]=-1;

}

a–;

}

printf(“Array is:–\n\n”);

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

{

for(j=0;j<=4;j++)

printf(“%d  “,A[i][j]);

printf(“\n”);

}

getch();

}

7.9 Write a program to implement selection sort.

 

 

 

Algorithm:–

 

Step 1: Read Array A.

Step 2: For k=0 to 9 repeat Step 3 to Step 8.

Step 3: Compute Small=A[k] & Loc=k.

Step 4: For i=0 to 9 repeat Step 5 to Step 7 otherwise go to Step 7.

Step 5: Check Small>A[i] then go to Step 6 otherwise go to Step 4.

Step 6: Compute Small=A[i] & Loc=i.

Step 7: Compute A[Loc]=A[k], A[k]=Small.

Step 8: Display Sorted Array A.

 

Flowchart:–

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

 

//Write a program to implement selection sort.

// Date : 16/03/2010

#include<stdio.h>

#include<conio.h>

void main()

{

int A[10];

int i,k,Small,Loc;

clrscr();

printf(“Enter Elements of Array:—\n”);

for(i=0;i<=9;i++)

scanf(“%d”,&A[i]);

for(k=0;k<=9;k++)

{

Small=A[k];

Loc=k;

for(i=k;i<=9;i++)

if(Small>A[i])

{

Small=A[i];

Loc=i;

}

A[Loc]=A[k];

A[k]=Small;

}

printf(“Sorted Array is:–\n\n”);

for(i=0;i<=9;i++)

printf(“%d  “,A[i]);

getch();

}

7.10 Write a program to implement Binary Search algorithm.

 

Algorithm:–

 

Step 1: Store 0 to Beg & 9 to End.

Step 2: Compute Mid=(Beg+End)/2.

Step 3: Read a Sorted Array Str & an Item to Search.

Step 4: Repeat Step 5 to Step 8 while Item!=Str[Mid])&&(Beg<=End) otherwise go to Step 9

Step 5: Check Item<Str[Mid] then go to Step 6 otherwise go to Step 7

Step 6: Compute End=Mid-1

Step 7: Compute Beg=Mid+1

Step 8: Compute Mid=(Beg+End)/2

Step 9: Check Beg>End go to Step 10 otherwise go to Step 11

Step 10: Display “Item Not Found”

Step 11: Display “Item Found”

 

Flowchart:–

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

 

//Write a program to implement Binary Search algorithm.

// Date : 16/03/2010

#include<stdio.h>

#include<conio.h>

void main()

{

int Str[10];

int i,Beg,End,Mid,Item;

clrscr();

Beg=0;

End=9;

Mid=(Beg+End)/2;

printf(“Enetr Any Sorted Array:–\n”);

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

scanf(“%d”,&Str[i]);

printf(“Enter Item Which U want to Search:–\n”);

scanf(“%d”,&Item);

while((Item!=Str[Mid])&&(Beg<=End))

{

if(Item<Str[Mid])

End=Mid-1;

else

Beg=Mid+1;

Mid=(Beg+End)/2;

}

if(Beg>End)

printf(“Item Not Found\n”);

else

printf(“%d Found At Index %d\n”,Item,Mid);

getch();

}

7.11 Write a program that will compute the length of a given character string.

 

Algorithm:–

 

Step 1: Read a string Str.

Step 2: For i=0 to End of String repeat Step 3 to Step

Step 3: Compute Len=Len+1

Step 4: Display Len.

Flowchart:–

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

 

//Write a program that will compute the length of a given character string.

// Date : 16/03/2010

#include<stdio.h>

#include<conio.h>

void main()

{

char Str[50];

int i,Len;

clrscr();

Len=0;

printf(“Enter a String:—\n”);

scanf(“%[^\n]s”,&Str);

for(i=0;Str[i]!=”;i++)

Len=Len+1;

printf(“Length of String is %d”,Len);

getch();

}

7.12 Write a program that will count the number occurrences of a specified character in a

         given line of text.

 

 

 

 

 

Algorithm:–

 

Step 1: Read a string Str & a Character CheckChar

Step 2: Length of String is Len

Step 3: For i=0 to Len repeat Step 4 to Step 5

Step 4: Check CheckChar==Str[i] go to Step 5 otherwise go to Step 3

Step 5: Count=Count+1

Step 6: Display Count

 

 

 

 

 

 

 

Flowchart:–

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

 

//Write a program that will count the number occurrences of a specified character in a

//         given line of text.

// Date : 16/03/2010

#include<stdio.h>

#include<conio.h>

void main()

{

char Str[50],CheckChar;

int i,Count,Len;

clrscr();

Count=0;

printf(“Enter a String:—\n”);

scanf(“%[^\n]s”,&Str);

Len=strlen(Str);

fflush(stdin);

printf(“Enter a charatcer:–\n”);

scanf(“%c”,&CheckChar);

for(i=0;i<=Len;i++)

if(CheckChar==Str[i])

Count=Count+1;

printf(“Number of occurences of %c is %d”,CheckChar,Count);

getch();

}

 

 

 

 

 

7.13 Write a program to read a matrix of size m*n and print its transpose.

 

 

 

 

Algorithm:–

 

Step 1: Read Row, Col & Matrix A

Step 2: Display A

Step 3: For i=0 to Col repeat Step 4 to Step 5 otherwise go to Step 6

Step 4: For j=0 to Row repeat Step 5 otherwise go to Step 3

Step 5: Compute C[i][j]=A[j][i]

Step 6: Display C

 

 

 

 

 

 

 

 

 

 

 

 

 

Flowchart:–

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

 

//Write a program to read a matrix of size m*n and print its transpose.

// Date : 16/03/2010

#include<stdio.h>

#include<conio.h>

#define MAX 10

void main()

{

int A[MAX][MAX],C[MAX][MAX];

int Row,Col,i,j;

clrscr();

printf(“Enter Number of Rows:–\n”);

scanf(“%d”,&Row);

printf(“Enter Number of Column:–\n”);

scanf(“%d”,&Col);

printf(“Enter Matrix:—\n”);

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)

scanf(“%d”,&A[i][j]);

clrscr();

printf(“Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)

printf(“%d  “,A[i][j]);

printf(“\n”);

}

for(i=0;i<Col;i++)

for(j=0;j<Row;j++)

C[i][j]=A[j][i];

printf(“Transpose of Matrix:—\n”);

for(i=0;i<Col;i++)

{

for(j=0;j<Row;j++)

printf(“%d  “,C[i][j]);

printf(“\n”);

}

getch();

}

7.14 Every book published by international publishers should carry an International

        Standard Book Number (ISBN). It is a 10 character 4 part number as shown below.

 

                                                0-07-041183-2

The first part denotes the region, the second represents publisher, the third identifies the book and the fourth is the check digit. The check digit is computed as follows:

 

Sum= (1*first digit) + (2*second digit) + (3*third digit)+…………….+ (9*ninth digit)

 

Check digit is the remainder when Sum is divided by 11. Write a program that reads a given ISBN number and check whether it represents a valid ISBN.

 

Algorithm:–

Step 1: Read Array ISBN.

Step 2: Compute Sum=Sum+(i*ISBN[i]) for i=0 to 9

Step 3: Compute CheckDig=Sum%11

Step 4: Check CheckDig=ISBN[10] then go to Step 5 Otherwise go to Step 6

Step 5: Display “Valid ISBN”

Step 6: Display “Invalid ISBN”

 

Flowchart:–

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

 

//Write a program that reads a given ISBN number and check whether it represents a valid ISBN.

// Date : 16/03/2010

#include<stdio.h>

#include<conio.h>

#define MAX 10

void main()

{

int ISBN[11];

int i,j,Sum,CheckDig;

clrscr();

Sum=0;

printf(“Enter ISBN Number:—\n”);

for(i=1;i<=10;i++)

scanf(“%d”,&ISBN[i]);

for(i=1;i<=9;i++)

Sum=Sum+(i*ISBN[i]);

CheckDig=Sum%11;

if(CheckDig==ISBN[10])

printf(“\nValid ISBN\n”);

else

printf(“\nInvalid ISBN\n”);

getch();

}

 

 

 

7.15 Write a program to read two matrices A and B and print the following:

 

a)       A + B and

b)      A – B.

 

Algorithm:–

 

Step 1: Read   Row, Col, Array A & B.

Step 2: Display Array A & B.

Step 3: Compute Addition of Array A & B & Store in Array C.

Step 4: Display Array C

 

Flowchart:–

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program:–

//Write a program to read two matrices A and B and print the following:

//a)       A + B and

//b)       A – B.

// Date : 16/03/2010

#include<stdio.h>

#include<conio.h>

#define MAX 10

void main()

{

int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX];

int Row,Col,i,j;

clrscr();

printf(“Enter Number of Rows:–\n”);

scanf(“%d”,&Row);

printf(“Enter Number of Column:–\n”);

scanf(“%d”,&Col);

printf(“Enter First Matrix:—\n”);

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)

scanf(“%d”,&A[i][j]);

printf(“Enter Second Matrix:—\n”);

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)

scanf(“%d”,&B[i][j]);

clrscr();

printf(“First Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)

printf(“%d  “,A[i][j]);

printf(“\n”);

}

printf(“Second Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)

printf(“%d  “,B[i][j]);

printf(“\n”);

}

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)

C[i][j]=A[i][j]+B[i][j];

printf(“Addition of Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)

printf(“%d  “,C[i][j]);

printf(“\n”);

}

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)

C[i][j]=A[i][j]-B[i][j];

printf(“Subtration of Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)

printf(“%d  “,C[i][j]);

printf(“\n”);

}

getch();

}

Algorithm:–

Step 1: Read Array ISBN.

Step 2: Compute Sum=Sum+(i*ISBN[i]) for i=0 to 9

Step 3: Compute CheckDig=Sum%11

Step 4: Check CheckDig=ISBN[10] then go to Step 5 Otherwise go to Step 6

Step 5: Display “Valid ISBN”

Step 6: Display “Invalid ISBN”

Flowchart:–

Program:–

//Write a program that reads a given ISBN number and check whether it represents a valid ISBN.

// Date : 16/03/2010

#include

#include

#define MAX 10

void main()

{

int ISBN[11];

int i,j,Sum,CheckDig;

clrscr();

Sum=0;

printf(“Enter ISBN Number:—\n”);

for(i=1;i<=10;i++)

scanf(“%d”,&ISBN[i]);

for(i=1;i<=9;i++)

Sum=Sum+(i*ISBN[i]);

CheckDig=Sum%11;

if(CheckDig==ISBN[10])

printf(“\nValid ISBN\n”);

else

printf(“\nInvalid ISBN\n”);

getch();

}

7.15 Write a program to read two matrices A and B and print the following:

a) A + B and

b) A – B.

Program:–

//Write a program to read two matrices A and B and print the following:

//a) A + B and

//b) A – B.

// Date : 16/03/2010

#include

#include

#define MAX 10

void main()

{

int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX];

int Row,Col,i,j;

clrscr();

printf(“Enter Number of Rows:–\n”);

scanf(“%d”,&Row);

printf(“Enter Number of Column:–\n”);

scanf(“%d”,&Col);

printf(“Enter First Matrix:—\n”);

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)
scanf(“%d”,&A[i][j]);

printf(“Enter Second Matrix:—\n”);

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)
scanf(“%d”,&B[i][j]);

clrscr();

printf(“First Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)
printf(“%d “,A[i][j]);

printf(“\n”);

}

printf(“Second Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)
printf(“%d “,B[i][j]);

printf(“\n”);

}

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)
C[i][j]=A[i][j]+B[i][j];

printf(“Addition of Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)
printf(“%d “,C[i][j]);

printf(“\n”);

}

for(i=0;i<Row;i++)

for(j=0;j<Col;j++)
C[i][j]=A[i][j]-B[i][j];

printf(“Subtration of Matrix:—\n”);

for(i=0;i<Row;i++)

{

for(j=0;j<Col;j++)
printf(“%d “,C[i][j]);

printf(“\n”);

}

getch();

}

Advertisements

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s