943,877 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 491
  • C++ RSS
Jul 14th, 2009
0

Adding an array to an element of the array

Expand Post »
Now i have an array, P[8]={0,1,1,2,0,3,1,0}

And i want to add 4 separate values to each element in the array,so if i was to lookup P[2] i can access its 4 values.

I tried doing it by giving those standard values an array of itself and making each value get it through a loop but a fundamental problem occurs when assinging P[6] to those 4 values cause it kind of makes the array size 6,anyway i think i've got some concept confused.Is it possible ? How do i circumvent it ?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
dchunt is offline Offline
19 posts
since Jul 2009
Jul 14th, 2009
0

Re: Adding an array to an element of the array

It looks like you want a 2D array:
C++ Syntax (Toggle Plain Text)
  1. int P[] =
  2. {
  3. {0, 1, 1, 2},
  4. {0, 3, 1, 0}
  5. };
  6.  
  7. for (int x = 0; x < 2; ++x)
  8. {
  9. for (int y = 0; y < 4; ++y)
  10. cout << P[x][y] << ' ';
  11. cout << '\n';
  12. }
Is that right? Or am I missing something that makes your problem not so simple?
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jul 14th, 2009
0

Re: Adding an array to an element of the array

If the size is determined at runtime you need a full fleged dynamic memory version. Something like

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3.  
  4. int **allocs(int x,int y){
  5. int **ret = new int*[x];
  6. for(int i=0;i<x;i++)
  7. ret[i] = new int[y];
  8. return ret;
  9.  
  10. }
  11.  
  12.  
  13. int main(){
  14. int **array = allocs(4,5);
  15. int p = 0;
  16.  
  17. for(int i=0;i<4;i++)
  18. for(int j=0;j<5;j++)
  19. array[i][j] = p++;
  20.  
  21. }
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jul 15th, 2009
0

Re: Adding an array to an element of the array

>If the size is determined at runtime you need a full fleged dynamic memory
>version. Something like

A full fledged dynamic memory version? Is that what you call your program you just posted.
It would be so much better to rely on the standard headers than your memory leaking program.

To OP: Apart from Tom Gun advice, you may want to consider an array of a structure:
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. struct myStruct{
  3. int firstVal;
  4. float secVal;
  5. char thirdVal;
  6. long fourthVal;
  7. };
  8. int main(){
  9.  
  10. myStruct P[]= {{0, 1.1, 'a', 14554},
  11. {2, 2.1, 'b', 14254},
  12. {4, 4.1, 'c', 14254},
  13. {1, 8.1, 'd', 14254},
  14. {1, 1.5, 'e', 14154},
  15. {0, 1.4, 'f', 14214},
  16. {2, 1.4, 'g', 14224},
  17. {9, 1.7, 'h', 13254}
  18. };
  19. std::cout<<P[6].firstVal;
  20. };
This would allow you to use heterogeneous data types.
PS: If you need dynamic allocation techniques, consider a std::vector rather than hacks such has post#2
Last edited by siddhant3s; Jul 15th, 2009 at 3:00 am.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jul 15th, 2009
1

Re: Adding an array to an element of the array

Quote ...
It would be so much better to rely on the standard headers than your memory leaking program.
Technically it leaks memory, but realistically, it is probably not going to be run on an OS that does not free memory when a process ends. As an example of how to allocate memory for a 2D array using new[], the code is fine.

It could be better, of course. As an example of both allocating and freeing, the code is more complete and even the technical memory leak is plugged. Good examples of heap memory should free as well as allocate. Here is the better code using the same style as monkey_king:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3.  
  4. int **allocs(int x,int y){
  5. int **ret = new int*[x];
  6. for(int i=0;i<x;i++)
  7. ret[i] = new int[y];
  8. return ret;
  9.  
  10. }
  11.  
  12.  
  13. void deallocs(int **array, int x){
  14. for(int i=0;i<x;i++)
  15. delete[] array[i];
  16. delete[] array;
  17.  
  18. }
  19.  
  20.  
  21. int main(){
  22. int **array = allocs(4,5);
  23. int p = 0;
  24.  
  25. for(int i=0;i<4;i++)
  26. for(int j=0;j<5;j++)
  27. array[i][j] = p++;
  28.  
  29. // check the array
  30. for(int i=0;i<4;i++){
  31. for(int j=0;j<5;j++)
  32. std::cout << array[i][j] << ' ';
  33. std::cout << '\n';
  34. }
  35.  
  36. deallocs(array, 4);
  37.  
  38. }
