| | |
Adding an array to an element of the array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 1
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 ?
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 ?
It looks like you want a 2D array:
Is that right? Or am I missing something that makes your problem not so simple?
C++ Syntax (Toggle Plain Text)
int P[] = { {0, 1, 1, 2}, {0, 3, 1, 0} }; for (int x = 0; x < 2; ++x) { for (int y = 0; y < 4; ++y) cout << P[x][y] << ' '; cout << '\n'; }
-Tommy (For Great Justice!) Gunn
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
If the size is determined at runtime you need a full fleged dynamic memory version. Something like
c++ Syntax (Toggle Plain Text)
#include <iostream> int **allocs(int x,int y){ int **ret = new int*[x]; for(int i=0;i<x;i++) ret[i] = new int[y]; return ret; } int main(){ int **array = allocs(4,5); int p = 0; for(int i=0;i<4;i++) for(int j=0;j<5;j++) array[i][j] = p++; }
>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:
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
>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)
#include <iostream> struct myStruct{ int firstVal; float secVal; char thirdVal; long fourthVal; }; int main(){ myStruct P[]= {{0, 1.1, 'a', 14554}, {2, 2.1, 'b', 14254}, {4, 4.1, 'c', 14254}, {1, 8.1, 'd', 14254}, {1, 1.5, 'e', 14154}, {0, 1.4, 'f', 14214}, {2, 1.4, 'g', 14224}, {9, 1.7, 'h', 13254} }; std::cout<<P[6].firstVal; };
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.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
•
•
•
•
It would be so much better to rely on the standard headers than your memory leaking program.
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)
#include <iostream> int **allocs(int x,int y){ int **ret = new int*[x]; for(int i=0;i<x;i++) ret[i] = new int[y]; return ret; } void deallocs(int **array, int x){ for(int i=0;i<x;i++) delete[] array[i]; delete[] array; } int main(){ int **array = allocs(4,5); int p = 0; for(int i=0;i<4;i++) for(int j=0;j<5;j++) array[i][j] = p++; // check the array for(int i=0;i<4;i++){ for(int j=0;j<5;j++) std::cout << array[i][j] << ' '; std::cout << '\n'; } deallocs(array, 4); }
•
•
•
•
you may want to consider an array of a structure:
•
•
•
•
If you need dynamic allocation techniques, consider a std::vector
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)
typedef vector<int> IntVec; typedef vector<IntVec> IntVec2D; int data[] = {0, 1, 1, 2, 0, 3, 1, 0}; IntVec2D P(2); P[0] = IntVec(data, data + 4); P[1] = IntVec(data + 4, data + 8); for (IntVec2D::size_type x = 0; x < P.size(); ++x) { for (IntVec::size_type y = 0; y < P[x].size(); ++y) cout << P[x][y] << ' '; cout << '\n'; }
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)
class Group // placeholder for a better name { int _data[4]; public: // placeholder for a better constructor Group(int a, int b, int c, int d) { _data[0] = a; _data[1] = b; _data[2] = c; _data[3] = d; } friend ostream& operator<<(ostream& os, const Group& g) { for (int x = 0; x < 4; ++x) { os << g._data[x]; if (x < 3) os << ' '; } return os; } }; Group P[] = { Group(0, 1, 1, 2), Group(0, 3, 1, 0) }; for (int x = 0; x < 2; ++x) cout << P[x] << '\n';
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)
int P[] = {0, 1, 1, 2, 0, 3, 1, 0}; int *q[] = {&P[0], &P[4]}; for (int x = 0; x < 2; ++x) { for (int y = 0; y < 4; ++y) cout << q[x][y] << ' '; cout << '\n'; }
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- Find the biggest element in that array (C++)
- Delete or Remove Array Element (C++)
- Ignore duplicate element in array and continue reading (Java)
- How do I turn this into a single dimensional array? (C#)
- keep getting "unitialized value in array element" (Perl)
- Controlling wheter an element in an array or not (Perl)
- Comparing an element in an array (C++)
- Stripping the last element off an array (Perl)
Other Threads in the C++ Forum
- Previous Thread: exe not working
- Next Thread: Confusions about the constructor of a class.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib 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 struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





