Im trying to make a C++ program to calculate the resistance in a parallel electric circuit. The formula is

Total = 1 / (1/resistor1)+ (1/resistor2).... + (1/resistorN)

I have made the program however when I compile it it has the "linker error"

``````#include <iostream>

using namespace std;

int RParallel(int);

int main()
{
int input[5];    // asign 4 inputted numbers to int

cout << "This Program will calculates total resistance"
<< "\nin a parallel electric circuit "
<< endl;                       // description of program

cout << "\nPlease enter 5 resistors separated by space: " ;

cin >> input[1] >> input[2] >> input[3] >> input[4] >> input[5];

RParallel( input[5] );             // calls the funtion

system("pause");
return 0;

}

int RParallel( int input[5] )
{
int N, temp, total;
total = 0;

for (N = 1; N <=5; N++)

{
temp = 1/(1/input[N]);
total += temp;
}

cout << "\n\nThe Resistant total is : " << total << endl << endl ;

}``````

## All 11 Replies

line 18 is wrong -- the array does not have 6 elements, only 5, which are numbered 0,1,2,3 and 4. There is no 5th element.

The parameters to the function in lines 5 and 27 to not agree.

Even if the program above works, the result will also be incorrect because integer division. I started C++ 3 days ago so the little I can do is this

``````#include <iostream>
using namespace std;

double RParallel(double[], int);

int main()
{
int input_size;
cout<<"Please enter number of inputs : ";
cin>>input_size;
double input[input_size];

for(int i = 0; i < input_size; i++)
{
cout<<"Enter input #"<<(i + 1)<<" : ";
cin>>input[i];
}

cout<<"Total resistance : "<<RParallel(input, input_size)<<endl;
return 0;
}

double RParallel(double input[], int size)
{
double total = 0.0;

for(int x = 0; x < size; x++)
{
total += (1/input[x]);
}

return (1/total);
}``````

NOTE: Using system("pause") is unsafe, simply put something like the lines below place of that

``````int something;
cin>>something;``````

The above lines will keep the console open until you enter any value from the keyboard.

line 18 is wrong -- the array does not have 6 elements, only 5, which are numbered 0,1,2,3 and 4. There is no 5th element.

The parameters to the function in lines 5 and 27 to not agree.

what do you mean it doesn't agree? the int suppose to be a double?

Even if the program above works, the result will also be incorrect because integer division. I started C++ 3 days ago so the little I can do is this

``````#include <iostream>
using namespace std;

double RParallel(double[], int);

int main()
{
int input_size;
cout<<"Please enter number of inputs : ";
cin>>input_size;
double input[input_size];

for(int i = 0; i < input_size; i++)
{
cout<<"Enter input #"<<(i + 1)<<" : ";
cin>>input[i];
}

cout<<"Total resistance : "<<RParallel(input, input_size)<<endl;
return 0;
}

double RParallel(double input[], int size)
{
double total = 0.0;

for(int x = 0; x < size; x++)
{
total += (1/input[x]);
}

return (1/total);
}``````

NOTE: Using system("pause") is unsafe, simply put something like the lines below place of that

``````int something;
cin>>something;``````

The above lines will keep the console open until you enter any value from the keyboard.

your right it would be wrong if i plug in 0's. thanks for the heads up.

By "not agree" he meant that the parameter at line 5 is an int and at line 27 it is an array of ints. That is probably what caused the undefined reference error.

No he means not agree because of the following line:

``cin >> input[1] >> input[2] >> input[3] >> input[4] >> input[5];``

This is saying that the array has up to 6 elements but it is defined as int input[5]. This means that calling input[5] will cause the program to crash because it is outside the bounds of the array. It should be this:

``cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4];``

Also, using this

``````int something;
cin>>something;``````

is dangerous because if the user enters a character instead of an int the program will freak out.

When I change line 5 and 27 to agree each other, it gives me another linker error.

``````cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4];

int RParallel( int input[4] )``````

Did you understand the code I posted?

This

``RParallel( input[5] );             // calls the funtion``

``RParallel( input );             // calls the funtion``

When you are passing arrays as arguments to a function you only pass the name. The reason for the linker error is when you pass input[4] (Or whatever array subscript you want) you are now passing a variable of type double, not double[]

No he means not agree because of the following line:

``cin >> input[1] >> input[2] >> input[3] >> input[4] >> input[5];``

This is saying that the array has up to 6 elements but it is defined as int input[5]. This means that calling input[5] will cause the program to crash because it is outside the bounds of the array. It should be this:

``cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4];``

Also, using this

``````int something;
cin>>something;``````

is dangerous because if the user enters a character instead of an int the program will freak out.

Um, no. Ancient Dragon said "The parameters to the function in lines 5 and 27 to not agree." However, you said that he is using "not agree" to refer to the line "cin >> input[1] >> input[2] >> input[3] >> input[4] >> input[5];", which is line 18.