mardi 30 juin 2020

Why am I getting the wrong output for this C++ code? (one of the problem of hackerrank)

This is the program for printing out sum of array elements. It is showing run time error. The output is coming out to be 0 instead of printing out the sum of the elements.

#include<iostream.h>
using namespace std;
void simpleArraySum()   
{
    int ar[100],n,i,sum=0;

    for(i=0;i<n;i++)
    {
        sum=sum + ar[i];
    }

    cout<<sum;
}
int main()
{
    int ar[100],n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
    }

    simpleArraySum();
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire