Thread: Desperate help needed. C++ Uni Assignment.

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    Desperate help needed. C++ Uni Assignment.

    Hello everyone.

    I've just started University and had a c++ Assignment due 2 days ago so I've already lost 20% of my marks.

    Basically I've hit a roadblock and can't figure out how to use array values in an equation.


    The program needs to calculate the Resistance total (r) values of a parallel circuit.
    It needs to prompt the user for the value of resistor 1, then resistor 2 then resistor 3 and store those values in an array of size 3 (assignment criteria).

    I've got the array working however I need to use the array values in an equation and no matter what I try it won't work.

    So the program needs to do this.:

    Enter R1:
    Enter R2:
    Enter R3:

    Total resistance = 1/R1+1/R2+1/R3.

    Answer = total resistance.

    Here's the coding as you see I haven't done much so far. I've tried many different things even using r = 1/[0]+1/[1]+1/[2]. I have no idea why that doesn't work.

    Code:
    #include <iostream>
    using namespace std;
     int main()
     {
        int myArray[3]; //Array of 3 integars
        int i;
        for (i=0;i<3;i++) //0-3
            {
            cout << "Enter the value of R" << i+1 << ": ";
            cin >> myArray[i];
            }
     
        int r, r1, r2, r3;
            myArray[0] = r1;
            myArray[1] = r2;
            myArray[2] = r3;
        
            cout <<r1<<"";
       
            /*r = 1/r1+1/r2+1/r3;
            cout << r;*/
            
            system("pause");
            return 0;
     }
    That's my coding so far and it doesn't work properly, it prompts for the resistor values correctly then displays the answer however the answer is not evne close to been correct.

    Can anyone please tell me what I'm doing wrong?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    The main problem that I see is that you assign myArray values within the loop (which is fine), but then you initialise variables r, r1, r2 and r3 and assign what is in those variables to the elements of myArray. Because variables that aren't initialised just hold random junk (whatever was in that memory before) your variables aren't going to contain anything useful.

    What you want to do is change 'myArray[0] = r1;' to 'r1 = myArray[0];' (and with the other ones too).

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Thanks mate, it's still not working I'm not sure if it changed anything.

    I put in different values and I get the same answer as I did no matter what values I put in the answer is always the same and around 8 digits.

    Can anyone see anything wrong? All I need it to do is calculate the r = eqaution and display it in a cout after the arrays are entered.

    Code:
    #include <iostream>
    using namespace std;
     int main()
     {
        int myArray[3]; //Array of 3 integars
        int i;
        for (i=0;i<3;i++) //0-3
            {
            cout << "Enter the value of R" << i+1 << ": ";
            cin >> myArray[i];
            }
     
        int r, r1, r2, r3;
            r1= myArray[0];
            r2= myArray[1];
            r3= myArray[2];
        
            cout <<r<<"";
       
            r = 1/r1+1/r2+1/r3;
            cout << r;
            
            system("pause");
            return 0;
     }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, I think you are displaying an uninitialized value, because you display r both before and after the calculation. The value before is undefined since you haven't given r any value.

    The next problem is that for all values of r1, r2, and r3 that isn't exactly 1, you will get zero as the result, since your variables are all integers, and a larger integer dividing a smaller will result in 0 as the result. I suggest you change the type of all your variables to be float.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Thanks mate that helped a bit, I can now use decimal form. However the answer is nowhere near close enough. Also float or double didn't work on declaring i, so I left it as int.

    Say I run the program it prompts me for R1, R2, R3 as it's meant to. I type in 4, 4, 2.
    According to my equation that should give an answer of 1 (.25+.25+.5=1) however my answer comes out like 5.70678e-0391. I have no idea why the equation isn't working it's directly typed in.

    Code:
    #include <iostream>
    using namespace std;
     int main()
     {
        float myArray[3]; //Array of 3 integars
        int i;
        for (i=0;i<3;i++) //0-3
            {
            cout << "Enter the value of R" << i+1 << ": ";
            cin >> myArray[i];
            }
     
        float r, r1, r2, r3;
            r1= myArray[0];
            r2= myArray[1];
            r3= myArray[2];
        
            cout <<r<<"";
       
            r = 1/r1+1/r2+1/r3;
            cout << r;
            
            system("pause");
            return 0;
     }

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    cout <<r<<"";
    Get rid of that line, it's simply outputting the value of an uninitialized variable which may very well be the 5.70678e-039 value that gets displayed (with your answer of 1 tacked on to the end).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I'd recommend putting parentheses around parts of your formula so it's a bit clearer how it works too. the compiler is probably following the proper math rules, so it should work properly, but it makes it a bit more readable for others if you include parentheses.

    also your formula is not exactly correct. the formula for parallel resistances is the inverse of the sum of the inverses of the individual resistances:

    Code:
    r = 1 / ((1 / r1) + (1 / r2) + (1 / r3));
    hope this helps

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Thanks heaps mate can't believe I missed that.

    It's all running nice now. However there's a few more things I need to include.

    I'll let ya's know how I go.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Hey guys I got an extension, I've got all of it done however it's due tomorrow and there's 2 things I'm not sure of.

    Firstly, I'm told that I'm required to use void functions for this program however I don't know anything about them or what they do or what they're used for. What would I use them for in my program?

    Secondly I'm unsure how to use if statements with arrays.

    I need it to display an error message if the user inputs a negative value, and I then need it to bring up the cin>> input again so the user can re enter a compatible value.

    This is what I've got so far:

    Code:
    #include <iostream>
    using namespace std;
     int main()
     {
        float myArray[3]; //Array of 3 integers
        int i;
        for (i=0;i<3;i++) //0-3
            {
            cout << "Enter the value of R" << i+1 << ": ";
            cin >> myArray[i];
        if  (myArray[i]<0) cout << "ERROR! Negative value entered please enter a positive value"; 
            }
        
        float r, r1, r2, r3;
            r1= myArray[0];
            r2= myArray[1];
            r3= myArray[2];
            
            r = 1/((1/r1)+(1/r2)+(1/r3));
            cout << "The total resistance of the circuit is: ";
            cout << r;
            cout << endl;
            
        float vArray[1];
        int j;
        for (j=0;j<1;j++)    
            {
            cout << "Enter the supply voltage for the circuit"": ";
            cin >> vArray[j];
            }
            
        float v, c, c1, c2, c3;    //I = V/R
            
            v = vArray[0];
            c = vArray[0]/r;
            c1 = (1/r1)/((1/r1)+(1/r2)+(1/r3))*c; 
            c2 = (1/r2)/((1/r1)+(1/r2)+(1/r3))*c;
            c3 = (1/r3)/((1/r1)+(1/r2)+(1/r3))*c;
            cout << "The current flowing through R1 is:";
            cout << c1;
            cout << endl;
            cout << "The current flowing through R2 is:";
            cout << c2;
            cout << endl;
            cout << "The current flowing through R3 is:";
            cout << c3;
            cout << endl; 
            cout << "The total current in the circuit is ";
            cout << (c1+c2+c3);
            cout << endl; 
            system("pause");
            return 0;
     }
    So far the if statement doesn't work at all and the program doesn't compile. I need it to reloop the for loop if the user inputs a negative value. However if R1 is already entered as a positive value and the user inputs a negative value for R2 it can't reloop and ask the user for R1 again it'll need to ask the user to renter R2. Then have an else statement if the user inputs a positive value which then needs to make the program not reloop and go on to the next part of programming.

    How would I get it to do that?

    Any help would be appreciated I'm so close to finishing it.
    Last edited by ILLaViTaR; 04-13-2009 at 12:28 PM.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You could just reset i to be i-1. That way when you run through the for-loop, and say you input R2 as being negative, you print the error-message, reset i to be 0 (i was 1 for R2), but then you increment i again, so you bring it back up to 1 and you get prompted to input R2 again.

    If you want help with the compile-errors please post them so we can see them.

    Void functions are functions that return void (that is, nothing). main is an int function because it returns an integer. I suggest you read up on functions here: Cprogramming.com Tutorial: Functions

    That link should give you all info you need to do the void function part of the assignment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Desperate Help needed
    By roco090 in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2005, 09:43 PM