Answer to Question #249894 in C++ for Hazzy

Question #249894

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.


1
Expert's answer
2021-10-12T00:37:21-0400
#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]);
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS