vendredi 26 juillet 2019

Why this code is doing wrong calculations?

When tried to debug this code I noticed that the value of sum as mentioned in code tend to give wrong results. I cant understand why this is happening. I know I would get a lot of downvotes and my question too would be closed, but I'm in dire need of solution...

I've already tried debugging through the code and can't understand what's wrong with the code

#include <iostream>
using namespace std;

int main()
{
long long int t,j,n,l,count=0,sum=0;
cin>>t;
string s;
//char i;
while(t>0)
{
    l=0;
    sum=0;
   count=0;
    cin  >>  s;
    n= s.length();
    for(j=0;j<n;j++)
    {
        if(s[j]=='a' || s[j]=='e' || s[j]=='i' || s[j]=='o' || s[j]=='u' || s[j]=='A'|| s[j]=='E' || s[j]=='I' ||s[j]=='O' || s[j]=='U')
        {
              count++;
             l=j;

             cout<<"l="<<j<<endl;
             cout<<"j="<<j<<endl;
             cout<<"n="<<n<<endl;
             cout<<"n-1="<<n-1<<endl;
             cout<<"j+1="<<j+1<<endl;

            sum=sum+((n-l)*(j+1));

            cout<<"SUM is="<<sum << endl;
        }
    }
     if(l==0)
        {
            sum=0;
        }
        cout<<"Second part Sum="<<endl;
    cout<<sum<<endl;
    t--;
}

}

Program is working correctly and efficiently, No error messages, it's running well but there seems to be a dilemma about the calculations of sum...[![enter image description here][1]][1]

Aucun commentaire:

Enregistrer un commentaire