| | |
Structures
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 12
Reputation:
Solved Threads: 0
I have the following struct
I would be creating an array of A and then populating the member variable for each of struct in the array. The members b,c,d are allocated dynamically based on the value of A.a.
To assign the values of A, i have the following function
I would like to know if there is a way to populate the members of the struct based on 'id' without using and if-then-else or switch-case statement?
C++ Syntax (Toggle Plain Text)
struct A { int a, *b, *c, *d; };
I would be creating an array of A and then populating the member variable for each of struct in the array. The members b,c,d are allocated dynamically based on the value of A.a.
To assign the values of A, i have the following function
C++ Syntax (Toggle Plain Text)
void assign_struct(A test[], int id) { if (id == 1) { A[0].a = 2; A[0].b = new int [A[0].a]; A[0].c = new int [A[0].a]; A[0].d = new int [A[0].a]; A[0].b[0] = 1; A[0].b[1] = 5; A[0].c[0] = 10; A[0].c[1] = 15; A[0].d[0] = 20; A[0].d[1] = 25; A[1].a = 1; A[1].b = new int [A[1].a]; A[1].c = new int [A[1].a]; A[1].d = new int [A[1].a]; A[1].b[0] = -2; A[1].c[0] = -56; A[1].d[0] = 34; } if (id == 2) { A[0].a = 1; A[0].b = new int [A[0].a]; A[0].c = new int [A[0].a]; A[0].d = new int [A[0].a]; A[0].b[0] = 18; A[0].c[0] = 9; A[0].d[0] = 36; ... ... ... } ... ... ... }
I would like to know if there is a way to populate the members of the struct based on 'id' without using and if-then-else or switch-case statement?
0
#2 24 Days Ago
First tell me how id has something to do with A[0].a
I mean you have
How does id correlates to A[0].a?
What would A[0].a be if id is 3,4,5,6,7,8,9,10 ?
I mean you have
C++ Syntax (Toggle Plain Text)
if (id == 1) { A[0].a = 2; ... } else if(id == 2) { A[0].a = 1; ... }
What would A[0].a be if id is 3,4,5,6,7,8,9,10 ?
I give up!
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
0
#4 24 Days Ago
You should replace
A with test everywhere inside your function body. A is a type. test is the variable itself. test is an array, not A . You cannot dereference A with the [] operator because there is nothing to dereference. C++ Syntax (Toggle Plain Text)
void assign_struct(A test[], int id) { // replace "A" with "test" inside here. }
Last edited by VernonDozier; 24 Days Ago at 9:00 pm.
0
#5 24 Days Ago
•
•
•
•
id has nothing to do with A[0].a. id is a variable that i pass to the function assign_struct. if id = 3 then A[0].a might be a different value, and if id=4 then A[0].a is another value and so on..
C++ Syntax (Toggle Plain Text)
void assign_struct(A test[], int id) { switch(id) { case 0 : A[0].a = 12; case 1 : A[0].a = 2; case 2 : A[0].a = 1 //so on } ///then rest of the code : A[0].b = new int[A[0].a]; ...
or if you know the values before compile time
C++ Syntax (Toggle Plain Text)
void assign_struct(A test[], int id) { int A[] = {12,1,2,3,7,3,8 }; //If you know its values during compile time ///then rest of the code : A[0].b = new int[A[id].a]; //use id as index with its value coordinated ...
Last edited by firstPerson; 24 Days Ago at 2:17 am.
I give up!
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
0
#7 23 Days Ago
•
•
•
•
Thank you firstPerson. But the question remains to be answered.Is there no way to do the assignments without the if then else or switch statements. Your solutions have switch statement in it.
I'll reiterate what I said in my last post. The code is not going to compile with terms like
A[0].b in it and it has nothing to do with whether you use if-statements or switch statements. A is a struct definition, not a variable. You cannot dereference it with the [] operator. Replace "A" with "test". Last edited by VernonDozier; 23 Days Ago at 10:47 am.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
0
#9 23 Days Ago
•
•
•
•
@ VernonDozier: That was a typo and I do realize that it wont compile. I do need to use test instead of A. Pointed noted. That being accepted is there an alternate route to the if then else?
The devil is in the details and you have dots there so it's unclear whether there is some sort of pattern. You have this line in a previous post:
•
•
•
•
id has nothing to do with A[0].a. id is a variable that i pass to the function assign_struct. if id = 3 then A[0].a might be a different value, and if id=4 then A[0].a is another value and so on..
You say id has nothing to do with test[0].a, but then you assign the value of test[0].a based on the value of id in your if-else statement, so that seems like a contradiction. I'm sure you could set up a complex (or not so complex) single or multi-dimensional array or map that could take out all of the branching, but I don't know if that would make the code any cleaner. I think that was what firstPerson was going for here:
C++ Syntax (Toggle Plain Text)
void assign_struct(A test[], int id) { int A[] = {12,1,2,3,7,3,8 }; //If you know its values during compile time ///then rest of the code : A[0].b = new int[A[id].a]; //use id as index with its value coordinated ...
except that he named his integer array A, which further compounds the earlier problem of using A instead of test.
You can do something like this, with a 2-D and 3 3-D arrays (one each for b[], c[], and d[] arrays. 2-D array will assign values to A[].a based on id. I don't have time to make a skeleton for the 3-D arrays, but this will create the storage and will fill in the A[].a values.
C++ Syntax (Toggle Plain Text)
void assign_struct(A test[], int id) { // assumes test[] has size 5, id ranges from 0 to 3 int aValues[4][5] = {{7,4,5,9,7}, {2,1,6,4,8}, {1,5,3,2,8}, {7,3,1,2,8}}; for (int i = 0; i < 5; i++) { test[i].a = aValues[id][i]; test[i].b = new int[test[i].a]; test[i].c = new int[test[i].a]; test[i].d = new int[test[i].a]; } // you can fill in the actual b, c, and d values with 3 separate 3-dimensional arrays. }
Last edited by VernonDozier; 23 Days Ago at 11:59 am.
![]() |
Similar Threads
- Creating dynamic array structures (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
- dynamic array of structures problem (C++)
- C++ structures (C++)
- Read and writing strings from structures (C++)
- Java Collections: ADTs, Data Structures & Algorithms (Java)
Other Threads in the C++ Forum
- Previous Thread: problems with vectors
- Next Thread: Merging 2 Files
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






