Sign in to follow this  
Blckknight118

Trouble with a C++ Program

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?

Share this post


Link to post
Share on other sites
Moved to For Beginners.

Quote:
Original post by Blckknight118
Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


No, because you don't need to "hold" them all.

You are going to have some sort of loop that reads each number from the standard input. Each time, you read it into the same variable, check the sign of the value, and update the appropriate running totals.

Think of how you would do it if you had to do the math yourself, in your head, as someone reads the numbers off to you, one at a time. Would you bother to remember what numbers were given to you previously? No; you have enough to think about as it is. Instead you would just keep track of the sums and the most recent number, so that you can do the addition.

Share this post


Link to post
Share on other sites
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.


Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


An array is a collection of variables. Typically in C++ you use std::vector instead of bare arrays... Why would you need to store a single number in several different variables?

Try walking through the pseudo-code as if you're actually running the program.

//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

Here we've just created 3 variables of type int and set them to 0 to store the grand totals.

//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter 2

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)0 + 2 = 2.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
Yes, the number is greater than 0, so our "TOTAL OF POSITIVE NUMBERS" is 0 + 2 = 2

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
This is not true for 2 and so "TOTAL OF NEGATIVE NUMBERS" stays the same.

//LOOP END
Ok, so we hit the loop end, but because we want to do this 10 times we'll go back to the first step in the instruction (I'm copying and pasting the inner part of the loop again):

////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter -5 this time

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)2 + -5 = -3.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
No, the new number is not greater than 0 and so our "POSITIVE NUMBERS TOTAL" stays at 2

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
Yes, our temporary number is less than 0 and so we get "NEGATIVE NUMBER TOTAL"(0) + -5 = -5


//LOOP END
Ok, we're at the end of the loop again, we'll do one more run through in this example:

////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter 12 this time

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)-3 + 12 = 9.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
Yes, the new number is greater than zero, so we add it to our "POSITIVE NUMBER TOTAL"(2) + 12 = 14

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
No, this is not true so we don't do anything and the value still holds -5.


//LOOP END
Ok, I'm done with my example, but imagine we do this 7 more times. Now we exit the loop.



//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT
Alright, this is the last step in the pseudo code, so we output:
"Positive Number Total = 14, Negative Number Total = -5, Total of All Numbers = 9"

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Moved to For Beginners.

Quote:
Original post by Blckknight118
Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


No, because you don't need to "hold" them all.

You are going to have some sort of loop that reads each number from the standard input. Each time, you read it into the same variable, check the sign of the value, and update the appropriate running totals.

Think of how you would do it if you had to do the math yourself, in your head, as someone reads the numbers off to you, one at a time. Would you bother to remember what numbers were given to you previously? No; you have enough to think about as it is. Instead you would just keep track of the sums and the most recent number, so that you can do the addition.


But the program needs ten different values from the user. Aren't I supposed to initialize ten separate int variables to be manipulated. How can the user enter in ten numbers if none of them are stored? Sorry, I'm just really confused with this. I understand how a for loop works, I just don't understand how it does in a situation where the user inputs ten separate numbers when you can only use one variable in the statement of the loop.

Share this post


Link to post
Share on other sites
Quote:
Original post by M2tM
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.


Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


An array is a collection of variables. Typically in C++ you use std::vector instead of bare arrays... Why would you need to store a single number in several different variables?

Try walking through the pseudo-code as if you're actually running the program.

//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

Here we've just created 3 variables of type int and set them to 0 to store the grand totals.

//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter 2

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)0 + 2 = 2.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
Yes, the number is greater than 0, so our "TOTAL OF POSITIVE NUMBERS" is 0 + 2 = 2

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
This is not true for 2 and so "TOTAL OF NEGATIVE NUMBERS" stays the same.

//LOOP END
Ok, so we hit the loop end, but because we want to do this 10 times we'll go back to the first step in the instruction (I'm copying and pasting the inner part of the loop again):

