prob1-w3

mail@pastecode.io avatar
unknown
plain_text
a year ago
617 B
1
Indexable
Never
#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;
}