| | |
Sum of Positive and Negative numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 7
Reputation:
Solved Threads: 0
Hi all,
Ok, so I am new to C++ and whilst I understand most of the theory, I am still struggling with the practical applications! I'm trying to write a program that will allow a user to enter 10 whole numbers, then return the sum of all 10 numbers, the sum of the positive numbers and the sum of the negative. At the moment I have:
Obviously the sum of negative and positive are missing. I know I need some kind of loop that will check if 'int one. . .ten' is positive or negative and add their values to two new totals, but I can't seem to figure out the structure.
Any help would be great. I'm not looking for the code, just someone to point me in the right direction.
Many thanks.
Ok, so I am new to C++ and whilst I understand most of the theory, I am still struggling with the practical applications! I'm trying to write a program that will allow a user to enter 10 whole numbers, then return the sum of all 10 numbers, the sum of the positive numbers and the sum of the negative. At the moment I have:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int one, two, three, four, five, six, seven, eight, nine, ten; int count; cout << "This program accepts 10 integer numbers, the returns\n"; cout << "the sum of all positive numbers, the sum of all negative\n"; cout << "numbers and the sum of all the numbers." << endl; cout << "Enter 10 whole numbers, each followed by 'Enter': \n "; cin >> one >> two >> three >> four >> five >> six >> seven >> eight >> nine >> ten; cout << endl; cout << "The sum of all 10 numbers = "; cout << one + two + three + four + five + six + seven + eight + nine + ten; cout << endl; return 0; }
Obviously the sum of negative and positive are missing. I know I need some kind of loop that will check if 'int one. . .ten' is positive or negative and add their values to two new totals, but I can't seem to figure out the structure.
Any help would be great. I'm not looking for the code, just someone to point me in the right direction.
Many thanks.
Last edited by cscgal; Oct 12th, 2008 at 5:26 am. Reason: Added code tags
Its better if you have an array of 10 ints and then put down a for loop to check whether its positive or negative and add them up.
If you have variables like this., IT WILL TAKE A LONG TIME TO WRITE IF LOOPS. AND THEN ADD.
to check if its negative or positive.
you can use.
If you have variables like this., IT WILL TAKE A LONG TIME TO WRITE IF LOOPS. AND THEN ADD.
to check if its negative or positive.
you can use.
C++ Syntax (Toggle Plain Text)
if(var<0) { cout<<"negative"; } else { cout<<"POSITIVE"; }
Last edited by Sky Diploma; Oct 12th, 2008 at 5:22 am.
•
•
Join Date: Sep 2008
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Hi all,
Ok, so I am new to C++ and whilst I understand most of the theory, I am still struggling with the practical applications! I'm trying to write a program that will allow a user to enter 10 whole numbers, then return the sum of all 10 numbers, the sum of the positive numbers and the sum of the negative. At the moment I have:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int one, two, three, four, five, six, seven, eight, nine, ten; int count; cout << "This program accepts 10 integer numbers, the returns\n"; cout << "the sum of all positive numbers, the sum of all negative\n"; cout << "numbers and the sum of all the numbers." << endl; cout << "Enter 10 whole numbers, each followed by 'Enter': \n "; cin >> one >> two >> three >> four >> five >> six >> seven >> eight >> nine >> ten; cout << endl; cout << "The sum of all 10 numbers = "; cout << one + two + three + four + five + six + seven + eight + nine + ten; cout << endl; return 0; }
Obviously the sum of negative and positive are missing. I know I need some kind of loop that will check if 'int one. . .ten' is positive or negative and add their values to two new totals, but I can't seem to figure out the structure.
Any help would be great. I'm not looking for the code, just someone to point me in the right direction.
Many thanks.
http://www.hitmill.com/programming/cpp/forLoop.htm
http://www.cplusplus.com/doc/tutorial/arrays.html
It would be good if you learned about looping as tomhogans mentioned, also try and learn arrays, it would make the program easier to read as you wont have so many variables.
To get you started I suggest you try and write it out on paper its not too difficult, in order to calculate the totals of the positive or negative try and divide the number by 2 and if it returns a 0 then its positive else its negative.
To get you started I suggest you try and write it out on paper its not too difficult, in order to calculate the totals of the positive or negative try and divide the number by 2 and if it returns a 0 then its positive else its negative.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
•
•
Join Date: Oct 2008
Posts: 32
Reputation:
Solved Threads: 4
Here's the code XD
I wouldn't know where to start if I didn't know arrays and the logic of array indexes.
When you state
Any questions feel free to pm me. These forums are very active and messages can get lost very easily inside threads.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int numbers[10]; int poscount=0, negcount=0, totalcount=0; cout << "This program accepts 10 integer numbers, the returns the sum of all positive\n"; cout << "numbers, the sum of all negative numbers and the sum of all the numbers." << endl << endl; for( int i=0; i<10; i++) { cout << "Enter integer number " << i+1 << ", followed by 'Enter': ", i; cin >> numbers[i]; if( numbers[i] > 0 ) poscount += numbers[i]; else negcount += numbers[i]; } cout << endl; cout << "Sum of positive numbers: " << poscount << endl; cout << "Sum of negative numbers: " << negcount << endl; cout << "Sum of all numbers: " << poscount + negcount << endl; system("pause>nul"); return 0; }
When you state
int number; you're asking for the computer to allocate enought memory for 1 integer number to be stored. But when you say int numbers[10]; you're asking for enough space to be allocated for 10 integers, all ten with the tag numbers but you can tell them apart by the index. You really have 10 variables but you can tell them apart by the index number. So you will have 1 integer in numbers[0] through numbers[9]. The max the index number goes for that array is 9, not 10. Then to print out cout << "Enter integer number " << i+1 << ", followed by 'Enter': ", i; we're using the index number to show a different number, in this case how many integers have been input so far.Any questions feel free to pm me. These forums are very active and messages can get lost very easily inside threads.
Last edited by emotionalone; Oct 13th, 2008 at 4:44 am.
![]() |
Similar Threads
- multiply 2 BigDecimal numbers (Java)
- A few noob questions (C++)
- need help adding positive and negative numbers (C++)
- veena and other people again help (Visual Basic 4 / 5 / 6)
- Help with C++ code (C++)
- the type def statement and enum (C++)
- 8085 microprocessor ASM language question (Assembly)
- StringTokenizer??? NEED HELP PLEASE!!! (Java)
Other Threads in the C++ Forum
- Previous Thread: While loop error
- Next Thread: how can we sort data from text file
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





