Arrays 203
char f;
clrscrO;
puts ("Enter a String:");
gets (s);
puts("Enter a Character to Find
f=getchar();
for (i=0,H<=15,n++)
I
if(s[i]~f)
C + + ;
I
printf ("The Character (%c) in a String (%s) occurs %d times ,f,s,c);
I
PITIFUL
Enter a String: programmer
Enter a Character to Find: r
The Character (r) in a String (programmer) occurs (3) times.
Explanation In this program the string and a single character is entered through the keyboard.
Inside the for loop, i f statement checks each element of the string for occurrence of the single entered
character. If the character ( ' r ') is found then c counter is increased otherwise without increasing the
counter the loop continues till' i ' reaches to 15. At last the value of c gives the total occurrence of
given character.
7.7 Write a program to display the elements of two arrays in two separate columns and add
their corresponding elements. Display the result of addition in the 3rd column.
mainO
{
in t i,num[]={24,34,12,44,56,17};
int numl[]={12,24,35,78,85,22};
c lrs c rO ;
printf ("Element of Array 1st- 2nd Array Addtion ");
for (i=0,n<=Sd++)
printf ("nt %d + %d = %d",num[i],numl[i],num[i]+numl[i]);
I
O U TPUT;
Element of Array 1st - 2nd Array Addition
24 + 12 = 36
34 + 24 = 58
12 + 35 = 47
44 + 78 = 122
56 + 85 =141
17 + 22 = 39
204 Programming and Data Structures
Explanation In the above program two integer arrays are initialized and the corresponding elements
of arrays are added through simple arithmetic operation.
7.8 Write a program to fcnter character & integer data type. Use two-dimensional array.
Perform and display the addition of three numbers.
Tips: The Unsigned character data type is capable to perform mathematical Operations on
numbers from 1 to 255.
# include <stdio.h>
# include <conio.h>
void mainO
{
static unsigned char l,r,i,real[3] [5] ,ima[3] [5];
long c;
clrscrO;
fo r (l=0;l<3;l++)
I
printf (“Enter Number[%d] :",l+l);
scanf ("% Id",&cc);
r=d255;
i=c%255;
realM5]=r;
ima[l][5]=i;
I
for (l-0;l<3;l++)
c=c+real[l][5]*255+ima[l][5];
printf ("nSum of 3 Numbers :%3ld”,c);
getchO;
}
QtJTEUTj
Enter Number [1]: 5
Enter Number [2]: 4
Enter Number [3]: 3
Sum of 3 Numbers : 12
Explanation The unsigned character data type is ranging from 0 to 2 5 5. In the above example, three
numbers are entered. They are divided and mod operation is carried with 2 5 5. If the numbers are less
than 255 neither division nor mod operations are carried out. Their sum is evaluated and displayed on
the screen. In case the entered numbers are greater than 255, real and imaginary parts are computed
and stored in the separate arrays. To obtain the whole number the real part is multiplied with 255 and
added to imaginary part.
Arrays 205
7.9 Write a program to accept any string up to 15 characters. Display the elements of string
with their element numbers in a separate column.
# include <stdio.h>
# include <conio.h>
# include <process.h>
void mainO
{
static char name [15];
int i;
clrscrO;
printf ("Enter your name
gets(name);
printf ("Element no. &' Character ");
for (i=0,-i<=15p++)
I
if(name[i]=='0r)
exitfl);
printf("n %d t%c",i,name[i]);
)
OUTPUT;
Enter your name: amit
1 m
Explanation The string elements are entered through the keyboard. The fo r loop starts from 'O'.
The value o f' i ' is the element number and argument is provided with an arrays name [ ] towards
printing the string element numbers and string characters.
7.10 Write a program to display names of days of a week using single dimensional array
having length of 7. ( Week having 7 days)
# include <;Stdio.h>
# include cconio. h>
void main ()
{
int day [7] ,i;
clrscrO ;
printf (" Enter numbers between 1 to7:n");
for (i=0,n<=6,i++)
scanf ("%d",&day[i]);
206 Programming and Data Structures
for (i=0;i<=6/i++)
switch(day[i])
{
}
}
case 1:
printf (" %dst day of week is Sunday"rday[i]);
break;
case 2;
printf ("n%dnd day of week is Monday"rday [i]);
break;
case 3:
printf (" %drd day of week is Tuesday",day [i]);
break;
case 4:
printf ("n%dth day of week is Wednesday",day [i]);
break;
case 5;
printf ("n%dth day of week is Thursday ",day[i]);
break;
case 6:
printf ("n%dth day of week is Friday", day [i]);
break;
case 7:
printf ("n%dth day of week is Saturday", day [i]);
break;
default:
printf ("n %dth is Invalid day",day[i]);
QUTfUT.;
Enter numbers between 1 to 7:1324578
1st day of week is Sunday
3rd day of week is Tuesday
2nd day of week is Monday
4th day of week is Wednesday
5th day of week is Thursday
7th day of week is Saturday
8th is invalid day.
Explanation In the above example, depending upon the value entered by the user, the swi tc h ()
statement decides which day to print.
Arrays 207
7.11 Write a program to display the contents of two arrays. The 1M array should contain the
string and 2nd numerical numbers.
#include <stdio.h>
#include <conio.h>
mainO
{
char city[6] «{'M', 'A', 'N', 'D', 'K', 'D'}/
int i,pin[6]-{4,3,l,6,0,3};
clrscrO;
for (i=0,i<6i++)
printf ("%c"/nty[i]);
printf
for (i=0,H<6,i++)
printf ("%d",pin[i]);
Q u m K i
NANDED - 431603
Explanation In the above example, two arrays of different data types are printed throughpr in t f ()
function. The two fo r loops are used for printing two arrays containing the first string and the second
numerics.
7.12 Write a program to display the number of days of different months of year.
# include <stdio.h>
# include <conio.h>
# Include <process.h>
void main ()
in t month[12] =.{31,28,31,30,31,30,31,31,30,31,30,31};
int i;
c lrs c rO ;
for(i=0,n<=llr'i++)
I
printf("n Month [%d] of a year contains %d days. ,i+ljnonth[I]);
printf(“n");
)
getcheO;
I
OUTPUT;
Month [1] of a year contains 31 days.
Month [2] of a year contains 28 days.
Month 13] of a year contains 31 days.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.147.193