SlideShare a Scribd company logo
ANCI C Chapter 7 Solution

Posted on April 6, 2012




Chapter Seven (07) Contains



01.One-Dimensional Arrays



02.Declaration of One-Dimensional Arrays



03.Initialization of One Dimensional Arrays



04.Two- Dimensional Arrays



05.Initialization of Two- Dimensional Arrays



06.Multi- Dimensional Arrays



07.Dynamic Arrays



Question No-7.2: Fill in the blanks in the following statements.

(a) The variable used as a subscript in an array is popularly known as…… variable.

Answer: index.

(b) An array can be initialized either at compile time or at ………..

Answer: run time.
(c) An array created using malloc function at run time is referred to as ……array.

Answer: pointer variable.

(d) An array that uses more than two subscripts is referred to as …….. array.

Answer: multidimensional

(e) ……… is the process of arranging the elements of an array in order.

Answer: sorting.



Question No-7.1: State whether the following statements are true or false.

(a) The type of all elements in an array must be the same.

Answer: True.

(b) When an array is declared, C automatically initializes its elements to zero.

Answer: True.

(c) An expression that evaluates to an integral value may be used as a subscript.

Answer: True.

(d) Accessing an array outside its range is a compile time error.

Answer: True.

(e) A char type variable can not be used as a subscript in an array.

Answer: True.

(f) An unsigned long int type can be used as a subscript in an array.

Answer: True.

(g) In C, by default, the first subscript is zero.

Answer: True.

(h) When initializing a multidimensional array, not specifying all its dimensions is an error.

Answer: True.

(i) When we use expression as a subscript, its result should be always greater than zero.
Answer: True.

(j) In C, we can use a maximum of 4 dimensions of an array.

Answer: False.

(k) In declaring an array, the array size can be constant or variable or an expression.

Answer: False.

(l) The declaration int x[2]={1,2,3};is illegal.

Answer: True.



Question No-7.3: Identify errors, if any, in each of the following array declaration statements,
assuming that ROW and COLUMN are declared as symbolic constants.

(a) int score (100);

Answer: Incorrect.

(b) float values [10,15];

Answer: Incorrect.

(c) float average [ROW],[COLUMN];

Answer: Incorrect.

(d) char name [15];

Answer: Correct.

(e) int sum [];

Answer: Correct.

(f) double salary [i+ROW]

Answer: Incorrect.

(g) long int number [ROW]

Answer: Incorrect.

(h) int array x[COLUMN];

Answer: Incorrect.
Question No-7.4: Identify errors, if any, in each of the following initialization statements.

(a) int number []={0,0,0,0,0};

Answer: Correct.

(b) float item [3] [2] ={0,1,2,3,4,5};

Answer: Correct.

(c) float word []={‘A’,’R’,’R’,’A’,’Y’};

Answer: Incorrect.

(d) int m[2,4] ={(0,0,0,0)(1,1,1,1)};

Answer: Incorrect.

(e) float result [10] =0;

Answer: Correct.



Qusetion No-7.5:Assume that the arrays A and B are declared as follows:

int A [5] [4];

float B [4]

Find the errors (if any) in the following program segments.



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

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

A [i] [j] =0;

Answer: No error.

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

scanf(“%f”,B[i]);

Answer: Error

Correction: for (i=1; i<=4; i++)
scanf (“%f”, &B[i]);

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

B[i] =B [i] + i;

Answer: Error.

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

B[i] = B[i] + i;



(d) for (i=4;i>=4;i–)

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

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

Answer: No error.

Question No-7.6: write a for loop statement that initializes all the dioganal elements of an array
to one and other to zero as shown below. assume 5 rows and 5 columns.

1 0 0 0 0 ………….. 0

0 1 0 0 0 ………….. 0

0 0 1 0 0 ………….. 0



0 0 0 0 - ………….. 1



Answer:

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

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

{

if(i==j)

printf(“1”);

else
printf(“0”);

}



Question No-7.7:we want to declare a two-dimentional integer type array called matrix for 3
rows and 5 columns. which for the following declarations are correct?



a) int matrix [3],[5];

Answer: Incorrect

b) int matrix [5] [3];

Answer: Incorrect

c) int matrix [1+2] [2+3];

Answer: Correct

d) int matrix [3,5];

Answer: Incorrect

e) int matrix [3] [5];

Answer: Correct



Question No-7.8: which of the following initialization statements are correct?

a) char str1[4]=”GOOD”;

Answer: Incorrect

b) char str2[ ]=”C”;

Answer: Correct

c) char str3[5]=”MOON”;

Answer: Correct

d) char str4[ ]={‘S’,’U’,’N’};

Answer: Incorrect
e) char str5[10]=”Sun”;

