for loops and arrays


i am probably doing something wrong with the for loop and i don't fully understand arrays
but i basically want the program to count resistors in parallel
and out put the amount of resistance

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Perpose: This program finds the resistons of a amount of resistors in parall
   Equation: 1/r = 1/R1........1/R6......1/R8 */


#include <iostream>
using namespace std;

int main()
{
    // declare varibles
   
    
    
    char Again;
    bool Running = true;
    int NumOfResistors;
    float Denominator[Dsize]; // compiler says this is undeclared
    float Numerator[Nsize];   // compiler says this is undeclared
    int Dsize;                 // compiler says this is undeclared
    int Nsize;                  // compiler says this is undeclared
    
    while(Running)
    {
                  system("CLS");
                  cout <<" _______________________________________ " << endl;
                  cout <<" _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_ " << endl;
                  cout <<"  Welcome to the parell resistor counter " << endl;
                  cout <<" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl << endl << endl; 
                  cout <<" Please type in the amount of resistors: ";
                  cin  >> NumOfResistors;
                  
                  for(int Resistor; NumOfResistors >= Resistor && 0 <= NumOfResistors; Resistor++ )
                  {
                          
                          
                          cout << " Type in the resistors ohms: ";
                          cin  >> Denominator;
                          Numerator[Nsize] = 1;
                          
                          Nsize++;
                          Dsize++;
                          
                  }
                  
                  cout <<"Nsize: " << Nsize << endl;
                  cout <<"Dsize: " << Dsize << endl;
                  
                
                 
                 
                 
                 
                 
                 
                 
                 
                 
                                  // ask user if they would like to rerun the program
                                  cout << "          Would you like to run this program again (y or n) " << endl;
                                  cin  >> Again;
                                  switch(Again)
                                  {
                                               // if the user says types y keep the program running 
                                               case 'y':
                                                    break;
                                               // if the user types n end the program and exit loop
                                               case 'n':
                                                    Running = false;
                                                    break;
                                               default:
                                                       // if the user types any other letter display massage
                                                       cout << " this is a invalid value " << endl;
                                  }                    
    }
                                  
                                  
    system("PAUSE");
    return 0;
}
                                      
                                      
float Denominator[Dsize];
float Numerator[Nsize];
You must use a fixed number. The array size can't be determined at runtime. You must change to using a vector or set a hard limit on array size.

1
2
3
4
5
6
7
8
9
10
//pseudocode
// totalres = 1 / ( 1/R1 + 1/R2...+1/Rn )
int i;
for ( i = 1; i == NumResistors; i++);
    resistor[i-1] = 1 / resistor[i-1]; // invert each resistor

for ( i = 1; i == NumResistors; i++);
    //add the inverted resistors

totalres = 1/totalres ; invert the sum from above


Hotaru
when i put
resistor[i-1] = 1 / resistor[i-1];
it says "invalid double{int}"
or with int it says "invalid int{int}"

Last edited on
Topic archived. No new replies allowed.