All submissions

Calculate Array Sum Using Only Pointers

Code

#include <stdio.h>

int calculate_sum(int *ptr, int n) {
    int sum =0;
    for(int i =0; i<n ;i++)
        sum+= *ptr++;
    return sum;
}

int main() {
    int n;
    scanf("%d", &n);

    int arr[100];
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    int result = calculate_sum(arr, n);
    printf("%d", result);

    return 0;
}

Solving Approach

 

 

 

Loading...

Input

5 1 2 3 4 5

Expected Output

15