Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

CHECK IT

write a program that prints the sum and the count of all odd numbers from an input set of n integers...CAN YOU CHECK IT? OTHER SUGGESTIONS TO IMPROVE MY PROGRAM.? <<<THANK YOU>>>

[code]
#include
using namespace std;
main ()
{ int x,n,sum, count;
sum=0;
count=0;
cout<<"Enter the number of integer";
cin>>n;

for(i=1; i<=n;i++)
{cout<<"Enter an number";
cin>>x;
if(x%2==1)
{ sum+=x;
count++;}
}
cout<<"sum="<<sum<<"
";
cout<<"count="<<count<<endl;
return 0;
}

[/code]

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : write a program that prints the sum and the count of all odd numbers
    : from an input set of n integers...CAN YOU CHECK IT? OTHER
    : SUGGESTIONS TO IMPROVE MY PROGRAM.? <<<THANK YOU>>>
    :
    : [code]:
    : #include
    : using namespace std;
    : main ()
    : { int x,n,sum, count;
    : sum=0;
    : count=0;
    : cout<<"Enter the number of integer";
    : cin>>n;
    :
    : for(i=1; i<=n;i++)
    : {cout<<"Enter an number";
    : cin>>x;
    : if(x%2==1)
    : { sum+=x;
    : count++;}
    : }
    : cout<<"sum="<<sum<<"
    ";
    : cout<<"count="<<count<<endl;
    : return 0;
    : }
    :
    : [/code]:
    :

    Other than the small typo at oistream which should be iostream, the code looks good.
    But, I'm thinking perhaps you're not fully doing what the exercise asks. It says "... from an [b]input set[/b] of n integers". This makes me think that you need to prompt the user for the amount of integers to be input (the 'n') and then for n integers input by the user. And from that list, calculate the count and sum of the numbers.
    This also because one can directly calculate the amount of odd integers in the range 1 to n: count = n/2 + n%2;
    The sum will be equal to: count * count

    Anyway, good luck
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • trebla2352trebla2352 Member Posts: 46
    but the problem showed that the user must input n number of integers, and x will be the number which will be determined if it is an odd or not, then if the statement satisfies, the count will be the counter of all odd numbers and the odd sum will be the sum of all odd nmbers pass to for loop?
    what is count=n/2 + n%2 that you said? thank you for you suggestion

    is my flow of the program correct?<<thanks
  • LundinLundin Member Posts: 3,711
    : This also because one can directly calculate the amount of odd integers in the range 1 to n: count = n/2 + n%2;

    That implies that all numbers are adjacent. It doesn't seem to be the case here.

    So as far as I can see, the program is correct, apart from the declaration of main() which isn't allowed to be implicit in neither the C nor the C++ language. Correct form is

    int main()

    Also, the code needs proper indention and spaces / blank lines, so it turns more readable.
  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : : This also because one can directly calculate the amount of odd integers in the range 1 to n: count = n/2 + n%2;
    :
    : That implies that all numbers are adjacent. It doesn't seem to be
    : the case here.
    :

    Hmm... yess... you know what. I'm being stupid here. Nvm my post(s)

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.