\" C programming \" Write a program that accepts four (4) int values and display
ID: 3599183 • Letter: #
Question
" C programming "
Write a program that accepts four (4) int values and displays the sum of
the largest and smallest of the values.
Mean, Lab for Repetetion
Write a program that accepts a sequence of positive integers from the user
(n > 0). The first non-positive value (n 0) terminates the sequence. The terminating value (n 0) is not included in the mean.
Display the mean value of the series. Note that the mean value of a sequence
of n integers a, a2…an is
and note that the mean value may not be an integer.
Explanation / Answer
(i)
#include <stdio.h>
int main() {
int num[20]; //array to store numbers
int n=4; // here number of inputs is 4
int i,j,k,max=0,min=0; //at first initialize max number and min number to zero
for(i=0;i<n;i++)
{
scanf("%d",&num[i]); // take inputs
}
for(j=0;j<n;j++)
{
if(num[j]>max)
{
max=num[j]; //gives maximun value
}
}
for(k=0;k<n;k++)
{
if(num[k]<min)
{
min=num[k]; //gives minimum value
}
}
printf("sum of largest and smallest value is %d",max+min); //print sum of max and min
return 0;
}
(ii)
#include <stdio.h>
int main() {
int num[100]; // array to store numbers
int i,j;
int n=0,sum=0; //initialize count of numbers and initial sum to zero
float mean;
while(1) // loop continues until break is encountered
{
scanf("%d",&num[i]);
if(num[i]<0) //if value is negative stop inputs
{
break;
}
++i;
++n; // increment count of numbers
}
for(j=0;j<n;j++)
{
sum=sum+num[j]; //get total sum
}
mean=sum/n; //calculate mean as division between total sum and count of numbers
printf("Mean value is %f",mean);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.