I think direct heap allocation should be a last resort. There are so many safer and easier ways than allocating memory yourself, and it takes too much brainpower to make sure that you are not introducing any bugs with your memory handling. A big source of bugs comes from manually handling your memory. That is why the newer languages have garbage collection.

Quote ...
you may want to consider an array of a structure:
Quote ...
If you need dynamic allocation techniques, consider a std::vector
Good advice all around, but I would advise being careful about overengineering. Programmers are good at adding bells and whistles that do nothing but make the whole program more complex. All in the name of 'future proofing' or 'being robust', even when the value of that added complexity is questionable.

The vector class is cool, and you can use it to replace arrays most of the time. Here is the same code I posted using a vector. For the same job, it is both more work and more code to do the extra work, if just a little:
C++ Syntax (Toggle Plain Text)
  1. typedef vector<int> IntVec;
  2. typedef vector<IntVec> IntVec2D;
  3.  
  4. int data[] = {0, 1, 1, 2, 0, 3, 1, 0};
  5. IntVec2D P(2);
  6.  
  7. P[0] = IntVec(data, data + 4);
  8. P[1] = IntVec(data + 4, data + 8);
  9.  
  10. for (IntVec2D::size_type x = 0; x < P.size(); ++x)
  11. {
  12. for (IntVec::size_type y = 0; y < P[x].size(); ++y)
  13. cout << P[x][y] << ' ';
  14. cout << '\n';
  15. }
C++0x will make that painful initialization step easier and more like the array version. But my point is that sometimes all you really need is an array. If there are only 8 values, heap memory is an extra point of failure and another thing to worry about. If the values are only ints, code to handle heterogeneous types adds no value. 'What if?' by itself is not a good reason to add complexity.

I think a better defense for the array of structs is abstraction. If the values are in groups of 4, they're related somehow, and wrapping them in an object might make the code using the groups more clear:
C++ Syntax (Toggle Plain Text)
  1. class Group // placeholder for a better name
  2. {
  3. int _data[4];
  4. public:
  5. // placeholder for a better constructor
  6. Group(int a, int b, int c, int d)
  7. {
  8. _data[0] = a;
  9. _data[1] = b;
  10. _data[2] = c;
  11. _data[3] = d;
  12. }
  13.  
  14. friend ostream& operator<<(ostream& os, const Group& g)
  15. {
  16. for (int x = 0; x < 4; ++x)
  17. {
  18. os << g._data[x];
  19.  
  20. if (x < 3) os << ' ';
  21. }
  22.  
  23. return os;
  24. }
  25. };
  26.  
  27. Group P[] =
  28. {
  29. Group(0, 1, 1, 2),
  30. Group(0, 3, 1, 0)
  31. };
  32.  
  33. for (int x = 0; x < 2; ++x) cout << P[x] << '\n';
It is up to the programmer to decide if the abstraction is worth the code needed to implement the abstraction. dchunt did not give enough detail to know if an abstraction is worth the effort. It is possible that all he needs is an array, and anything more is unnecessary fluff.

Another situation came to mind when writing this post. If dchunt needs P to stay a 1D array, a second array of pointers into P can be used to achieve the same effect as a 2D array:
C++ Syntax (Toggle Plain Text)
  1. int P[] = {0, 1, 1, 2, 0, 3, 1, 0};
  2. int *q[] = {&P[0], &P[4]};
  3.  
  4. for (int x = 0; x < 2; ++x)
  5. {
  6. for (int y = 0; y < 4; ++y)
  7. cout << q[x][y] << ' ';
  8. cout << '\n';
  9. }
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009

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: exe not working
Next Thread in C++ Forum Timeline: Confusions about the constructor of a class.





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


Follow us on Twitter


© 2011 DaniWeb® LLC