Exercise 1: Practice to store and print data in an array
a) Write a C program that reads marks of 10 students in to 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.
#include<stdio.h>
int main(){
int arr[10];
printf("Enter 10 elements into the array. The values must be between 0 and 20:\n");
int i,n, d, index =0;
for(i=0; i<10; i++){
printf("Element %d: ", (i+1));
scanf("%d", &arr[i]);
if(arr[i]>0 && arr[i]<= 20){
index++;
}
}
printf("%d\n", index);
int arr2[index], k=0;
for(n=0; n<i; n++){
if(arr[n]>0 && arr [n]<=20){
arr2[k] = arr[n];
k++;
}
}
printf("The elements of the array that are in the range of 0 and 20 are: ");
for(d=0; d<index; d++){
printf("%d ", arr2[d]);
}
}
Comments
Leave a comment