Q1) Write ONE program that initializes an array of integers of size 10. The inte
ID: 665023 • Letter: Q
Question
Q1) Write ONE program that initializes an array of integers of size 10. The integers are entered by the user and does the below:
a)Print the elements.
b)Find and print the sum of the integers in the array.
c)Find and print the largest and the smallest of the integers.
d)Change the value of the fourth element in the array to the product of the fifth and the seventh elements and print the result.
Q2) Write a program to initialize an array of size 8 with floating point numbers entered by the user.
e)Print the array.
f)Find the average of the elements and print it.
Find and print all the elements that has a value higher than the average.
Explanation / Answer
// function main begins program execution
main()
{
int i; // counter
int n[10]; // n is an array of 10 integers
int sum = 0; // sum of the array
int smallest, largest;
int r;
printf( "Enter 10 numbers: " );
scanf( "%d", &n[10] );
// initialise elements of array to 0
for ( i = 0; i < 10; i++ )
{
n[i] = 0; // set element at location i to 0
} // end for
// sum of contents of array n
for ( i = 0; i < 10; i++ )
{
sum += n[i];
} // end for
// Largest number in array n
for(i=0;i<n;i++)
scanf("%d",&n[i]);
big=n[0];
for(i=1;i<n;i++){
if(big<n[i])
big=n[i];
}
printf("Largest element: %d",big);
// Smallest number in array n
small=n[0];
for(i=1;i<n;i++){
if(small>n[i])
small=n[i];
}
printf("Smallest element: %d",small);
printf( "The sum of array n is %f ", sum );
printf( "The smallest number of array n is %f ", smallest );
printf( "The largest number of array n is %f ", largest );
return 0; // indicates successful termination
} // end function main
Q2 #include <stdio.h>
int main(){
int n, i;
float num[8]
float sum=0.0, average,Avg_value;
printf("Enter the numbers of data: ");
scanf("%d",&n);
for(i=0; i<n; ++i)
{
printf("%d. Enter number: ",i+1);
scanf("%f",&num[i]);
sum+=num[i];
}
average=sum/n;
printf("Average = %.2f",average);
Avg_value = average;
for(i=0; i<n; ++i)
if(Avg_value<n[i])
Avg_value=n[i];
}
printf("Larger than average: %d",small);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.