4
1. Write a program to process 10 collections of daily high temperatures. Your program should count and print : - the number of hot days (85 or higher), - the number of pleasant days (60-84), and - the number of cold days (less than 60). It should also display the category of each temperature. At the end of the run, display the average temperature. Without array Using Array (the same output) #include<stdio.h> #define SIZE 5 int main(void) { int hot=0, pleasant=0, cold=0; int sum=0, i, temp; double average; for(i=0; i<SIZE; i++) { printf("Enter temperature %d > ",i+1); scanf("%d",&temp); sum +=temp; if(temp >= 85) { printf("%d, is a hot day\n",temp); ++hot; } else if(temp >= 60 && temp <= 84) { printf("%d, is a pleasant day\n",temp); ++pleasant; } else { printf("%d, is a cold day\n",temp); ++cold; } } average = (double) sum/SIZE; printf("The collection of hot days is %d\n",hot); printf("The collection of pleasant days is %d\n",pleasant); printf("The collection of cold days is %d\n",cold); printf("The average temperature is %.2f\n",average); return(0); } #include<stdio.h> #define SIZE 5 int main(void) { int hot=0, pleasant=0, cold=0; int sum=0, i, temp[SIZE]; double average; for(i=0; i<SIZE; i++) { printf("Enter temperature %d > ",i+1); scanf("%d",&temp[i]); sum +=temp[i]; if(temp[i] >= 85) { printf("%d, is a hot day\n",temp[i]); ++hot; } else if(temp[i] >= 60 && temp[i] <= 84) { printf("%d, is a pleasant day\n",temp[i]); ++pleasant; } else { printf("%d, is a cold day\n",temp[i]); ++cold; } } average = (double)sum/SIZE; printf("The collection of hot days is %d\n",hot); printf("The collection of pleasant days is %d\n",pleasant); printf("The collection of cold days is %d\n",cold); printf("The average temperature is %.2f\n",average); return(0); } temp[0] temp[1] temp[2] temp[3] temp[4] 55 80 87 40 70

chapter8-appendix1

Embed Size (px)

Citation preview

Page 1: chapter8-appendix1

1. Write a program to process 10 collections of daily high temperatures. Your program should count and print:  - the number of hot days (85 or higher), - the number of pleasant days (60-84), and - the number of cold days (less than 60).

It should also display the category of each temperature. At the end of the run, display the average temperature.

 

Without array Using Array (the same output) #include<stdio.h> #define SIZE 5 int main(void) { int hot=0, pleasant=0, cold=0; int sum=0, i, temp; double average; for(i=0; i<SIZE; i++) { printf("Enter temperature %d > ",i+1); scanf("%d",&temp); sum +=temp; if(temp >= 85) { printf("%d, is a hot day\n",temp); ++hot; } else if(temp >= 60 && temp <= 84) { printf("%d, is a pleasant day\n",temp); ++pleasant; } else { printf("%d, is a cold day\n",temp); ++cold; } } average = (double) sum/SIZE; printf("The collection of hot days is %d\n",hot); printf("The collection of pleasant days is %d\n",pleasant); printf("The collection of cold days is %d\n",cold); printf("The average temperature is %.2f\n",average); return(0); }

#include<stdio.h> #define SIZE 5 int main(void) { int hot=0, pleasant=0, cold=0; int sum=0, i, temp[SIZE]; double average; for(i=0; i<SIZE; i++) { printf("Enter temperature %d > ",i+1); scanf("%d",&temp[i]); sum +=temp[i]; if(temp[i] >= 85) { printf("%d, is a hot day\n",temp[i]); ++hot; } else if(temp[i] >= 60 && temp[i] <= 84) { printf("%d, is a pleasant day\n",temp[i]); ++pleasant; } else { printf("%d, is a cold day\n",temp[i]); ++cold; } } average = (double)sum/SIZE; printf("The collection of hot days is %d\n",hot); printf("The collection of pleasant days is %d\n",pleasant); printf("The collection of cold days is %d\n",cold); printf("The average temperature is %.2f\n",average); return(0); }

