0

The below code will print the sum of elements in the array passed as the argument.Define the method printArrayElementsSum

int printArrayElementsSum(long int arr[],int N)
{
    long int sum=0;
    long int len = sizeof(arr)/sizeof(arr[0]);
    for(int i=0;i<len;i++)
    sum+=arr[i];
    printf("%lld",sum);
    
    return sum;
    
    
}
int main()
{
    int N;
    scanf("%d",&N);
    int arr[N], index;
    for(index=0; index < N; index++){
        scanf("%d",&arr[index]);
    }
    printArrayElementsSum(arr,N);
    return 0;
}

If the input is

5
1 2 3 4 5

i need the output as 15 But the above code giving me wrong answer What mistake i have done please rectify it And the arr element is >100000

Develper
  • 57
  • 3
  • 11
  • Here's a hint: you are not using your `N` parameter – AndyG Jun 16 '21 at 15:51
  • 1
    *What mistake I have done please rectify it* -Debugging your own code is part and parcel of learning how to write programs. Just writing a program, seeing that it doesn't produce the correct results, and then asking someone else (StackOverflow members in this case) to debug your code is not how it's supposed to work. – DeBARtha Jun 16 '21 at 15:54
  • Would you mind selecting *one of * C and C++, which are different languages? – MikeCAT Jun 16 '21 at 15:56
  • Which language, C or C++? They are distinct languages. For example, C++ has `std::accumulate` for summing sequences, whereas C doesn't. Please adjust your tags accordingly. – Thomas Matthews Jun 16 '21 at 16:03

0 Answers0