Assignments#05
Assignments#05
Due date May 08,2010 Program 1 Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. First the program should ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period. Input validation: Do not accept a number less than 1 for the number of years. Do not accept negative numbers for the monthly rainfall.
SOLUTION :
#include<stdio.h> void main() { float sum,avrg,rainfall; int years,x,y; clrscr(); printf("Enter number of years \n"); scanf("%d",&years); if(years<1) { printf("Invalid entry please enter more than zero\n"); scanf("%d",&years); } printf("Total number of months are \n"); y=years*12; printf("%d",y); for(x=0;x<=years;x++) { for(x=0;x<years*12;x++) { printf("\nEnter rainfalls for the whole year (inches) \n"); scanf("%f",&rainfall); printf("The total rainfall for the year is= \n"); sum=sum+rainfall;
printf("%f",sum); printf("\n"); avrg=sum/y; printf("The average rainfall for each month is %f\n",avrg); } getch(); }
Program 2 Kilims are woven rugs which generally use symmetrical designs. For this assignment, instead of using a loom, wool, and a shuttle, we are asking you to produce a pattern using for loops and nested for loops. Here is the pattern:
SOLUTION :
#include<stdio.h> void main(void) { int a=1,b=1; clrscr(); for(a=1;a<=36;a++) { printf("*"); } printf("\n"); for(a=1;a<=6;a++) { printf("***"); printf( ); } printf("\n"); for(a=1;a<=6;a++) { printf("***"); printf( ); } printf("\n");
for(a=1;a<=6;a++) { printf( ); printf("***"); } printf("\n"); for(a=1;a<=6;a++) { printf( ); printf("***"); } printf("\n"); for(a=1;a<=36;a++) { printf("*"); } printf("\n\t\t"); for(a=1;a<=4;a++) { for(b=1;a<=4-a;b++) { printf(" "); }
for(b=1;b<=(a*2)-1;b++) { printf("*"); } printf("\n\t\t"); } for(a=3;a>=1;a--) { for(b=1;b<=4-a;b++) { printf(" "); } for(b=1;b<=(a*2)-1;b++) { printf("*"); } printf("\n\t\t"); } printf("\n"); for(a=1;a<=36;a++) { printf("*"); }
printf("\n"); for(a=1;a<=6;a++) { printf( ); printf("***"); } printf("\n"); for(a=1;a<=6;a++) { printf( ); printf("***"); } printf("\n"); for(a=1;a<=6;a++) { printf("***"); printf( ); } printf("\n"); for(a=1;a<=6;a++) { printf("***"); printf( );