array simple
Submit solution
C
Points:
15 (partial)
Time limit:
5.0s
Memory limit:
64M
Authors:
Problem type
Allowed languages
Task: Read an array of n elements, find the sum of all array elements.
sample input:
5 //Enter the no of elements:
10 //element1
20 //element2
30 //element3
40 //element4
50 //element5
sample output:
The array elements are 10,20,30,40,50,
sum is 150
Comments
include<stdio.h>
int main() { int size,i,sum=0; printf("Enter the size of the array :\n"); scanf("%d",&size); int arr[size]; printf("Enter the elements of the array :\n"); for(i=0;i<size;i=i+1) { scanf("%d",&arr[i]); } printf("The sum of elements of the array is :\n"); for(i=0;i<size;i=i+1) { sum=sum+arr[i]; } printf("%d",sum); return 0; }
Why is this described as the incorrect solution ?
include <stdio.h>
int main() { int n,sum,i; scanf("%d",&n); int arr[n]; sum=0; for (i=0;i<n;i++) { scanf("%d",&arr[i]); } printf ("The array elements are "); for (i=0;i<n;i++) { printf ("%d,",arr[i]); } for (i=0;i<n;i++) { sum = sum + arr[i]; } printf("\n the sum is %d",sum); return 0; } //SHOWING THIS SOLUTION AS WRONG.WHY? //PLEASE CORRECT THIS