Array of structs help.

Hi, I am working on a simple project that calculates resistance and voltage. I just don't understand what it means by this.

"Repeat part 4, except this time, instead of using a 2 dimensional array to store
the test data, use a 1 dimensional array of structs, where r1, r2, r3, and I are
individual fields in the struct."

I need help with what it means by a 1 dimensional array of structs. Here is what I have but it looks so redundant.
Is there a more compact way to do this, or is this it?

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <string>
#include "testerPart4."
using namespace std;

void main(){
	
struct circuit 
{
  int r1;
  int r2;
  int r3;
  int I;
};
// cir1 means circuit 1
// curr means current
struct circuit;

circuit cir1[3];
circuit curr1[1];

circuit cir2[3];
circuit curr2[1];

circuit cir3[3];
circuit curr3[1];

circuit cir4[3];
circuit curr4[1];
circuit cir5[3];

circuit curr5[1];
circuit cir6[3];
circuit curr6[1];


circuit cir7[3];
circuit curr7[1];

circuit cir8[3];
circuit curr8[1];

circuit cir9[3];
circuit curr9[1];

circuit cir10[3];
circuit curr10[10];




cir1[0].r1 = 3;
cir1[1].r1 = 8;
cir1[2].r1 = 6;
curr2[0].I = 1;

cir2[4].r1 = 2;
cir2[5].r1 = 2;
cir2[6].r1 = 4;
curr2[1].I = 7;

cir3[8].r1 = 8;
cir3[9].r1 = 6;
cir3[10].r1 = 5;
curr3[2].I = 3;

cir4[12].r1 = 7;
cir4[13].r1 = 7;
cir4[14].r1 = 6;
curr4[3].I = 1;

cir5[0].r1 = 5;
cir5[1].r1 = 1;
cir5[2].r1 = 7;
curr5[4].I = 7;

cir6[0].r1 = 3;
cir6[1].r1 = 5;
cir6[2].r1 = 6;
curr6[5].I = 4;

cir7[0].r1 = 4;
cir7[1].r1 = 6;
cir7[2].r1 = 6;
curr7[6].I = 5;

cir8[0].r1 = 1;
cir8[1].r1 = 5;
cir8[2].r1 = 3;
curr8[7].I = 5;

cir9[0].r1 = 5;
cir9[1].r1 = 8;
cir9[2].r1 = 1;
curr9[8].I = 3;

cir10[0].r1 = 6;
cir10[1].r1 = 7;
cir10[2].r1 = 7;
curr10[9].I = 4;




system("pause");
}
Last edited on
circuit array_of_circuits[5];
Now you have an array of 5 circuit structs. Each struct can be indexed with number 0-4

for example: std::cout << array_of_circuits[0].r1; prints r1 from the first circuit struct
Last edited on
The struct array also makes it easy to get input for each struct using a for loop.

1
2
3
4
5
6
7
8
9
10
11
for (int i=0; i<size; i++)
{
    cout << "Enter r1 value for circuit " << i+1 << ": ";
    cin >> arrayCircuits[i].r1;
    cout << "Enter r2 value for circuit " << i+1 << ": ";
    cin >> arrayCircuits[i].r2;
    cout << "Enter r3 value for circuit " << i+1 << ": ";
    cin >> arrayCircuits[i].r3;
    cout << "Enter I value for circuit " << i+1 << ": ";
    cin >> arrayCircuits[i].I;
}
closed account (SECMoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 struct circuit 
  {
      int r1; 
      int r2; 
      int r3; 
      int I; 
  };

 int main ()
 {
    cout << "enter your r1, r2, r3 and I fields for each object\n";

    int tempr1, tempr2, tempr3, tempi;
    circuit array [10];/// array of ten struct objects
  
    for (int i=0; i <10: i++)
        {
          cin>> arry [i].r1>> array[i].r2>> array [i].r3>>array [i].I ;
        }
 }
Thanks, I'll mess around with these two to see which one is better fit for my project.
Topic archived. No new replies allowed.