Q1) Write ONE program that initializes an array of integers of size 10. The inte
ID: 640222 • 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:
Print the elements.
Find and print the sum of the integers in the array.
Find and print the largest and the smallest of the integers.
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 intialize an array of size 8 with floating point numbers entered by the user.
Print the array.
Find the average of the elements and print it.
Find and print all the elements that has a value higher than the average
*Write both programs using C language.
Explanation / Answer
//Q1) Write ONE program that initializes an array of integers of size 10. The integers are entered by the user and does the below:
#include <stdio.h>
int main()
{
int numbers[10]; // array of integers of size 10
int i = 0;
int sum = 0, max, min;
// The integers are entered by the user
while (i < 10)
{
printf ("Enter number %d: ", (i + 1));
scanf("%d", &numbers[i]);
i++;
}
// Print the elements.
i = 0;
max = numbers[0];
min = numbers[0];
while (i < 10)
{
printf ("Number %d is %d ", (i + 1), numbers[i]);
sum = sum + numbers[i];
if (max < numbers[i])
max = numbers[i];
if (min > numbers[i])
min = numbers[i];
i++;
}
// print the sum of the integers in the array.
printf ("Sum is %d ", sum);
// print the largest and the smallest of the integers.
printf ("Largest is %d ", max);
printf ("Smallest is %d ", min);
// Change the value of the fourth element in the array to the product of the fifth and the seventh elements and print the result.
numbers[3] = numbers[4] * numbers[6];
i = 0;
while (i < 10)
{
printf ("Number %d is %d ", (i + 1), numbers[i]);
i++;
}
return 0;
}
// Q2) Write a program to intialize an array of size 8 with floating point numbers entered by the user.
#include <stdio.h>
int main()
{
float numbers[8]; // array of integers of size 10
int i = 0;
float sum = 0.0, max, min;
// The integers are entered by the user
while (i < 10)
{
printf ("Enter number %d: ", (i + 1));
scanf("%f", &numbers[i]);
i++;
}
// Print the elements.
i = 0;
max = numbers[0];
min = numbers[0];
while (i < 10)
{
printf ("Number %d is %f ", (i + 1), numbers[i]);
sum = sum + numbers[i];
if (max < numbers[i])
max = numbers[i];
if (min > numbers[i])
min = numbers[i];
i++;
}
// print the sum of the integers in the array.
printf ("Sum is %f ", sum);
// print the largest and the smallest of the integers.
printf ("Largest is %f ", max);
printf ("Smallest is %f ", min);
// Change the value of the fourth element in the array to the product of the fifth and the seventh elements and print the result.
numbers[3] = numbers[4] * numbers[6];
i = 0;
while (i < 10)
{
printf ("Number %d is %f ", (i + 1), numbers[i]);
i++;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.