Structures

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 12
Reputation: nalasimbha is an unknown quantity at this point 
Solved Threads: 0
nalasimbha nalasimbha is offline Offline
Newbie Poster

Structures

 
0
  #1
24 Days Ago
I have the following struct

  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
  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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,142
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 144
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
0
  #2
24 Days Ago
First tell me how id has something to do with A[0].a

I mean you have
  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 ?
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?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 12
Reputation: nalasimbha is an unknown quantity at this point 
Solved Threads: 0
nalasimbha nalasimbha is offline Offline
Newbie Poster
 
0
  #3
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..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
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.


  1. void assign_struct(A test[], int id)
  2. {
  3. // replace "A" with "test" inside here.
  4. }
Last edited by VernonDozier; 24 Days Ago at 9:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,142
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 144
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
0
  #5
24 Days Ago
Originally Posted by nalasimbha View 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..
Then just do this :
  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
  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; 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?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 12
Reputation: nalasimbha is an unknown quantity at this point 
Solved Threads: 0
nalasimbha nalasimbha is offline Offline
Newbie Poster
 
0
  #6
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #7
23 Days Ago
Originally Posted by nalasimbha View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 12
Reputation: nalasimbha is an unknown quantity at this point 
Solved Threads: 0
nalasimbha nalasimbha is offline Offline
Newbie Poster
 
0
  #8
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #9
23 Days Ago
Originally Posted by nalasimbha View Post
@ 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..
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:

  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.

  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; 23 Days Ago at 11:59 am.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC