Thread: noob c ++ question. Summations

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    11

    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. #2
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    you should at least try to write some code.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Is it your first class?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you know of std::vector? Are you allowed to use it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    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. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As good time as any to learn it. Not all universities forbid the use of things not yet taught.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    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. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The concept sounds good.
    You only really need to get started.
    Start with a question of how many resistors that should be entered.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Quote Originally Posted by Elysia View Post
    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. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Quote Originally Posted by Elysia View Post
    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. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Quote Originally Posted by Elysia View Post
    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. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Quote Originally Posted by Elysia View Post
    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. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Put a different way: what does the code do? Can you write it in pseudo code? How does it work?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob question about RAM
    By crvenkapa in forum Tech Board
    Replies: 2
    Last Post: 03-26-2010, 03:41 PM
  2. Noob Question: Can't compile with "end1"
    By Lillers in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2010, 04:23 AM
  3. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  4. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  5. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM