Using Arrays and Functions to calculate parallel resistance

Well here is a code I had came across and learned from to do one of my previous projects. My new project is to calculate resistance of 10 or less using Arrays and Functions. I cannot ask the user how many resistor though they would need to end it by entering a negative number (ie. -1). I am totally lost on how to do this can some one help me.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{

int n, MAX;
float resistance, par_Resistance, current, percent_I;

cout << "Enter number of parallel resistors in the circuit." << endl;
cin >> n;
cout << endl;
MAX = n;

float array[9];

for (int i = 0; i <= MAX; i++)
{
cout << "Enter value of R" << i+1 << endl;
cin >> array[i];
cout << endl;
}

for (int i = 0; i < MAX; i++)
{
resistance = 1/array[i];
par_Resistance + resistance;

}
resistance = 1/par_Resistance;
cout << "The equivelent parallel resistance is " << resistance << " ohms." << endl;

system("pause");

}
1
2
3
while( cin>>resistance and resistance>0 ){
   //...
}
i am still lost heres another program I did. How do I incorporated arrays into this seeing I have gotten everything else down.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
double ser;
double res;
double val;
double para;
double Rpara;

ser = 0;
res = 0;
para = 0;
cout << "Enter the value of the resistor or enter -1 if finished: ";
cin >> val;

while ( val != -1 )
{
if ( val <= 0 )
{
cout << "Enter a valid resistor value or enter -1 if finished: ";
cin >> val;
}


else
{
ser = ser + val;
para = para + (1 / val);
Rpara = 1 / para;
res = res++;

cout << "Enter the value of the resistor or input -1 if finished: ";
cin >> val;
}

}
cout << "Enter the value of the resistor or enter -1 if finished: ";
cin >> val;


cout << "Total resistance in series= " << ser << " Ohm(s)" << endl;
cout << "Total resistance in parallel= " << Rpara << " Ohm(s)";
system("PAUSE");
return 0;
}
Something like this...
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
#include <iostream>

using namespace std;

int main()
{

    const int MAX = 10;
    double array[MAX];
    double val;
    int    count = 0;

    cout << "Enter the value of the resistor or enter -1 if finished: ";
    cin >> val;

    while ( val != -1 )
    {
        if ( val <= 0 )
        {
            cout << "Enter a valid resistor value or enter -1 if finished: ";
            cin >> val;
        }
        else
        {
            array[count] = val;
            count++;

            if (count >= MAX)
                break;

            cout << "Enter the value of the resistor or input -1 if finished: ";
            cin >> val;
        }
    }

    for (int i=0; i<count; i++)
    {
        cout << 'R' << (i+1) << " = " << array[i] << endl;
    }

    return 0;
}
this is what I have now except i keep getting an error that says:

An unhandled exception of type 'System.DivideByZeroException' occurred in arrya.exe

Additional information: Attempted to divide by zero.




#include "stdafx.h"

#include <iostream>

using namespace std;
double parallel(double array[],int count);
int main()
{

    const int MAX = 10;
    double array[MAX];
    int val;
    int    count = 0;
	 cout << "This program will calculate a parallel resistance network. The program will end when a negative number is inputted" << endl;
     cout << "" << endl;
	 cout << "Enter the value of the resistor: ";

    cin >> val;

    while ( val > 0 )
    {
        if ( val <= 0 )
        {
            cout << "Enter a valid resistor value: ";
            cin >> array[val];
        }
        else
        {
            array[count] = array[val];
            count++;

            if (count >= MAX)
                break;

            cout << "Enter the value of the resistor: ";
            cin >> val;
        }
    }

    for (int i=0; i<count; i++)
    {
		cout << 'R' << (i+1) << " = " << array[i] << parallel(array, count) << endl;
    }
	system("pause");
    return 0;
}

double parallel(double array[], int count)
{
	int Retval = 0;
	for(int i=0; i<count; i++)
	{
		Retval = Retval * (1/array[i]);
	}
	return (1/Retval);
}
Well, I don't think Retval should be an integer. Double would seem a better choice.
But remember, anything multiplied by zero will result in zero. So
Retval = Retval * (1/array[i]); this line will always give a zero result, since that was the initial value.
And next you attempt to return (1/0);

Don't be discouraged, your previous code (without the array) seemed to work, you just need to take a closer look at the calculation here, and maybe borrow some of your own previous code.

Topic archived. No new replies allowed.