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

int main(void) void fun(int list[]); int fun(int *list);


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

for(i=0;i<SIZE;i++) for(i=0;i<SIZE;i++)
if(list[i]%2==0) if((list[i]%2)==0) //if(*(list+i)%2)==0)
sum += list[i]; sum += list[i]; //sum += *(list+i);
return (sum);
printf("The sum of the even numbers }
= %d\n",sum);
}

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


30 12 51 17 45 62

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

S-ar putea să vă placă și