Desperate!!! Function Question.

Hello,
I am taking C++ and am not good at it at all. I have lots of problems with functions. The following program is a question on hw and every time I think it will work...it doesnt. Currently I am getting a very strange answer for the math. It is supposed to ask the user to give a number of resistors in a parallel circuit from 2 to 5 then the values and the function is supposed to calculate the total resistance...any help as to what I am missing would be great...Thank you in advance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;
double RParallel (double&);
int main ()
{
    double amt, r1, r2, r3, r4, r5, res;
    cout<<"Please enter the number of resistors in the parallel circuit (between 2 and 5): ";
    cin>>amt;
    if (amt<2.0 || amt>5.0)
    {
        cout<<"\nIncorrect amount of resistors, try again."<<endl;
        system ("pause");
        return 0;
    }
    if (amt==2.0)
    {
        cout<<"\nEnter the values for the resistors:"<<endl;
        cin>>r1>>r2;
        cout<<"\nThe resistor values are ( "<<r1<<" "<<r2<<" )"<<endl;
        RParallel (res);
        cout<<"The parallel resistance is: "<<res<<endl;
    }
    if (amt==3.0)
    {
        cout<<"\nEnter the values for the resistors:"<<endl;
        cin>>r1>>r2>>r3;
        cout<<"The resistor values are ( "<<r1<<" "<<r2<<" "<<r3<<" )"<<endl;
        RParallel (res);
        cout<<"The parallel resistance is: "<<res<<endl;
    }
    if (amt==4.0)
    {
        cout<<"\nEnter the values for the resistors:"<<endl;
        cin>>r1>>r2>>r3>>r4;
        cout<<"The resistor values are ( "<<r1<<" "<<r2<<" "<<r3<<" "<<r4<<" )"<<endl;
        RParallel (res);
        cout<<"The parallel resistance is: "<<res<<endl;
    }
    if (amt==5.0)
    {
        cout<<"\nEnter the values for the resistors:"<<endl;
        cin>>r1>>r2>>r3>>r4>>r5;
        cout<<"The resistor values are ( "<<r1<<" "<<r2<<" "<<r3<<" "<<r4<<" "<<r5<<" )"<<endl;            
        RParallel (res);
        cout<<"The parallel resistance is: "<<res<<endl;
    }
    system("pause");
    return 0;
}
double RParallel (double& res)
{
     double amt, r1, r2, r3, r4, r5;
       if (amt==2.0)
          {res=1.0/((1.0/(1.0*r1))+(1.0/(1.0*r2)));}
       if (amt==3.0)
          {res=1.0/((1.0/r1)+(1.0/r2)+(1.0/r3));}
       if (amt==4.0)
          {res=1.0/((1.0/r1)+(1.0/r2)+(1.0/r3)+(1.0/r4));}
       if (amt==5.0)
           {res=1.0/((1.0/r1)+(1.0/r2)+(1.0/r3)+(1.0/r4)+(1.0/r5));}
       return res;
}


Please enter the number of resistors in the parallel circuit (between 2 and 5):
2

Enter the values for the resistors:
1
1

The resistor values are ( 1 1 )
The parallel resistance is: 1.23295e-307
Press any key to continue . . .
Floating point numbers are inaccurate, so you cannot compare them using ==. See here:
http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm

Aside from that, amt should not be a floating point value in the first place. You can't have 3.14 resistors, so use int.
I have one question: How come variable "amt" is initialized separately in main() and RParallel function? Aren't they compiled as separate variables, which means that you can't compare RParallel's amt to values that are given to main()'s amt?

As far as I know amt should be created globally. I'm an absolute beginner, so I'm not sure and would like an explanation.

Other than that, I would create a for loops, use an array and not use another function. I'm not sure this is correct way, though:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//...
double array[6]; //6 to hold 5 values
double parRes;
int amt;
//...
cin >> amt //number of resistors
//...if statement to see if (2 <= amt <= 5) and the rest of missing code
for(int i=0, int value, parRes=0; i < amt; i++) {
cin >> value;
parRes = parRes + 1/value;
array[i] = value;
}
//...
cout << "Resistor values are: "
for (int i=0, i < amt; i++) cout << array[i] << ", " << endl;
cout << "Parallel resistance is " << 1/parRes
//... 


