Trouble with a C++ Program

Started by
32 comments, last by M2tM 13 years, 6 months ago
I would be extremely grateful if anyone helped me with this problem. Heres what I have to do.

Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero (which will be a negative number or zero) the sum of all the numbers less than zero, and the sum of all the numbers whether positive negative or zero. The user enters in 10 numbers just once each and the user can enter them in any order. Your program cannot ask the user to enter the positive numbers and negative numbers separately.

I got no idea what to do, If initialize 10 separate integer variables I don't know how I would get the program to differentiate between the positive and negative numbers without using an extremely long series of if statements. Please help
If any of you guys are c programmers, I would be extremely grateful if you helped me with this problem. Heres what I have to do. Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero (which will be a negative number or zero) the sum of all the numbers less than zero, and the sum of all the numbers whether positive negative or zero. The user enters in 10 numbers just once each and the user can enter them in any order. Your program cannot ask the user to enter the positive numbers and negative numbers separately. I got no idea what to do, If initialize 10 separate integer variables I don't know how I would get the program to differentiate between the positive and negative numbers without using an extremely long series of if statements. Please help
Advertisement
Someone......please......help me
CS 101 -- Arrays
Well we don't do homework. Also be patient, you don't need to post within 10 minutes. Go open your C++ book and read it. Does it explicitly tell you that you cannot use if's? and how you gotta write a ton of if's when you only need two?
I smell an assignment question...

I doubt you'll get tonnes of help, or someone to write it for you.

To achieve your goal i'd use: Arrays, for loops and if statements.
Woah, I wasn't asking for you guys to do my homework, I was asking for help on how I would approach this problem. I don't know how to use arrays, but I will look into that, thank you. And I am sorry I spam posted, I'm to used to people ignoring my posts for days on end.
Google these:

int
for
if
std::cin
std::cout

The general pseudo-code for your program might look like this:

//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL POSITIVE NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NEGATIVE NUMBERS
//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
//LOOP END
//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT

You need to learn how to break problems down into discreet steps like this if you plan on programming at all. Then what you do is take these steps and convert them into code. If you can do this in your head, that's great. If you cannot then you will want to write some pseudo-code or a flowchart to help understand the problem.

As far as I can tell the problem does not warrant an array unless you are also expected to output the numbers individually later on. You can keep a running total in the input loop and avoid the need for a vector (or array). Maybe I'm missing something though.

If it helps, you can try copy/pasting my pseudo code into your editor and directly translating from English into C++ deleting the comments one at a time as you do so.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
Quote:Woah, I wasn't asking for you guys to do my homework, I was asking for help on how I would approach this problem. I don't know how to use arrays, but I will look into that, thank you. And I am sorry I spam posted, I'm to used to people ignoring my posts for days on end.


Just to clarify for you, thats not how your OP read.

As i mentioned in my post, if people here even percieve its an assignment or homework question, the quality of answers goes down.

Looking into arrays will be a great start to resolving your problem, and learning about for loops will also help with what you hope to achieve.

Once you have some code put together, put it in the thread here as another post, and get comments on it, at a glance what you want to do should be around 10-20 lines, including whitespace at a beginner level.

Quote:As far as I can tell the problem does not warrant an array unless you are also expected to output the numbers individually later on. You can keep a running total in the input loop and avoid the need for a vector (or array). Maybe I'm missing something though.


it will depend on any other info that he's not provided, but you're right, array's may not be required, i expected that when someone says 'read ten numbers then output the values' that its stored as those ten values.

The rest of the brief doesnt make that a requirement so arrays may not be necessary.
Quote:Original post by M2tM
Google these:

int
for
if
std::cin
std::cout

The general pseudo-code for your program might look like this:

//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL POSITIVE NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NEGATIVE NUMBERS
//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
//LOOP END
//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT

You need to learn how to break problems down into discreet steps like this if you plan on programming at all. Then what you do is take these steps and convert them into code. If you can do this in your head, that's great. If you cannot then you will want to write some pseudo-code or a flowchart to help understand the problem.

As far as I can tell the problem does not warrant an array unless you are also expected to output the numbers individually later on. You can keep a running total in the input loop and avoid the need for a vector (or array). Maybe I'm missing something though.

If it helps, you can try copy/pasting my pseudo code into your editor and directly translating from English into C++ deleting the comments one at a time as you do so.


No, I don't need to show the numbers entered in again. Just the the sum of the negative and zero numbers, positive and zero numbers and all the numbers together.
Quote:Original post by Blckknight118
Quote:Original post by M2tM
Google these:

int
for
if
std::cin
std::cout

The general pseudo-code for your program might look like this:

//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL POSITIVE NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NEGATIVE NUMBERS
//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
//LOOP END
//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT

You need to learn how to break problems down into discreet steps like this if you plan on programming at all. Then what you do is take these steps and convert them into code. If you can do this in your head, that's great. If you cannot then you will want to write some pseudo-code or a flowchart to help understand the problem.

As far as I can tell the problem does not warrant an array unless you are also expected to output the numbers individually later on. You can keep a running total in the input loop and avoid the need for a vector (or array). Maybe I'm missing something though.

If it helps, you can try copy/pasting my pseudo code into your editor and directly translating from English into C++ deleting the comments one at a time as you do so.


No, I don't need to show the numbers entered in again. Just the the sum of the negative and zero numbers, positive and zero numbers and all the numbers together.


You are being overly-specific when you say the sum of positive and zero numbers... The sum of a number and zero is the number. You can just say: the positive number sum, the negative number sum, and all numbers summed.

In any case, good luck, you've got a fairly precise set of instructions provided if you have further issues follow up in this thread. In the future make posts in the "For Beginners" section instead of General Programming which is usually for more complicated problems.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk

This topic is closed to new replies.

Advertisement