////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter -5 this time

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)2 + -5 = -3.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
No, the new number is not greater than 0 and so our "POSITIVE NUMBERS TOTAL" stays at 2

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
Yes, our temporary number is less than 0 and so we get "NEGATIVE NUMBER TOTAL"(0) + -5 = -5


//LOOP END
Ok, we're at the end of the loop again, we'll do one more run through in this example:

////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter 12 this time

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)-3 + 12 = 9.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
Yes, the new number is greater than zero, so we add it to our "POSITIVE NUMBER TOTAL"(2) + 12 = 14

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
No, this is not true so we don't do anything and the value still holds -5.


//LOOP END
Ok, I'm done with my example, but imagine we do this 7 more times. Now we exit the loop.



//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT
Alright, this is the last step in the pseudo code, so we output:
"Positive Number Total = 14, Negative Number Total = -5, Total of All Numbers = 9"


OMG THAT MAKES SO MUCH DAMN SENSE.

Thank you so much, that just cleared so much, everything clicked. I don't know how to thank you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Blckknight118
Quote:
Original post by Zahlman
Moved to For Beginners.

Quote:
Original post by Blckknight118
Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


No, because you don't need to "hold" them all.

You are going to have some sort of loop that reads each number from the standard input. Each time, you read it into the same variable, check the sign of the value, and update the appropriate running totals.

Think of how you would do it if you had to do the math yourself, in your head, as someone reads the numbers off to you, one at a time. Would you bother to remember what numbers were given to you previously? No; you have enough to think about as it is. Instead you would just keep track of the sums and the most recent number, so that you can do the addition.


But the program needs ten different values from the user. Aren't I supposed to initialize ten separate int variables to be manipulated. How can the user enter in ten numbers if none of them are stored? Sorry, I'm just really confused with this. I understand how a for loop works, I just don't understand how it does in a situation where the user inputs ten separate numbers when you can only use one variable in the statement of the loop.


Ok, let's try this even a step slower.

You have a variable, why is it called a variable? Because it can change!

What does the following program do?

int x;
x = 2;
cout << "The variable contains: " << x << endl;
x = 5;
cout << "The variable contains: " << x << endl;
x = 20;
cout << "The variable contains: " << x << endl;
x = 5;
cout << "The variable contains: " << x << endl;

Assuming you have this within your main function this should output:
The variable contains: 2
The variable contains: 5
The variable contains: 20
The variable contains: 5

What if we do this:

int x;
cin >> x;
cout << "You just typed: " << x << endl;
cin >> x;
cout << "You just typed: " << x << endl;
cin >> x;
cout << "You just typed: " << x << endl;
cin >> x;
cout << "You just typed: " << x << endl;

We are using the same variable, but we've asked the user to enter 4 numbers. We don't need a different variable for each number because we only need to use that number for a single line after, we can then re-assign x later on and re-use the variable.

Alright, let's try something else now, previous example:

int x;
int Total = 0;
cin >> x;
Total+=x;
cout << "You just typed: " << x << " The running total is: " << Total << endl;
cin >> x;
Total+=x;
cout << "You just typed: " << x << " The running total is: " << Total << endl;
cin >> x;
Total+=x;
cout << "You just typed: " << x << " The running total is: " << Total << endl;
cin >> x;
Total+=x;
cout << "You just typed: " << x << " The running total is: " << Total << endl;

That's cool, but we're doing the exact same thing several times, we can put that into a for loop!

Your assignment now is to take the code I wrote above and put it into a for loop that runs 10 times. Then turn that into the assignment you had.

Share this post


Link to post
Share on other sites
Quote:
Original post by Blckknight118
Quote:
Original post by M2tM
...


OMG THAT MAKES SO MUCH DAMN SENSE.

Thank you so much, that just cleared so much, everything clicked. I don't know how to thank you.


No problem, I'm glad to help :D

Share this post


Link to post
Share on other sites
Ok so I thought I had and I understand, but I don't know how to implement it.

Here's my code thus far.

#include <iostream>
using namespace std;

