208 Programming and Data Structures
Month [41 of a year contains 30 days.
Month I S] of a year contains 31 days.
Month [6] of a year contains 30 days.
Month [7] of a year contains 31 days.
Month [8] of a year contains 31 days.
Month [9] of a year contains 30 days.
Month [10] of a year contains 31 days.
Month [11] of a year contains 30 days.
Month [12] of a year contains 31 days.
Explanation In the above example, one dimensional array month [12] is initialized with number of
days of different months of a year from 1 to 12 as per their order. In pr in f () function number of days
of the months are printed. The value of % i ' is increased by one for obtaining the increasing order of
month of a year.
7.13 Write a program to display the number of days of given month of a year.
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main ()
{
in t month[12]={1,3,5,7,8,10,12,4,6,9,11,2};
int i,mn;
clr s c rO ;
printf ("Enter Number of Month
scanf ("%d",&cmn);
for (t=0,i<=ll;i++)
I
if(mn~month[i])
goto compare;
I
printf ("n Invalid Month");
exit(l);
compare:;
if(i+ l~ 12)
printf ("Month (%d) Contains 28 days.",month[i]);
if(i+l<8)
printf ("Month (%d) Contains 31 days.",month[i]);
i/(i+2>7&&i+1/=12)
printf ("Month (%d) Contains 30days.”,month[i]);
getcheO;
}
OUTPUT:
Enter Number of Month: 2
Month (2) Contains 28 days.
Arrays 209
Explanation This program is slightly different as compared to the last one. The number of days of
different months of a year are sorted. For example, first seven (1,3,5,7,8,10,12) elements of an array
have month numbers having 31 days, next four 30 days and last one 28 days.
The user enters the month number and i f statement checks where this month number appears in the
array. Whenever there is a match control goes to the compare statements. The i f statements prints the
number of days depending upon the conditions stated as above.
7.14 Write a program to find the average sales of an item out of 12 months sale.
xnain()
{
flo a t sum=0,avg=0;
In t sa le ;
In t item [12];
c lr s c r O ;
printf (“ Enter Month No.-Sale of an Itemhnonth ");
For (sale=0;sale<=ll;sale++)
printf (" %d = ",sale+l);
scatnf("%d",&item[sale]);
I
for (sale=0,sale<=ll;sale++)
sum=sum+item[sale];
avg=sumll2;
printf (" t Average Sale of an item lmonth-%f'ravg);
I
OUTPUT ;
Enter Month No.*Sale of an Item/month
1 = 125
2 = 225
3 = 325
4 = 425
5 = 525
6 = 625
7 = 725
8 = 825
9 = 925
10 = 500
11 = 600
12 = 700
Average Sale of an item /month= 543.750000
Explanation In the above program, sales of 12 months are entered and stored in an array item [12].
By using the f o r loop sum of sales of 12 months is calculated. Average of sales is then computed and
the result is displayed.
210 Programming and Data Structures
7.15 Wiite a progtam to find the similar elements in an array and find the occurrence of
bimilar numbers for number of times.
mixiQ
{
int k»0, j » l ,i ;
int it«m[5];
clrscrO ;
printf (tt Enter Numbers. ");
for (i=0,-i<=4^++)
(
printf ("t %d = ",i);
scanf("%dn,&dtem[i]);
I
for(j=0,j<=4,j++)
I
for (i=j+l,n<=4,n++)
/
if(item[j]==item[i])
I
printf ("n t%d %d Elements are same.
I
}
}
if(k~10)
printf ("n All elements are same");
else if (k~ 0)
printf ("n No Elements are same");
else
printf (" Elements Contains Double numbers");
I
QUIFUJ
Enter Numbers
0 = 5
1 = 8
2 = 3
3= 5
4 = 9
0 3 Elements Are Same.
Elements Contain Double numbers.
Explanation This program checks similar elements in the same array and displays element numbers
having same values. In this program, three for loops are used. The first for loop is initialized for
reading the numbers. The second and third fo r loops are used for finding similar numbers. The i f
statement compares elements of an array with one another. When the similar numbers are found
element numbers are printed and counter %k' is increased. Depending upon the value of 'k' i f
statement displays the respective message.
Arrays 211
7.16 Write a program to calculate & display total cost of 4 models of Pentium PCs. Use the
single dimension arrays for PC codes their price and quantity available.
# include <stdio.h>
# include <conio.h>
void main ()
{
int i,pccode[4]={l,2,3,4}?
long t=0,price[4]={25000,30000,35000,40000};
int stock[4]={25,20,15,20}?
clrscrO ?
printf (" Stock & Total Cost Details n");
pnnf/7"===========— ==========================m");
printf ("Model Qty.tRate (Rs.) Total Value");
p r i» ^ (" « = = = = = = = = = = = = = = = = = = = s = = = a s s a o r= = s = = = s = = = " );
for (i=0;i<=3;i++)
I
printf (" Pentium%d %d %8ld %15ld ",pccode[i],stock[i],price[i],price[i]*stock[i]);
t=t+price[i]*stock[i];
I
pm j//r/ ============================sa«*========w");
printf ("Total Value of All PCs in Rs. %ld",t);
printf f" =======================================fi,/);
I
OUTPUT:
Stock & Total Cost Details
Model Qty. Rate(Rs.) Total Value
Pentiuml 25
25000
625000
Pentium2 20
30000
600000
Pentium3 15
35000 525000
Pentium4 20
40000 800000
Total Value of All PCs in Rs.2550000
Explanation
Here in the above program, three integer arrays
pccode
[ ],
price
[ ]
and
sto ck [
] are initialized. After this the for loop is used for finding the total value of the available stock of each
model. Result is printed by using simple multiplication of price [ ] and stock [ ]. This product
is then added to variable ' t ', which is the total value. The for loop is executed for four times. At the
end, variable ' t ' gives us the total cost of all PCs. Total cost is printed with pr i n t f () statement.
7.17 Write a program to display the given message by using putcO and stdouH) functions.
#include <stdio.h>
#include <conio.h>
main(void)
/
..................Content has been hidden....................

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