` Answer: Correct



Question No-7.9: What is a data structure? Whey is an array called a data structure?

Answer:

C support a rich set of derived and user-defined data types in

C language .Array and structures are also a structure data types because

they are used to represent data values that have a structure of some sort. In programming
parlance, such data types are known as data types.



Question No-7.10: What is a dynamic array? How is it created? Give a typical example of a
dynamic array?

Answer: The process of dynamic memory allocation and the arrays created at run time are
called dynamic array.

Example:

Malloc , calloc and realloc are dynamic array.

Question No-7.11: What is the error in the following program?

Answer:

main ()

{

int x;

float y [10];

……….

}



Question No-7.13: Discuss how initial values can be assigned to a multidimensional array.
Answer: C support arrays of three or more dimensional. the general form of a multidimensional
array is ….

Type array name [a1] [a2] [a3]…….[am];

Where a1l is the size of the dimensional.

Question-7.14: What is the output of the following program?

main ()

{

int m [] = {1,2,3,4,5}

int m;

for (x=0; x<5; x++ )

y=y+m [x]

printf(“%d”, y);

}



Output: 15



Question No-7.15: What is the output of the following program?

main ()

{

char string [ ]= “HELLO WORLD”;

int m;

for (m=0; string [m] !=’’;m++)

if ((m%2)==0 )

printf (“%c”, string [m] );

}
Output: HLOWRD



Solution of Programming Exercise



Question No-7.1: Write a program for fitting a straight line through a set of points (xi,yi),i=1,
…..,n.

The straight line equation is

Y=mx+c

And the values of m and c are given by

m=

c = (∑yi-m∑xi)

All summations are from 1 to n.Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int n,j,k,l,p,q,r,s;

float m,c;

printf(“How many points in the straight line: “);

scanf(“%d”,&n);

int *x,*y;

x=new[n];

y=new[n];

printf(“Enter %d points of x and y:n”,n);

for(int i=1;i<=n;i++)
scanf(“%d %d”,&x[i],&y[i]);

j=0;

k=0;

l=0;

q=0;

r=0;

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

{

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

k=k+x[i];

l=l+y[i];

p=k*l;

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

r=r+x[i];

}

j=j*n;

q=q*n;

s=r*r;

m=(j-p)/(q-s);

c=((l-(m*k))/n);

printf(“the value of the slop m= %fnthe value of the constant c= %f”,m,c);

getch();

}



Question No-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………………………………………….10



Day

………………………………………….



1

2

3

-

-

31

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 and

(b)The lowest temperature.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int cityday[5][5];

int i,j,max,min,m,n;

m=n=1;

printf(“n”);
for(i=0;i<5;i++)

{

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

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

}

max=cityday[0][0];

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

{

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

{

if(max<cityday[i][j])

{

max=cityday[i][j]; m=j+1; n=i+1;

}

}

}

printf(“nmax temperature %d in city no %d on the day %d”,max,m,n);

min=cityday[0][0];

m=n=1;

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

{

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

{

if(min>cityday[i][j])

{
min=cityday[i][j];

m=j+1; n=i+1;

}

}

}

printf(“nmin temperature %d in city no %d on the day %d”,min,m,n);

getch();

}



Question No-7.3: An election is contested by 5 candidates. The candidates are numbered 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 ballot should be considered as a spoilt ballot and
the program should also count the number of spoilt ballots.



Solution:



#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int i,j,k,l,m,n,votter[6]={0};

j=1;k=2;l=3;m=4;n=5;

printf(“press 1 to 5 for votting:n”);

printf(“press 0 for stop votting:n”);

scanf(“%d”,&i);
while(i!=0)

{

if(i==1)

{

votter[1]++;

j=i;

}

else if(i==2)

{

votter[2]++;

k=i;

}

else if(i==3)

{

votter[3]++;

l=i;

}

else if(i==4)

{

votter[4]++;

m=i;

}

else if(i==5)

{

votter[5]++;
n=i;

}

else

{

votter[0]++;

j=i;

}

scanf(“%d”,&i);

}

printf(“nCandidat %d has votes= %d”,j, votter[j]);

printf(“nCandidat %d has votes= %d”,k, votter[k]);

printf(“nCandidat %d has votes= %d”,l, votter[l]);

printf(“nCandidat %d has votes= %d”,m, votter[m]);

printf(“nCandidat %d has votes= %d”,n, votter[n]);

getch();

}




Question No-7.4: The following set of numbers is popularly known as Pascles triangle.1



1          2      1

1          3      3          1

1          4      6          4          1

1          5      10          10         5            1

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



If we denote rows by I and columns by j, then any element (except the boundary elements) in
the triangle is given by

P[i][j] = P[i-1][j-1] + P[i-1][j]

Write a program to calculate the elements of the Pascle triangle for 10 rows and print the
results.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int A[5][5]={0},i,j; // Declare Array with index 5 & 5 with values 0

for(i=0;i<5;i++) //This for() loop for first column value to do 1

A[i][0]=1;

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

for(j=1;j<5;j++) // for column

A[i][j]=A[i-1][j-1]+A[i-1][j]; // rules

clrscr(); // to clear previous screen

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

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

printf(“%d      “,A[i][j]); // to print value

printf(“nn”);

}

getch();

}
Question No-7.6:Given are two 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 from
arrays A and B, in ascending order.



#include<stdio.h>

#include<conio.h>

#define N 5

void main()

{

int i,j=0,a[N],b[N],c[2*N];

clrscr();

printf(“Enter the Matrix A:n”);

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

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

printf(“nEnter the Matrix B:n”);

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

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

printf(“nnThe resultant Matrix C is:n”);

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

{

if(b[i]<a[i])

{

c[j++]=b[i];

c[j++]=a[i];}

else
{

c[j++]=a[i];

c[j++]=b[i];}

}

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

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

getch();

}



Question No-7.7 Two matrices that have the same number of rows and columns can be
multiplied to produce a third matrix. Consider the following two matrices.

A = a11 a12……..a1n

a12 a22……..a2n

-     -         -

-     -             -

an1……………….ann



B=        b11 b12………b1n

b12 b22……..b2n

-     -         -

-     -         -

bn1……………..bnn



The product of A and B is a third matrix C of size n*n where is element of C is given by the
following equation.

Cij = ikbkj
Write a program that will read tha values of elements of A and B and produce the product
matrix C.

Solution:

#include<stdio.h>

#include<conio.h>

#define M 2

void main(){

int i,j,k,a[M][M],b[M][M],c[M][M];

clrscr();

printf(“Enter the matrix A:n”);

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

{

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

{

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

}

}

printf(“nEnter the matrix B:n”);

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

{

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

{

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

}

}

//calculation begins
for(i=0;i<M;i++)

{

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

{

c[i][j]=0;

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

{

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

}

}

}

printf(“nThe resultant matrix C is:n”);

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

{

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

{

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

}

printf(“n”);

}

getch();

}



Question No-7.8: Write a program that fills a five-by-five matrix 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.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j; // Declare Array with index 5 & 5 with values 0

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

{

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

if(i>=4-j)

{

if(i==4-j)

printf(” 0 “);

else

printf(“-1 “);

}

else printf(“+1 “);

printf(“nn”);

}

getch();

}

Question No-7.9 Selection sort is based on the following idea:
Selecting the largest array element and swapping it with the last array element leaves an
unsorted list whose size is 1 less than the size of the original list. If we repeat this step again on
the unsorted list we will have an ordered list of size 2 and an unordered list size n-2. When
repeat this until the size of the unsorted list becomes one , the result will be a sorted list.

Write a program to implement this algorithm.



Solution: Sorry!this solution will be as soon as possible…. Authority



Question No-7.10 Develop a program to implement the binary search algorithm. This technique
compares the search key value of the element that is midway in a “sorted” lies. Then ;

(a) If they match, the search is over.

(b) If the search key value is less than the middle value, then the first half of list contains the key
value.

(c) If the search key value is greater than the middle value, then the second half contains the key
value.

Repeat this “devide –and-conquer “ strategy until we have match. If the list is reduced to one
non-matching element, then the list does not contain the key value.

Used the sorted list created in exercise 7.9 or used any other sorted list.



Solution:

#include<stdio.h>

#include<conio.h>

void main(){

int i,beg,end,mid,a[20],item;

clrscr();

printf(“Enter 13 elementsn”);

for(i=1;i<=13;i++)
{

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

}

printf(“nEnter what item you want to searchn”);

scanf(“%d”,&item);

beg=1;

end=13;

mid=((beg+end)/2);

while(beg<=end && a[mid]!=item)

{

if(item<a[mid])

{

end=mid-1;

}

else

{

beg=mid+1;

}

mid=((beg+end)/2);

}

if(item==a[mid])

printf(“nnThe item is in the listnIt’s position is=%dn”,mid);

else

printf(“nnThe item is not in the listn”);

getch();
}

Question No-7.11: Write a program that will compute the length of a given character string.

Solution:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char s[50];

int length;clrscr();

printf(“nnInput a string:”);

printf(“?”);

gets(s);

length= strlen(s);

printf(“n this string contains %d character.”,length);

getch();

}



Question No-7.5 The annual examination results of 100 students are tabulated as follows:

________________________________________

    Roll No:   Subject 1 Subject 2     Subject 3

________________________________________

________________________________________

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.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int a[5][4],sum[5]={0,0,0,0,0};

int i,j,t;

clrscr();

printf(“enter roll,marks in three sub for five studentn”);

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

{

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

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

}

printf(“roll s_1 s_2 s_3″);

printf(“nn”);

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

{

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

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

printf(“n”);

}

printf(“n”);
for(i=0;i<5;i++)

{

sum[i]=a[i][1]+a[i][2]+a[i][3];

printf(“sum[%d]=%d”,i,sum[i]);

printf(“n”);

}

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

{

for(j=0;j<=5-i;j++)

if(sum[j]>sum[j+1])

{

t=sum[j+1];

sum[j+1]=sum[j];

sum[j]=t;

}

}

printf(“largest value:=%d”,sum[5]);



getch();

}



Question No-7.12 Write a program that will count the number occurrences of a specified
character in a given line of text. Test your program.



Solution:

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

#include<string.h>

void main()

{

char a[100],b[100];

char n,dinar;

printf(“Input two string:n”);

gets(a);

n=strlen(a);

gets(b);

sazon=strncmp(a,b,n);

if(dinar==0)

printf(“equal.”);

else

if(sazon>0)

printf(“a>b”);

else

printf(“a<b”);

getch();

}

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

Solution:

#include<stdio.h>

#include<conio.h>

void main()
{

int i,j,k,A[3][2];

clrscr();

printf(“give your values:n”);

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

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

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

printf(“Your Matrics is:n”);

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

{

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

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

printf(“n”);

}

printf(“the transverse of the above matrics:n”);

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

{

for(j=1;j<=3;j++)

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

printf(“n”);

}

getch();

}



Question No-:7.14 Every book published by international publishers should carry an
International Standard Book Number (ISBN) .It is a ten characters 4 part number as shown
bellows,

    0-07-041183-2

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

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

Cheek digit is the remainder when sum is divided by 11. Write a program that reads a given
(ISBN) number and cheaks wheather it represents a valid ISBN.



Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int ISBN[10];

int i,sum,n,d;

clrscr();

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

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

n=1; sum=0;

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

{

sum+=n*ISBN[i];

n++;

}

d=sum%11;

if(d==ISBN[9])
printf(“valid.”);

else

printf(“invalid.”);

getch();

}



Question No-7.1 write a program to read two matrices A and B and print the folloing:

(a) A+B; and

(b) A-B.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int a[3][3],b[3][3],s[3][3],c[3][3];

int i,j;

clrscr();

printf(“enter 1st matrices:n”);

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

{

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

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

}

printf(“enter 2nd matrices:n”);
for(i=0;i<3;i++)

{

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

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

}

printf(“nA matricesn”);

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

{

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

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

printf(“n”);

}

printf(“nB matricesn”);

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

{

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

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

printf(“n”);

}

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

{

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

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



}
printf(“nnsum of two matrices Sn”);

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

{

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

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

printf(“n”);

}

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

{

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

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

}

printf(“nnminus of two matrices Cn”);

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

{

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

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

printf(“n”);

}

getch();

}



Share this:

Share

Like this:
Like

One blogger likes this post.



This entry was posted in Uncategorized by fuadahmed. Bookmark the permalink.

Leave a Reply



Enter your comment here...



Theme: Twenty Eleven |          Blog at WordPress.com.

Follow

Follow “Fuad Ahmed”

Get every new post delivered to your Inbox.




Powered by WordPress.com

More Related Content

What's hot

The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
Shariful Haque Robin
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solution
rohit kumar
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
Hazrat Bilal
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.
Hazrat Bilal
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solution
rohit kumar
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
Abdullah Al Naser
 
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solutionLet us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Hazrat Bilal
 
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
rohit kumar
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
KavyaSharma65
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
Sreedhar Chowdam
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
Rahul Pandit
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operations
Sreedhar Chowdam
 
C Pointers
C PointersC Pointers
C Pointers
omukhtar
 

What's hot (20)

The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solution
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solution
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solutionLet us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
 
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
C++ file
C++ fileC++ file
C++ file
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operations
 
C Pointers
C PointersC Pointers
C Pointers
 

Similar to Ansi c

COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
Hemantha Kulathilake
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
trupti1976
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
naveed jamali
 
UNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptxUNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptx
Abhishekkumarsingh630054
 
C Arrays.ppt
C Arrays.pptC Arrays.ppt
C Arrays.ppt
ssuser0c1819
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
Gradeup
 
CS.3.Arrays.pdf
CS.3.Arrays.pdfCS.3.Arrays.pdf
CS.3.Arrays.pdf
YasirAli74993
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
Mrhaider4
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
MATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdfMATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdf
ahmed8651
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
mohd_mizan
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
SumitSingh813090
 
C tutorial
C tutorialC tutorial
C tutorial
Anurag Sukhija
 
Array Array Array Array Array Array Arra
Array Array Array Array Array Array ArraArray Array Array Array Array Array Arra
Array Array Array Array Array Array Arra
FaysalAhamed32
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
BAINIDA
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 

Similar to Ansi c (20)

COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
UNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptxUNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptx
 
C Arrays.ppt
C Arrays.pptC Arrays.ppt
C Arrays.ppt
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
CS.3.Arrays.pdf
CS.3.Arrays.pdfCS.3.Arrays.pdf
CS.3.Arrays.pdf
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
MATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdfMATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdf
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
C tutorial
C tutorialC tutorial
C tutorial
 
Array Array Array Array Array Array Arra
Array Array Array Array Array Array ArraArray Array Array Array Array Array Arra
Array Array Array Array Array Array Arra
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 

Recently uploaded

Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 

Recently uploaded (20)

Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 

Ansi c

  • 1. ANCI C Chapter 7 Solution Posted on April 6, 2012 Chapter Seven (07) Contains 01.One-Dimensional Arrays 02.Declaration of One-Dimensional Arrays 03.Initialization of One Dimensional Arrays 04.Two- Dimensional Arrays 05.Initialization of Two- Dimensional Arrays 06.Multi- Dimensional Arrays 07.Dynamic Arrays Question No-7.2: Fill in the blanks in the following statements. (a) The variable used as a subscript in an array is popularly known as…… variable. Answer: index. (b) An array can be initialized either at compile time or at ……….. Answer: run time.
  • 2. (c) An array created using malloc function at run time is referred to as ……array. Answer: pointer variable. (d) An array that uses more than two subscripts is referred to as …….. array. Answer: multidimensional (e) ……… is the process of arranging the elements of an array in order. Answer: sorting. Question No-7.1: State whether the following statements are true or false. (a) The type of all elements in an array must be the same. Answer: True. (b) When an array is declared, C automatically initializes its elements to zero. Answer: True. (c) An expression that evaluates to an integral value may be used as a subscript. Answer: True. (d) Accessing an array outside its range is a compile time error. Answer: True. (e) A char type variable can not be used as a subscript in an array. Answer: True. (f) An unsigned long int type can be used as a subscript in an array. Answer: True. (g) In C, by default, the first subscript is zero. Answer: True. (h) When initializing a multidimensional array, not specifying all its dimensions is an error. Answer: True. (i) When we use expression as a subscript, its result should be always greater than zero.
  • 3. Answer: True. (j) In C, we can use a maximum of 4 dimensions of an array. Answer: False. (k) In declaring an array, the array size can be constant or variable or an expression. Answer: False. (l) The declaration int x[2]={1,2,3};is illegal. Answer: True. Question No-7.3: Identify errors, if any, in each of the following array declaration statements, assuming that ROW and COLUMN are declared as symbolic constants. (a) int score (100); Answer: Incorrect. (b) float values [10,15]; Answer: Incorrect. (c) float average [ROW],[COLUMN]; Answer: Incorrect. (d) char name [15]; Answer: Correct. (e) int sum []; Answer: Correct. (f) double salary [i+ROW] Answer: Incorrect. (g) long int number [ROW] Answer: Incorrect. (h) int array x[COLUMN]; Answer: Incorrect.
  • 4. Question No-7.4: Identify errors, if any, in each of the following initialization statements. (a) int number []={0,0,0,0,0}; Answer: Correct. (b) float item [3] [2] ={0,1,2,3,4,5}; Answer: Correct. (c) float word []={‘A’,’R’,’R’,’A’,’Y’}; Answer: Incorrect. (d) int m[2,4] ={(0,0,0,0)(1,1,1,1)}; Answer: Incorrect. (e) float result [10] =0; Answer: Correct. Qusetion No-7.5:Assume that the arrays A and B are declared as follows: int A [5] [4]; float B [4] Find the errors (if any) in the following program segments. (a) for (i=0;i<=5;i++) for(j=1;j<=4;j++) A [i] [j] =0; Answer: No error. (b) for (i=1;i<4;i++) scanf(“%f”,B[i]); Answer: Error Correction: for (i=1; i<=4; i++)
  • 5. scanf (“%f”, &B[i]); (c) (i=0;i<=4;i++) B[i] =B [i] + i; Answer: Error. Correction: for (i=1; i<=4; i++) B[i] = B[i] + i; (d) for (i=4;i>=4;i–) for (j=0;j<4;j++) A [i] [j] =B [j] +1.0; Answer: No error. Question No-7.6: write a for loop statement that initializes all the dioganal elements of an array to one and other to zero as shown below. assume 5 rows and 5 columns. 1 0 0 0 0 ………….. 0 0 1 0 0 0 ………….. 0 0 0 1 0 0 ………….. 0 0 0 0 0 - ………….. 1 Answer: for(i=0;i<5;i++) for(j=0;j<5;j++) { if(i==j) printf(“1”); else
  • 6. printf(“0”); } Question No-7.7:we want to declare a two-dimentional integer type array called matrix for 3 rows and 5 columns. which for the following declarations are correct? a) int matrix [3],[5]; Answer: Incorrect b) int matrix [5] [3]; Answer: Incorrect c) int matrix [1+2] [2+3]; Answer: Correct d) int matrix [3,5]; Answer: Incorrect e) int matrix [3] [5]; Answer: Correct Question No-7.8: which of the following initialization statements are correct? a) char str1[4]=”GOOD”; Answer: Incorrect b) char str2[ ]=”C”; Answer: Correct c) char str3[5]=”MOON”; Answer: Correct d) char str4[ ]={‘S’,’U’,’N’}; Answer: Incorrect
  • 7. e) char str5[10]=”Sun”; ` Answer: Correct Question No-7.9: What is a data structure? Whey is an array called a data structure? Answer: C support a rich set of derived and user-defined data types in C language .Array and structures are also a structure data types because they are used to represent data values that have a structure of some sort. In programming parlance, such data types are known as data types. Question No-7.10: What is a dynamic array? How is it created? Give a typical example of a dynamic array? Answer: The process of dynamic memory allocation and the arrays created at run time are called dynamic array. Example: Malloc , calloc and realloc are dynamic array. Question No-7.11: What is the error in the following program? Answer: main () { int x; float y [10]; ………. } Question No-7.13: Discuss how initial values can be assigned to a multidimensional array.
  • 8. Answer: C support arrays of three or more dimensional. the general form of a multidimensional array is …. Type array name [a1] [a2] [a3]…….[am]; Where a1l is the size of the dimensional. Question-7.14: What is the output of the following program? main () { int m [] = {1,2,3,4,5} int m; for (x=0; x<5; x++ ) y=y+m [x] printf(“%d”, y); } Output: 15 Question No-7.15: What is the output of the following program? main () { char string [ ]= “HELLO WORLD”; int m; for (m=0; string [m] !=’’;m++) if ((m%2)==0 ) printf (“%c”, string [m] ); }
  • 9. Output: HLOWRD Solution of Programming Exercise Question No-7.1: Write a program for fitting a straight line through a set of points (xi,yi),i=1, …..,n. The straight line equation is Y=mx+c And the values of m and c are given by m= c = (∑yi-m∑xi) All summations are from 1 to n.Solution: #include<stdio.h> #include<conio.h> void main() { clrscr(); int n,j,k,l,p,q,r,s; float m,c; printf(“How many points in the straight line: “); scanf(“%d”,&n); int *x,*y; x=new[n]; y=new[n]; printf(“Enter %d points of x and y:n”,n); for(int i=1;i<=n;i++)
  • 10. scanf(“%d %d”,&x[i],&y[i]); j=0; k=0; l=0; q=0; r=0; for(i=1;i<=n;i++) { j=j+(x[i]*y[i]); k=k+x[i]; l=l+y[i]; p=k*l; q=q+(x[i]*x[i]); r=r+x[i]; } j=j*n; q=q*n; s=r*r; m=(j-p)/(q-s); c=((l-(m*k))/n); printf(“the value of the slop m= %fnthe value of the constant c= %f”,m,c); getch(); } Question No-7.2 The daily maximum temperature recorded in 10 cities during the month of January (for all 31 days) have been tabulated as follows:
  • 11. City 1 2 3………………………………………….10 Day …………………………………………. 1 2 3 - - 31 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 and (b)The lowest temperature. Solution: #include<stdio.h> #include<conio.h> void main() { int cityday[5][5]; int i,j,max,min,m,n; m=n=1; printf(“n”);
  • 12. for(i=0;i<5;i++) { for(j=0;j<5;j++) scanf(“%d”,&cityday[i][j]); } max=cityday[0][0]; for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(max<cityday[i][j]) { max=cityday[i][j]; m=j+1; n=i+1; } } } printf(“nmax temperature %d in city no %d on the day %d”,max,m,n); min=cityday[0][0]; m=n=1; for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(min>cityday[i][j]) {
  • 13. min=cityday[i][j]; m=j+1; n=i+1; } } } printf(“nmin temperature %d in city no %d on the day %d”,min,m,n); getch(); } Question No-7.3: An election is contested by 5 candidates. The candidates are numbered 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 ballot should be considered as a spoilt ballot and the program should also count the number of spoilt ballots. Solution: #include<stdio.h> #include<conio.h> void main() { clrscr(); int i,j,k,l,m,n,votter[6]={0}; j=1;k=2;l=3;m=4;n=5; printf(“press 1 to 5 for votting:n”); printf(“press 0 for stop votting:n”); scanf(“%d”,&i);
  • 15. n=i; } else { votter[0]++; j=i; } scanf(“%d”,&i); } printf(“nCandidat %d has votes= %d”,j, votter[j]); printf(“nCandidat %d has votes= %d”,k, votter[k]); printf(“nCandidat %d has votes= %d”,l, votter[l]); printf(“nCandidat %d has votes= %d”,m, votter[m]); printf(“nCandidat %d has votes= %d”,n, votter[n]); getch(); } Question No-7.4: The following set of numbers is popularly known as Pascles triangle.1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 - - - - - - -
  • 16. - - - - - - - If we denote rows by I and columns by j, then any element (except the boundary elements) in the triangle is given by P[i][j] = P[i-1][j-1] + P[i-1][j] Write a program to calculate the elements of the Pascle triangle for 10 rows and print the results. Solution: #include<stdio.h> #include<conio.h> void main() { int A[5][5]={0},i,j; // Declare Array with index 5 & 5 with values 0 for(i=0;i<5;i++) //This for() loop for first column value to do 1 A[i][0]=1; for(i=1;i<5;i++) // for row for(j=1;j<5;j++) // for column A[i][j]=A[i-1][j-1]+A[i-1][j]; // rules clrscr(); // to clear previous screen for(i=0;i<5;i++) { for(j=0;j<=i;j++) printf(“%d “,A[i][j]); // to print value printf(“nn”); } getch(); }
  • 17. Question No-7.6:Given are two 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 from arrays A and B, in ascending order. #include<stdio.h> #include<conio.h> #define N 5 void main() { int i,j=0,a[N],b[N],c[2*N]; clrscr(); printf(“Enter the Matrix A:n”); for(i=0;i<N;i++) scanf(“%d”,&a[i]); printf(“nEnter the Matrix B:n”); for(i=0;i<N;i++) scanf(“%d”,&b[i]); printf(“nnThe resultant Matrix C is:n”); for(i=0;i<N;i++) { if(b[i]<a[i]) { c[j++]=b[i]; c[j++]=a[i];} else
  • 18. { c[j++]=a[i]; c[j++]=b[i];} } for(i=0;i<2*N;i++) printf(“%d “,c[i]); getch(); } Question No-7.7 Two matrices that have the same number of rows and columns can be multiplied to produce a third matrix. Consider the following two matrices. A = a11 a12……..a1n a12 a22……..a2n - - - - - - an1……………….ann B= b11 b12………b1n b12 b22……..b2n - - - - - - bn1……………..bnn The product of A and B is a third matrix C of size n*n where is element of C is given by the following equation. Cij = ikbkj
  • 19. Write a program that will read tha values of elements of A and B and produce the product matrix C. Solution: #include<stdio.h> #include<conio.h> #define M 2 void main(){ int i,j,k,a[M][M],b[M][M],c[M][M]; clrscr(); printf(“Enter the matrix A:n”); for(i=0;i<M;i++) { for(j=0;j<M;j++) { scanf(“%d”,&a[i][j]); } } printf(“nEnter the matrix B:n”); for(i=0;i<M;i++) { for(j=0;j<M;j++) { scanf(“%d”,&b[i][j]); } } //calculation begins
  • 20. for(i=0;i<M;i++) { for(j=0;j<M;j++) { c[i][j]=0; for(k=0;k<M;k++) { c[i][j]=c[i][j]+(a[i][k]*b[k][j]); } } } printf(“nThe resultant matrix C is:n”); for(i=0;i<M;i++) { for(j=0;j<M;j++) { printf(“%d “,c[i][j]); } printf(“n”); } getch(); } Question No-7.8: Write a program that fills a five-by-five matrix as follows: • Upper left triangle with +1s
  • 21. Lower right triangle with -1s • Right to left diagonal with zeros Display the contents of the matrix using not more than two printf statements. Solution: #include<stdio.h> #include<conio.h> void main() { int i,j; // Declare Array with index 5 & 5 with values 0 for(i=0;i<5;i++) { for(j=0;j<5;j++) if(i>=4-j) { if(i==4-j) printf(” 0 “); else printf(“-1 “); } else printf(“+1 “); printf(“nn”); } getch(); } Question No-7.9 Selection sort is based on the following idea:
  • 22. Selecting the largest array element and swapping it with the last array element leaves an unsorted list whose size is 1 less than the size of the original list. If we repeat this step again on the unsorted list we will have an ordered list of size 2 and an unordered list size n-2. When repeat this until the size of the unsorted list becomes one , the result will be a sorted list. Write a program to implement this algorithm. Solution: Sorry!this solution will be as soon as possible…. Authority Question No-7.10 Develop a program to implement the binary search algorithm. This technique compares the search key value of the element that is midway in a “sorted” lies. Then ; (a) If they match, the search is over. (b) If the search key value is less than the middle value, then the first half of list contains the key value. (c) If the search key value is greater than the middle value, then the second half contains the key value. Repeat this “devide –and-conquer “ strategy until we have match. If the list is reduced to one non-matching element, then the list does not contain the key value. Used the sorted list created in exercise 7.9 or used any other sorted list. Solution: #include<stdio.h> #include<conio.h> void main(){ int i,beg,end,mid,a[20],item; clrscr(); printf(“Enter 13 elementsn”); for(i=1;i<=13;i++)
  • 23. { scanf(“%d”,&a[i]); } printf(“nEnter what item you want to searchn”); scanf(“%d”,&item); beg=1; end=13; mid=((beg+end)/2); while(beg<=end && a[mid]!=item) { if(item<a[mid]) { end=mid-1; } else { beg=mid+1; } mid=((beg+end)/2); } if(item==a[mid]) printf(“nnThe item is in the listnIt’s position is=%dn”,mid); else printf(“nnThe item is not in the listn”); getch();
  • 24. } Question No-7.11: Write a program that will compute the length of a given character string. Solution: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[50]; int length;clrscr(); printf(“nnInput a string:”); printf(“?”); gets(s); length= strlen(s); printf(“n this string contains %d character.”,length); getch(); } Question No-7.5 The annual examination results of 100 students are tabulated as follows: ________________________________________ Roll No: Subject 1 Subject 2 Subject 3 ________________________________________ ________________________________________ Write a program to read the data and determine the following : (a)Total marks obtained by each student.
  • 25. (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. Solution: #include<stdio.h> #include<conio.h> void main() { int a[5][4],sum[5]={0,0,0,0,0}; int i,j,t; clrscr(); printf(“enter roll,marks in three sub for five studentn”); for(i=0;i<5;i++) { for(j=0;j<4;j++) scanf(“%d”,&a[i][j]); } printf(“roll s_1 s_2 s_3″); printf(“nn”); for(i=0;i<5;i++) { for(j=0;j<4;j++) printf(“ %d”,a[i][j]); printf(“n”); } printf(“n”);
  • 27. #include<conio.h> #include<string.h> void main() { char a[100],b[100]; char n,dinar; printf(“Input two string:n”); gets(a); n=strlen(a); gets(b); sazon=strncmp(a,b,n); if(dinar==0) printf(“equal.”); else if(sazon>0) printf(“a>b”); else printf(“a<b”); getch(); } Question No-7.13: Write a program to read a matrix of size m*n and print its transpose. Solution: #include<stdio.h> #include<conio.h> void main()
  • 28. { int i,j,k,A[3][2]; clrscr(); printf(“give your values:n”); for(i=1;i<=3;i++) for(j=1;j<=2;j++) scanf(“%d”,&A[i][j]); printf(“Your Matrics is:n”); for(i=1;i<=3;i++) { for(j=1;j<=2;j++) printf(“%d “,A[i][j]); printf(“n”); } printf(“the transverse of the above matrics:n”); for(i=1;i<=2;i++) { for(j=1;j<=3;j++) printf(” %d”,A[j][i]); printf(“n”); } getch(); } Question No-:7.14 Every book published by international publishers should carry an International Standard Book Number (ISBN) .It is a ten characters 4 part number as shown
  • 29. bellows, 0-07-041183-2 The first part denotes the region, the second represents publisher, the third identifies the book and the fourth is the cheek digit. The cheek digit is computed as follows: Sum=(1*first digit)+(2*second digit)+(3*third digit)+……+(9*ninth digit). Cheek digit is the remainder when sum is divided by 11. Write a program that reads a given (ISBN) number and cheaks wheather it represents a valid ISBN. Solution: #include<stdio.h> #include<conio.h> void main() { int ISBN[10]; int i,sum,n,d; clrscr(); for(i=0;i<10;i++) scanf(“%d”,ISBN[i]); n=1; sum=0; for(i=0;i<9;i++) { sum+=n*ISBN[i]; n++; } d=sum%11; if(d==ISBN[9])
  • 30. printf(“valid.”); else printf(“invalid.”); getch(); } Question No-7.1 write a program to read two matrices A and B and print the folloing: (a) A+B; and (b) A-B. Solution: #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],s[3][3],c[3][3]; int i,j; clrscr(); printf(“enter 1st matrices:n”); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); } printf(“enter 2nd matrices:n”);
  • 31. for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(“%d”,&b[i][j]); } printf(“nA matricesn”); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(” %d”,a[i][j]); printf(“n”); } printf(“nB matricesn”); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(” %d”,b[i][j]); printf(“n”); } for(i=0;i<3;i++) { for(j=0;j<3;j++) s[i][j]=a[i][j]+b[i][j]; }
  • 32. printf(“nnsum of two matrices Sn”); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(” %d”,s[i][j]); printf(“n”); } for(i=0;i<3;i++) { for(j=0;j<3;j++) c[i][j]=a[i][j]-b[i][j]; } printf(“nnminus of two matrices Cn”); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(” %3d”,c[i][j]); printf(“n”); } getch(); } Share this: Share Like this:
  • 33. Like One blogger likes this post. This entry was posted in Uncategorized by fuadahmed. Bookmark the permalink. Leave a Reply Enter your comment here... Theme: Twenty Eleven | Blog at WordPress.com. Follow Follow “Fuad Ahmed” Get every new post delivered to your Inbox. Powered by WordPress.com