944,030 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 334
  • C++ RSS
Nov 2nd, 2009
0

Structures

Expand Post »
I have the following struct

C++ Syntax (Toggle Plain Text)
  1. struct A
  2. {
  3. int a,
  4. *b,
  5. *c,
  6. *d;
  7. };

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)
  1. void assign_struct(A test[], int id)
  2. {
  3. if (id == 1)
  4. {
  5. A[0].a = 2;
  6. A[0].b = new int [A[0].a];
  7. A[0].c = new int [A[0].a];
  8. A[0].d = new int [A[0].a];
  9.  
  10. A[0].b[0] = 1;
  11. A[0].b[1] = 5;
  12. A[0].c[0] = 10;
  13. A[0].c[1] = 15;
  14. A[0].d[0] = 20;
  15. A[0].d[1] = 25;
  16.  
  17. A[1].a = 1;
  18. A[1].b = new int [A[1].a];
  19. A[1].c = new int [A[1].a];
  20. A[1].d = new int [A[1].a];
  21.  
  22. A[1].b[0] = -2;
  23. A[1].c[0] = -56;
  24. A[1].d[0] = 34;
  25. }
  26.  
  27. if (id == 2)
  28. {
  29. A[0].a = 1;
  30. A[0].b = new int [A[0].a];
  31. A[0].c = new int [A[0].a];
  32. A[0].d = new int [A[0].a];
  33.  
  34. A[0].b[0] = 18;
  35. A[0].c[0] = 9;
  36. A[0].d[0] = 36;
  37.  
  38. ...
  39. ...
  40. ...
  41.  
  42. }
  43. ...
  44. ...
  45. ...
  46. }

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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nalasimbha is offline Offline
20 posts
since May 2009
Nov 2nd, 2009
0
Re: Structures
First tell me how id has something to do with A[0].a

I mean you have
C++ Syntax (Toggle Plain Text)
  1. if (id == 1)
  2. { A[0].a = 2; ... }
  3. else if(id == 2) { A[0].a = 1; ... }
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 ?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 2nd, 2009
0
Re: Structures
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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nalasimbha is offline Offline
20 posts
since May 2009
Nov 2nd, 2009
0
Re: Structures
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)
  1. void assign_struct(A test[], int id)
  2. {
  3. // replace "A" with "test" inside here.
  4. }
Last edited by VernonDozier; Nov 2nd, 2009 at 9:00 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 3rd, 2009
0
Re: Structures
Click to Expand / Collapse  Quote originally posted by nalasimbha ...
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..
Then just do this :
C++ Syntax (Toggle Plain Text)
  1. void assign_struct(A test[], int id)
  2. {
  3. switch(id)
  4. {
  5. case 0 : A[0].a = 12;
  6. case 1 : A[0].a = 2;
  7. case 2 : A[0].a = 1
  8. //so on
  9. }
  10.  
  11. ///then rest of the code :
  12. A[0].b = new int[A[0].a];
  13. ...

or if you know the values before compile time
C++ Syntax (Toggle Plain Text)
  1. void assign_struct(A test[], int id)
  2. {
  3. int A[] = {12,1,2,3,7,3,8 }; //If you know its values during compile time
  4.  
  5. ///then rest of the code :
  6. A[0].b = new int[A[id].a]; //use id as index with its value coordinated
  7. ...
Last edited by firstPerson; Nov 3rd, 2009 at 2:17 am.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 3rd, 2009
0
Re: Structures
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nalasimbha is offline Offline
20 posts
since May 2009
Nov 3rd, 2009
0
Re: Structures
Click to Expand / Collapse  Quote originally posted by nalasimbha ...
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; Nov 3rd, 2009 at 10:47 am.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 3rd, 2009
0
Re: Structures
@ 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nalasimbha is offline Offline
20 posts
since May 2009
Nov 3rd, 2009
0
Re: Structures
Click to Expand / Collapse  Quote originally posted by nalasimbha ...
@ 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:

Quote ...
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..
Replacing "A" with "test",

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)
  1. void assign_struct(A test[], int id)
  2. {
  3. int A[] = {12,1,2,3,7,3,8 }; //If you know its values during compile time
  4.  
  5. ///then rest of the code :
  6. A[0].b = new int[A[id].a]; //use id as index with its value coordinated
  7. ...

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)
  1. void assign_struct(A test[], int id)
  2. {
  3. // assumes test[] has size 5, id ranges from 0 to 3
  4. int aValues[4][5] = {{7,4,5,9,7}, {2,1,6,4,8}, {1,5,3,2,8}, {7,3,1,2,8}};
  5.  
  6. for (int i = 0; i < 5; i++)
  7. {
  8. test[i].a = aValues[id][i];
  9. test[i].b = new int[test[i].a];
  10. test[i].c = new int[test[i].a];
  11. test[i].d = new int[test[i].a];
  12. }
  13.  
  14. // you can fill in the actual b, c, and d values with 3 separate 3-dimensional arrays.
  15. }
Last edited by VernonDozier; Nov 3rd, 2009 at 11:59 am.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problems with vectors
Next Thread in C++ Forum Timeline: Merging 2 Files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC