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;
#include <iostream>
usingnamespace std;
int main()
{
constint 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.