C Programming: Kireshvanth.B 20Z326

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Name : Kireshvanth.

B
Roll no : 20Z326

C PROGRAMMING
9.28 ) Describe the array that is defined in each of the following
statements. Indicate what values are assigned to the individual array
elements.
Solutions:
a) c is a floating point and one dimensional array.
The elements in the array c are:
c[0]=2.
c[1]=5.
c[2]=3.
c[3]=-4.
c[4]=12.
c[5]=12.
c[6]=0.
c[7]=8.

b) c is a one dimensional floating array with 8 elements.


The elements are:
c[0]=2.
c[1]=5.
c[2]=3.
c[3]=-4.
c[4]=0.
c[5]=0.
c[6]=0.
c[7]=0.
c) z is a one dimensional integer array with 12 elements.
The elements are:
z[0]=0
z[1]=0
z[2]=8
z[3]=0
z[4]=0
z[5]=6
z[6]=0
z[7]=0
z[8]=0
z[9]=0
z[10]=0
z[11]=0
d) flag is a one dimensional character array with 4
elements.
The elements are:
flag[0]=’T’
flag[1]=’R’
flag[2]=’U’
flag[3]=’E’

e) flag is a one dimensional character array with 5


elements.
The elements are:
flag[0]=’T’
flag[1]=’R’
flag[2]=’U’
flag[3]=’E’
flag[4]=0

f) flag is a one dimensional character array with 5


elements.
The elements are:
flag[0]=’T’
flag[1]=’R’
flag[2]=’U’
flag[3]=’E’
flag[4]=’\0’

g) flag is a one dimensional character array with 6


elements.
The elements are:
flag[0]=’F’
flag[1]=’A’
flag[2]=’L’
flag[3]=’S’
flag[4]=’E’
flag[5]=’\0’

h) p is a two dimensional integer array of 2x4 elements.


The elements are :
P[0][0]= 1
P[0][1]= 3
P[0][2]= 5
P[0][3]= 7
P[1][0]= 0
P[1][1]= 0
P[1][2]= 0
P[1][3]= 0

i) p is a two dimensional integer array of 2x4 elements.


The elements are :
P[0][0]= 1
P[0][1]= 1
P[0][2]= 3
P[0][3]= 3
P[1][0]= 5
P[1][1]= 5
P[1][2]= 7
P[1][3]= 7

j) p is a two dimensional integer array of 2x4 elements.


The elements are :
P[0][0]= 1
P[0][1]= 3
P[0][2]= 5
P[0][3]= 7
P[1][0]= 2
P[1][1]= 4
P[1][2]= 6
P[1][3]= 8

k) p is a two dimensional integer array of 2x4 elements.


The elements are :
P[0][0]= 1
P[0][1]= 3
P[0][2]= 0
P[0][3]= 0
P[1][0]= 5
P[1][1]= 7
P[1][2]= 0
P[1][3]= 0
l) c is a three dimensional integer array of 2x3x4
elements.
The elements are :
c[0][0][0]= 1
c[0][0][1]= 2
c[0][0][2]= 3
c[0][0][3]= 0
c[0][1][0]= 4
c[0][1][1]= 5
c[0][1][2]= 0
c[0][1][3]= 0
c[0][2][0]= 6
c[0][2][1]= 7
c[0][2][2]= 8
c[0][2][3]= 9
c[1][0][0]= 10
c[1][0][1]= 11
c[1][0][2]= 0
c[1][0][3]= 0
c[1][1][0]= 0
c[1][1][1]= 0
c[1][1][2]= 0
c[1][1][3]= 0
c[1][2][0]= 12
c[1][2][1]= 13
c[1][2][2]= 14
c[1][2][3]= 0
m) colors is a two dimensional character array of 3x6
elements.
The elements are :
colors[0][0]=’R’
colors[0][1]=’E’
colors[0][2]=’D’
colors[0][3]=0
colors[0][4]=0
colors[0][5]=0
colors[1][0]=’G’
colors[1][1]=’R’
colors[1][2]=’E’
colors[1][3]=’E’
colors[1][4]=’N’
colors[1][5]=0
colors[2][0]=’B’
colors[2][1]=’L’
colors[2][2]=’U’
colors[2][3]=’E’
colors[2][4]=0
colors[2][5]=0
9.31. Describe the output generated by each of the following
programs:
a)

Output:
20
Finding the sum of even numbers in the array.

b)

Output :
25
Finding the sum of odd numbers in the array.
c)

Output :
45
Finding the sum of all elements in the array.

d)

Output :
25
Finding the sum of odd numbers in the array, which is
initialized externally.
e)

Output :
1
Finding the smallest value .

f)
Output :
159
Finding the smallest value in each row.

g)

Output :
9 10 11 12
Finding the largest value in each column.
h)
Output :
0224
4668
8 10 10 12
Finding an odd element and reducing its value by 1 and
printing the other elements as such in array.
i)
Output :
PPoorrmmiiggwwtt aa eeggeettffnn
Printing the odd numbered array twice by skipping the
even numbered array.

9.39 . Write a C program that will enter a line of text, store it in an


array and then display it backwards. Allow the length of the line to
be unspecified (terminated by pressing the Enter key), but assume
that it will not exceed 80 characters.
Test the program with any line of text of your own choosing.
Compare with the program given in Example 7.15,which makes use
of recursion rather than an array. Which approach is better, and
why?

Source Code:
#include <stdio.h>
#include <string.h>
int main()
{
char s[80];

printf("Enter a string to reverse :\n");


gets(s);

strrev(s);

printf("Reverse of the string is : \n%s", s);


return 0;
}

Output :
On using recursion the program runs faster and uses less
memory allocations.

9.43.

Source Code :

#include<stdio.h>
#include<math.h>
void equ(int a);
int main(){

int t;
printf("Enter the value of t : ");
scanf("%d",&t);

for (int i=0 ; i<=t ;i++){


equ(i);
}

void equ(int a){

printf("For t = %d , y = %0.2f\n",a,2*exp(-0.1 * a)*sin(0.5 * a));

}
Output :

You might also like