temp[0] temp[1] temp[2] temp[3] temp[4]

55 80 87 40 70

Page 2: chapter8-appendix1

2. Write a method/function CalPower(BaseNumber, exponent) that calculates and returns the value of BaseNumberexponent. For example, CalPower(3,4)=3*3*3*3. The method/function should accept a data of type int and return a value of type int. You should not use any method/function from the class Math 

#include<stdio.h> int CalPower(int BaseNumber, int exponent); int main(void) { int x, y, total; printf("Enter the base number > "); scanf("%d",&x); printf("Enter the exponent number > "); scanf("%d",&y); total = CalPower(x,y); printf("%d exponent %d is %d\n",x,y,total); return(0); } int CalPower(int BaseNumber, int exponent) { int i, result =1; for(i=0; i<exponent; i++) result = result *BaseNumber; return(result); }

Page 3: chapter8-appendix1

3. Write a for loop that sums the even values from LIST_SIZE element array list.  

W/O function call Array Arguments #include<stdio.h> #define SIZE 6 int main(void) { int list[SIZE],i,sum=0; for(i=0; i<SIZE; i++) { printf("Enter value %d ",i+1); scanf("%d",&list[i]); } for(i=0;i<SIZE;i++) if(list[i]%2==0) sum += list[i]; printf("The sum of the even numbers = %d\n",sum); }

#include<stdio.h> #define SIZE 6 void fun(int list[]); int main(void) { int x[SIZE],i; for(i=0; i<SIZE; i++) { printf("Enter value %d ",i+1); scanf("%d",&x[i]); } fun(x); return (0); } void fun(int list[]) { int i, sum = 0; for(i=0;i<SIZE;i++) if(list[i]%2==0) sum += list[i]; printf("The sum of the even numbers = %d\n",sum); }

#include<stdio.h> #define SIZE 6 int fun(int *list); int main(void) { int x[SIZE], i , total; for(i=0; i<SIZE; i++) { printf("Enter value %d ",i+1); scanf("%d",&x[i]); } total = fun(x); printf("The sum of the even numbers = %d\n",total); return (0); } int fun(int *list) { int i, sum = 0; for(i=0;i<SIZE;i++) if((list[i]%2)==0) //if(*(list+i)%2)==0) sum += list[i]; //sum += *(list+i); return (sum); }

x[0] x[1] x[2] x[3] x[4] x[5]

30 12 51 17 45 62

 

list

Page 4: chapter8-appendix1

 4. Write a program that allows the user to enter number of students and their grade, then it search for a 

particular student grade by entering the number of any student and display either the grade of the student or the student no found. 

#include<stdio.h> #define SIZE 5 int main(void) { int ID[SIZE], mark[SIZE],i,key; for(i=0; i<SIZE; i++) { printf("Enter number of student %d > ",i+1); scanf("%d",&ID[i]); printf("Enter mark for %d > ",ID[i]); scanf("%d",&mark[i]); } printf("Enter number of student to see his grade "); scanf("%d",&key); for(i=0 ; i<SIZE; i++) if(ID[i] == key) { printf("The grade for student %d is %d", key, mark[i]); break; } if(i == SIZE) printf("This number %d is not found ",key); return (0); }

#include<stdio.h> #define SIZE 5 #define NOT_FOUND -1 int serach(const int no[ ], int target, int n); int main(void) { int ID[SIZE], mark[SIZE], i, key, index; for(i=0; i<SIZE; i++) { printf("Enter number of student %d > ", i+1); scanf("%d",&ID[i]); printf("Enter mark for %d > ",ID[i]); scanf("%d",&mark[i]); } printf("Enter number of student to see his grade "); scanf("%d",&key); index = serach(ID, key, SIZE); if(index != -1) printf("The grade for student %d is %d\n", key, mark[index]); else printf("This number %d is not found\n ",key); return(0); } int serach(const int no[ ],int target,int n) { int i, found = 0, where; i=0; while(!found && i < n) if(no[i] == target) found = 1; else ++i; if(found) where=i; else where = NOT_FOUND; return (where); }