int main() {


cout << " Welcome to Whole Number Extravagenza! \n";
cout << " Please enter a number 10 numbers to be calculated ";
cout << " Pleae enter each number followed by a space ";

int num_totalall;
int num_negativetotal;
int num_positivetotal;

int num;

cout << " Please enter in 10 numbers ";
This is where I get tripped up. What about all ten of the values that are to be entered in? How are the supposed to be tested to see if they are greater than or less than zero?
for(num = 0; num > 10; num++) Here I try to get the loop to go ten times
{ I don't know what to put here.. Would it be num+=num_total?

Share this post


Link to post
Share on other sites
Quote:
Original post by Blckknight118
Ok so I thought I had and I understand, but I don't know how to implement it.

Here's my code thus far.

#include <iostream>
using namespace std;

int main() {


cout << " Welcome to Whole Number Extravagenza! \n";
cout << " Please enter a number 10 numbers to be calculated ";
cout << " Pleae enter each number followed by a space ";

int num_totalall;
int num_negativetotal;
int num_positivetotal;

int num;

cout << " Please enter in 10 numbers ";
This is where I get tripped up. What about all ten of the values that are to be entered in? How are the supposed to be tested to see if they are greater than or less than zero?
for(num = 0; num > 10; num++) Here I try to get the loop to go ten times
{ I don't know what to put here.. Would it be num+=num_total?
First, you should initialize your variables to a known value.

Second, you need to collect the data from the user. num is not data collected from the user. You need a cin in there somewhere. To be clear, you need 5 variables (3 to hold the totals, 1 to hold the data you get from the user, and a final one to maintain your position in the for loop).

Third, once you collect data from the user you want to check whether that data matches the necessary condition. If the data matches the necessary condition for a running total then you'll want to add it to the running total. Otherwise do not add it to the running total.

Hope this helps.

Share this post


Link to post
Share on other sites
Ok this is the code I got. I don't know why it isn't working...

#include <iostream>
using namespace std;

int main() {


cout << " Welcome to Whole Number Extravagenza! \n";
cout << " Please enter a number 10 numbers to be calculated ";
cout << " Pleae enter each number followed by a space ";

int num_totalall;
int num_negativetotal;
int num_positivetotal;

int num_loop;
int num;

cin >> num;

for(num_loop= 0; num_loop >= 10; num_loop++)
{
if( num >= 0)
{ num + num_positivetotal; }

}

for(num_loop= 0; num_loop >= 10; num_loop++)
{
if( num <= 0)
{ num + num_negativetotal; }

}

cout << num_negativetotal;

return 0;

}

Share this post


Link to post
Share on other sites
your thinking about it the wrong way... you need to follow a slightly different logic...


Loop start:
read input

if input > 0
positiveSum += input

if input < 0
negativeSum += input

end loop:

output sums



not

read input

loop:
if input > 0
positiveSum += input
end loop:

loop:
if input < 0
negativeSum += input
end loop:

output sums



you also need a store for the value outside of the loops.

Share this post


Link to post
Share on other sites
I was going to post a nice, readable, commented solution. But where's the fun in that?
#include<iostream>
int main(){int _[2]={0},_l,_1=10;while(_1--)std::cin>>_l,_[_l>0]+=_l;std::cout<<*_<<'\n'<<1[_]<<'\n'<<*_+1[_]<<'\n';}

Share this post


Link to post
Share on other sites
#include <iostream>
using namespace std;

int main() {


cout << " Welcome to Whole Number Extravagenza! \n";
cout << " Please enter a number 10 numbers to be calculated ";
cout << " Pleae enter each number followed by a space ";

int num_totalall = 0;
int num_negativetotal = 0;
int num_positivetotal = 0;

int num_loop;
int num;

cin >> num;

for(num_loop= 0; num_loop >= 10; num_loop++)
{
if(num >= 0)
num += num_positivetotal;

if(num <= 0)
num += num_negativetotal;
}

cout << num_negativetotal;
return 0;

}

Ok I definitely don't understand why this isn't working. I have the two nested if loops

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this