GIVE DETAILED CODE AND ANSWER IN LANGUAGE C. Write a program that will first rea
ID: 3807569 • Letter: G
Question
GIVE DETAILED CODE AND ANSWER IN LANGUAGE C.
Write a program that will first read in a positive integer number, say w, from standard input. Then you need to create enough space in the heap via a call to malloc to hold an array of n integers. You then need to use array index operators to fill the array with n integer values entered by the user via calls to scanf. The index operator is also referred to as a subscript operator (i.e., A[i]). After the array has been filled with numbers, your program should walk down the array and build their sum. Print out the sum.Explanation / Answer
#include <stdio.h>
#include <malloc.h>
int main()
{
int sum = 0;
int n,i;
printf("Ente how many numbers do you want to enter: ");
scanf("%d", &n);
int *arr =(int*) malloc(n * sizeof(int));
printf("Enter %d values: ", n);
for(i=0; i<n; i++){
scanf("%d", &arr[i]);
}
for(i=0; i<n; i++){
sum = sum + arr[i];
}
printf("Sum is %d ", sum);
return 0;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Ente how many numbers do you want to enter: 5
Enter 5 values:
1
2
3
4
5
Sum is 15
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.