# Thread: noob c ++ question. Summations

1. ## noob c ++ question. Summations

Hi I am in a c ++ class and I had a question. For homework I have to calculate the total resistance for an electrical circuit. The question is is that the resistances are arbitrary so there can be say 100 resistances in the circuit or say 15 resistances in the circuit. I have to ask the user to input how many resistances there are in the circuit and then from there, the program has to ask the user for all of the individual values of each of the resistances and then take the sum of all the resistances. How would I do that? Any help at all would be much appreciated. Thanks.

2. you should at least try to write some code.

3. Is it your first class?

4. Do you know of std::vector? Are you allowed to use it?

5. Originally Posted by Elysia
Do you know of std::vector? Are you allowed to use it?
Oh please...

I doubt he is allowed to use it, especially if he is a newbie.

6. As good time as any to learn it. Not all universities forbid the use of things not yet taught.

7. I don't think I am not allowed to not use anything, so anything that would work would be ok. I don't have that particular code written yet, because I simply have no clue on where to start that segment of code. The only thing that I can think that we learned how to do are loops. I was thinking is there a way to create for instance a finite loop in which the user types in the exact amount of resistors that he/she wants to calculate and then the program would then loop i.e. ask the user the value of resistance that many times and then take the sum of that entire loop segment? Ex.

output: how many resistors are there in the circuit > 3 (user input)

output: what is the value of the resistance > 5

output: what is the value of the resistance > 3

output: what is the value of the resistance > 2

---- end of loop

output: the total resistance is 10

something like that with a do while loop is all that I can think of and I don't really know where to start.

8. The concept sounds good.
You only really need to get started.
Start with a question of how many resistors that should be entered.

9. Originally Posted by Elysia
The concept sounds good.
You only really need to get started.
Start with a question of how many resistors that should be entered.
I have I just don't know where to go in terms of the syntax and if my idea is even possible. The general layout would be like this:

int total_resistors ;

double resistor_value ;

double sum ;

cout << "how many resistors are there in the circuit " ;

cin >> total_resistors ;

do {

cout << "enter the value of the resistor" ;

cin >> resistor_value ;

{while (total_resistors) ; ------- in this case I would like the loop to be finite so that whatever the user entered for the value of the total resistors in the circuit the program would end the loop. So if the user entered the value 5 for the total amount of resistors in the circuit the program would loop 5 times (or 4 additional times). It would then calculate the value of the entire users input for all the values asked in the loop.

sum = resistor_value + (2nd entry of resistor value) + resistor_value + (3rd entry of resistor value) resistor_value + (nth entry of resistor value) resistor_value ;

cout << "the total resistance is" >> sum >> ;

So it would then spit out the result of the sum, but I don't know if this is possible and if it is I have no idea what the syntax would be thus that is where I need help.

10. You could use a for loop:
for (int i = 0; i < total_resistors; i++)

You could sum the total resistance entered so far:
total_resistance += entered_resistance;

And then print out the total resistance:
std::cout << total_resistance;

Hope that helps a little.

11. Originally Posted by Elysia
You could use a for loop:
for (int i = 0; i < total_resistors; i++)

You could sum the total resistance entered so far:
total_resistance += entered_resistance;

And then print out the total resistance:
std::cout << total_resistance;

Hope that helps a little.
Thanks I'll give that a shot.

12. Originally Posted by Elysia
You could use a for loop:
for (int i = 0; i < total_resistors; i++)

You could sum the total resistance entered so far:
total_resistance += entered_resistance;

And then print out the total resistance:
std::cout << total_resistance;

Hope that helps a little.
I tried the code and I got it to work for the condition i.e. it loops however many resistors the user types into the program. The only thing that is weird is that when I do

total_resistance += entered_resistance ;

the program always spits out 5.212e+291

cout << "How many resistors are there in the circuit? > " ;
cin >> total_resistors ;

for (int i = 0 ; i < total_resistors ; i++)

{
cout << "Enter the value of the resistance > " ;
cin >> entered_resistance ;

}

total_resistance += entered_resistance ;

cout << " total resistance is " << total_resistance << "\n" ;

13. OK first off, use code tags.
And secondly, obviously you get the wrong answer, because
a) You don't initialize total_resistance, and
b) Your logic is off the chart (why?)

14. Originally Posted by Elysia
OK first off, use code tags.
And secondly, obviously you get the wrong answer, because
a) You don't initialize total_resistance, and
b) Your logic is off the chart (why?)
I did initialize total_resistance I just didn't show it in the code.

and I don't know what you mean by "your logic is off the chart?"

15. Put a different way: what does the code do? Can you write it in pseudo code? How does it work?

Popular pages Recent additions