Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,233 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,771 people online right now. Registration is fast and FREE... Join Now!




Finding average

 
Reply to this topicStart new topic

Finding average

deangelobrooks
15 Mar, 2007 - 08:17 PM
Post #1

New D.I.C Head
*

Joined: 15 Mar, 2007
Posts: 10


My Contributions
Hi guys i need help on a program please.
I was asked to write a program for class where it ask the user to enter 10 numbers of any choice whether negative or positive. at the end it should display the sum of all negative numbers and the sum of all positive numbers. then also display the average of the negative numbers and average of positve numbers.

This was all i came up with, it worked but i cant find the average:
CODE
#include <iostream>
using namespace std;
int main()
{
    int sum_greater_than_zero = 0, num_greater_than_zero= 0, num_less_than_zero= 0, sum_less_than_zero = 0;
    int counter = 0;
    int numbers;
    int average;
        while(counter < 10)
    {
        cout << "Please enter Number\n";
        cin >> numbers;
        counter++;

    
        if (numbers <= 0 )
            sum_less_than_zero += numbers;
        else if ( numbers > 0 )
            sum_greater_than_zero += numbers;
        
    }
    cout << "The Sum of all negative numbers are" << sum_less_than_zero<< "\n";
    cout << "The Sum of all positive numbers are" << sum_greater_than_zero<< "\n";
    cout << "The Average of Negative numbers are" << average<< "\n";
            cin >> numbers;
    return 0;
}

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Finding Average
15 Mar, 2007 - 08:26 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
you need two more counters... one to count up each time you find a number < zero and one for when you find a number >= zero... then you can find the average by dividing by the right counter... make sure you check to see if it is zero though, else you get that pesky division by zero!.
User is offlineProfile CardPM
+Quote Post

deangelobrooks
RE: Finding Average
15 Mar, 2007 - 08:45 PM
Post #3

New D.I.C Head
*

Joined: 15 Mar, 2007
Posts: 10


My Contributions
QUOTE(NickDMax @ 15 Mar, 2007 - 09:26 PM) *

you need two more counters... one to count up each time you find a number < zero and one for when you find a number >= zero... then you can find the average by dividing by the right counter... make sure you check to see if it is zero though, else you get that pesky division by zero!.




How do i do that Nick..
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Finding Average
15 Mar, 2007 - 10:18 PM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(deangelobrooks @ 16 Mar, 2007 - 10:15 AM) *

QUOTE(NickDMax @ 15 Mar, 2007 - 09:26 PM) *

you need two more counters... one to count up each time you find a number < zero and one for when you find a number >= zero... then you can find the average by dividing by the right counter... make sure you check to see if it is zero though, else you get that pesky division by zero!.




How do i do that Nick..



well have counter say minus_count
now

CODE

if (numbers <= 0 )
{
   sum_less_than_zero += numbers;
   minus_counter++;
}
else if ( numbers > 0 )
{
   sum_greater_than_zero += numbers;  
}

minus_avg = sum_less_than_zero / minus_counter;
plus_avg = sum_greater_than_zero / (counter - minus_counter);



do check for "divide by zero error".
hope this will help you. wink2.gif

This post has been edited by AmitTheInfinity: 15 Mar, 2007 - 10:19 PM
User is offlineProfile CardPM
+Quote Post

deangelobrooks
RE: Finding Average
16 Mar, 2007 - 09:51 AM
Post #5

New D.I.C Head
*

Joined: 15 Mar, 2007
Posts: 10


My Contributions
Thanks Amit.. I'm gonna try it see if it works ok...
User is offlineProfile CardPM
+Quote Post

deangelobrooks
RE: Finding Average
19 Mar, 2007 - 03:20 PM
Post #6

New D.I.C Head
*

Joined: 15 Mar, 2007
Posts: 10


My Contributions
I did all that but i had a problem when i set it minus_counter = 0.. it showed error...
can you check to see what i did wrong please..

CODE

#include <iostream>
using namespace std;
int main()
{
    int sum_greater_than_zero = 0, num_greater_than_zero= 0, num_less_than_zero= 0, sum_less_than_zero = 0;
    int counter = 0;
    int numbers;
    int minus_avg;
    int plus_avg;
    int minus_counter = 0;
        while(counter < 10)
    {
        cout << "Please enter Number\n";
        cin >> numbers;
        counter++;

    
        if (numbers <= 0 )
            sum_less_than_zero += numbers;
        else if ( numbers > 0 )
            sum_greater_than_zero += numbers;
    
    
    }
        if (numbers <= 0 )
{
   sum_less_than_zero += numbers;
   minus_counter++;
}
else if ( numbers > 0 )
{
   sum_greater_than_zero += numbers;  
}

minus_avg = sum_less_than_zero / minus_counter;
plus_avg = sum_greater_than_zero / (counter - minus_counter);

    cout << "The Sum of all negative numbers are" << sum_less_than_zero<< "\n";
    cout << "The Sum of all positive numbers are" << sum_greater_than_zero<< "\n";
    cout << "The Average of Negative numbers are" << minus_avg<< "\n";
    cout << "The Average of Positive numbers are" << plus_avg<< "\n";
            cin >> numbers;
    return 0;
}

User is offlineProfile CardPM
+Quote Post

cire_1803
RE: Finding Average
19 Mar, 2007 - 05:58 PM
Post #7

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 43


My Contributions
This is my revised code.
I used num_less_than_zero to count number less than zero and num_greater_than_zero to count numbers greater than zero. I made some changes in this code. you use minus_counter as the divisor. I change it to this way. And i goes good. Just try it

CODE
minus_avg = sum_less_than_zero / num_less_than_zero;
plus_avg = sum_greater_than_zero / num_greater_than_zero;


Main program;
CODE
#include <iostream>
using namespace std;
int main()
{
int sum_greater_than_zero = 0, num_greater_than_zero= 0, num_less_than_zero= 0, sum_less_than_zero = 0;
int counter = 0;
int numbers;
int minus_avg;
int plus_avg;
int minus_counter = 0;

while(counter < 10) {
cout << "Please enter Number\n";
cin >> numbers;
counter++;

if (numbers <= 0 )
{
sum_less_than_zero += numbers;
num_less_than_zero++;
}
else if ( numbers > 0 )
{
sum_greater_than_zero += numbers;
num_greater_than_zero++;
}

}

minus_avg = sum_less_than_zero / num_less_than_zero;
plus_avg = sum_greater_than_zero / num_greater_than_zero;

cout << "The Sum of all negative numbers are" << sum_less_than_zero<< "\n";
cout << "The Sum of all positive numbers are" << sum_greater_than_zero<< "\n";
cout << "The Average of Negative numbers are" << minus_avg<< "\n";
cout << "The Average of Positive numbers are" << plus_avg<< "\n";
cin >> numbers;
return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:44PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month