It's okay to use integers in formulas that get assigned to doubles, right?
Last edited on
Couldn't wait for a reply, so I tested the thing myself and it works without issues. It's the same thing but written much more efficiently and it works properly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main() {
	double resVal[6]; //6 to hold 5 values
	double parRes=0, val;
	int amt;
	do {
		cout << "Enter number of resistors (2 to 5): ";
		cin >> amt;
		if (amt<2 || amt>5) cout << "Incorrect number of resistors, please try again.\n\n";
	} while (amt<2 || amt>5);
	for(int i=0; i < amt; i++) {
		cout << "Enter value of resistor " << i+1 << ": ";
		cin >> val;
		parRes = parRes + 1/val;
		resVal[i] = val;
	}
	cout << "\nThe resistor values are: "; //not needed, because user can see values above
	for (int i=0; i < amt; i++) cout << resVal[i] << ", ";
	cout << "\nParallel resistance is " << 1/parRes;
	int exit; cout << "\nEnter a value to exit... "; cin >> exit;
	return 0;
}


Loops and arrays are powerful stuff!
Last edited on
double resVal[6]; //6 to hold 5 values

That's not correct. When you declare an array like that, you say how many elements it's going to hold. resVal can hold 6 elements, from resVal[0] to resVal[5].
Last edited on
But isn't last element always "null"? Or is this only with character arrays? I guess I mixed this up, sorry for an error.

Otherwise the code is OK, right? Is there anything in it that could be done more efficiently?
For C-style strings, the last element of the array is always the null character ('\0'). That doesn't stop you from creating a character array that is not null terminated, but you won't be able to use it as a C string.

In the above program you really shouldn't worry about efficiency. However, it would be easier and more proper to use a std::vector rather than an array, considering you have an unknown number of elements.
Thanks! I'm currently half way through "C++ Beginner’s Guide" by Herbert Schildt, so I haven't learned vectors yet.
He doesn't mention them in his guide though, just checked. Which book/online guide would you recommend after finishing this one? I can't decide on my own what is most important to learn, so I'd really like to work through a guide or at least get some directions.
Hello all,
Thank you for the help...
I got it to work...with help from the teacher of course :)...
here is what he showed me
Oh and i would have been able to do this without problem if it wasnt required that we use functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
double RParallel (double, double, double=1e12, double=1e12, double=1e12);
int main ()
{
    double r1, r2, r3, r4, r5;
    int amt;
    double res;
    cout<<"Please enter the number of resistors in the parallel circuit (between 2 and 5): ";
    cin>>amt;
    if (amt<2 || amt>5)
    {
        cout<<"\nIncorrect number of resistors, try again."<<endl;
        system ("pause");
        return 0;
    }
    if (amt==2)
    {
        cout<<"\nEnter the values for the resistors:"<<endl;
        cin>>r1>>r2;
        cout<<"\nThe resistor values are ( "<<r1<<" "<<r2<<" )"<<endl;
        res=RParallel (r1, r2);
        cout<<"The parallel resistance is: "<<res<<endl;
    }
    if (amt==3)
    {
        cout<<"\nEnter the values for the resistors:"<<endl;
        cin>>r1>>r2>>r3;
        cout<<"The resistor values are ( "<<r1<<" "<<r2<<" "<<r3<<" )"<<endl;
        res=RParallel (r1, r2, r3);
        cout<<"The parallel resistance is: "<<res<<endl;
    }
    if (amt==4)
    {
        cout<<"\nEnter the values for the resistors:"<<endl;
        cin>>r1>>r2>>r3>>r4;
        cout<<"The resistor values are ( "<<r1<<" "<<r2<<" "<<r3<<" "<<r4<<" )"<<endl;
        res=RParallel (r1, r2, r3, r4 );
        cout<<"The parallel resistance is: "<<res<<endl;
    }
    if (amt==5)
    {
        cout<<"\nEnter the values for the resistors:"<<endl;
        cin>>r1>>r2>>r3>>r4>>r5;
        cout<<"The resistor values are ( "<<r1<<" "<<r2<<" "<<r3<<" "<<r4<<" "<<r5<<" )"<<endl;            
        res=RParallel (r1, r2, r3, r4, r5);
        cout<<"The parallel resistance is: "<<res<<endl;
    }
    system("pause");
    return 0;
}
double RParallel (double r1, double r2, double r3, double r4, double r5)
{
       return 1.0/((1.0/r1)+(1.0/r2)+(1.0/r3)+(1.0/r4)+(1.0/r5));
}

Yup, Fresh Grass had it right. Basically in the RParallel function in your OP, the values amt, r1, r2, r3, r4, r5 were random and uninitialised; and completely different from the values amt, r1, r2, r3, r4, r5 in main().
Fresh Grass wrote:
Which book/online guide would you recommend after finishing this one?

I recommend the book Programming: Principles and Practices using C++, by Bjarne Stroustrup. It's an excellent, thorough introduction to both programming and C++. Be warned that it's a big book, though.

@DexterMorgan: +1 for the nickname :)
closed account (z05DSL3A)
Which book/online guide would you recommend after finishing this one?

http://www.cplusplus.com/articles/Book_Of_Brilliant_Things/
Topic archived. No new replies allowed.