prob1-w3
unknown
plain_text
2 years ago
617 B
3
Indexable
#include <stdio.h> #include <malloc.h> int main(){ int *A, N, temp; int sum = 0; printf("Number of elements: "); scanf("%d", &N); // dynamic memory allocation for pointer array A = (int*) malloc(N * sizeof(int)); // input array elements for (int i = 0; i < N; i++){ printf("Input element %d: ", i + 1); scanf("%d", &temp); *(A + i) = temp; } printf("Original array:\n"); // compute the sum using pointer operator for (int i = 0; i < N; i++){ sum += *(A + i); printf("Element %d: %d\n", i+1, *(A + i)); } printf("Sum of array: %d", sum); free(A); return 0; }
Editor is loading...