Click here to Skip to main content
15,942,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
i am new to C++. I have to store data which consists of one main part and further sub-parts e.g a laptop model and its specifications. Can i use a 2D array for this ?

rough pseudocode :

Array[n][m] ;

where n is the total number of models and m is the number of specifications(4 to 5 different) that are to be stored ..
and secondly how can i generate 16 random integers from 0-16 in such a way that none of the integers is repeated ?

Help Awaited !

1 solution

1. Yes, you may use an array that way. However it might be a non-optimal solution: suppose you have just two models, one having 3 specifications and the other having 5 specifications then you have to declare the array this way:
Laptop laptop[2][5];

wasting the memory required 3 'laptop' objects.
I suggest you to use one of the STL container classes, like, for instance std::vector.

2. I wrote a trick for a very similar task: "Random extraction of 5 cards from a deck"[^].
Share this answer
Ghost_x 21-Dec-11 12:54pm    
And what if the number of elements are fixed..
i mean in all cases there are 5 specifications, should i still prefer vectors ?

and thanks alot for the random number extraction :)
CPallini 21-Dec-11 13:09pm    
Nope: you might prefer arrays in this case (you might prefer arrays anyway if you are dealing with few items).
You are welcome.
Monjurul Habib 22-Dec-11 14:30pm    
CPallini 22-Dec-11 15:20pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900