Exercise 1: Practice to store and print data in an array
a) Write a C program that reads marks of 10 students into a single subscripted array.
b) Above marks should be between 0 to 20. Modify the above program to add marks to the array only if the input mark is between the given range.
c) Display the values stored in the array.
Exercise 2: Practice accessing data stored in an array
Modify the above program to find the mean of the marks stored in the array.
#include <stdio.h>
int main()
{
int arr[10];
int n;
printf("\nEnter elements of the array:\n");
for (int i = 0; i < 10; i++) {
scanf("%d",&n);
while (n < 0 || n > 20) {
printf("Range is 0 to 20. Re-enter.");
scanf("%d",&n);
}
arr[i] = n;
}
printf("\nElements of the array\n");
for (int i = 0; i < 10; i++) {
printf("%d\t",arr[i]);
}
int sum=0;
for (int i = 0; i < 10; i++) {
sum+=arr[i];
}
float mean=sum/10.0;
printf("\nMean= %f",mean);
return 0;
}
Comments
Leave a comment