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"
Can you please help me spot the mistake

#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: " ;
   
    // asign inputted numbers
    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 ;
     
    return total;
}

Recommended Answers

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

should be this instead

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.

Re-read my comments. The statement about lines 5 and 27 is not related to the comment about input array.

Comments by other members in this thread are also valid, so the OP should read them all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.