Storing Array in Array ?

Im a new learner and yet, I have to create a big (I guess) program.
for this part, the idea is to show a table, and I manage to create it somehow it took a lot of space (lines) and it isn't convenient at all, so I try to create the table by using loop.

However, this is where all the trouble appear I don't know how change an array within a loop, this is my coding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std ;
using namespace System ;

void userInterface() ;

int main ()
{
	userInterface() ;
	system ("pause") ;
	return 0 ;
}

void userInterface()
{	
	string topBar[] = {"-BRAND-","-MODEL-","-FREQUENCY-","-CORE-","-THREAD-","-PRICE-"} ;
	string gCard1[] = {"INTEL","Dualcore G630","2.70","2","2","164"} ;
	string gCard2[] = {"Ivybridge","Xeon E3-1245V2","3.40","4","8","865"} ;
	string gCard3[] = {"Ivybridge","Core i3 - 3220","3.30","2","4","359"} ;
	string gCard4[] = {"Ivybridge","Core i5 - 3570","3.40","4","4","564"} ;
	string gCard5[] = {"Ivybridge","Core i7 - 3770","3.40","4","8","889"} ;
	int gCardNull[99] = {topBar[],gCard1[],gCard2[],gCard3[],gCard4[],gCard5[]} ;
	int i, j ;
	int x ;

	for (i=0 ; i<6 ; i++)
	{
		if(i==1)
		{
			cout.width(25) ;
			x = 18 ;
		}
		else
		{
			cout.width(15) ;
			x = 12 ;
		}
		cout<< setiosflags( ios::left )
			<< setw(x) ;

			for (j=0 ; j<6 ; j++)
			{
				cout << gCardNull[j] ;
			}
	}
	cout << endl ;
}


and this is actually what I want to create, but this 1 is too long

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std ;
using namespace System ;

void userInterface() ;

int main ()
{
	userInterface() ;
	system ("pause") ;
	return 0 ;
}

void userInterface()
{	
	string topBar[] = {"-BRAND-","-MODEL-","-FREQUENCY-","-CORE-","-THREAD-","-PRICE-"} ;
	string gCard1[] = {"INTEL","Dualcore G630","2.70","2","2","164"} ;
	string gCard2[] = {"Ivybridge","Xeon E3-1245V2","3.40","4","8","865"} ;
	string gCard3[] = {"Ivybridge","Core i3 - 3220","3.30","2","4","359"} ;
	string gCard4[] = {"Ivybridge","Core i5 - 3570","3.40","4","4","564"} ;
	string gCard5[] = {"Ivybridge","Core i7 - 3770","3.40","4","8","889"} ;
	int i ;
	int x ;

	for (i=0 ; i<6 ; i++)
	{
		if(i==1)
		{
			cout.width(25) ;
			x = 18 ;
		}
		else
		{
			cout.width(15) ;
			x = 12 ;
		}
		cout<< setiosflags( ios::left )
			<< setw(x) << topBar[i] ;
	}
	cout << endl ;

	for (i=0 ; i<6 ; i++)
	{
		if(i==1)
		{
			cout.width(25) ;
			x = 18 ;
		}
		else
		{
			cout.width(15) ;
			x = 12 ;
		}
		cout<< setiosflags( ios::left )
			<< setw(x) << gCard1[i] ;
	}
	cout << endl ;

	for (i=0 ; i<6 ; i++)
	{
		if(i==1)
		{
			cout.width(25) ;
			x = 18 ;
		}
		else
		{
			cout.width(15) ;
			x = 12 ;
		}
		cout<< setiosflags( ios::left )
			<< setw(x) << gCard2[i] ;
	}
	cout << endl ;

	for (i=0 ; i<6 ; i++)
	{
		if(i==1)
		{
			cout.width(25) ;
			x = 18 ;
		}
		else
		{
			cout.width(15) ;
			x = 12 ;
		}
		cout<< setiosflags( ios::left )
			<< setw(x) << gCard3[i] ;
	}
	cout << endl ;

	for (i=0 ; i<6 ; i++)
	{
		if(i==1)
		{
			cout.width(25) ;
			x = 18 ;
		}
		else
		{
			cout.width(15) ;
			x = 12 ;
		}
		cout<< setiosflags( ios::left )
			<< setw(x) << gCard4[i] ;
	}
	cout << endl ;

	for (i=0 ; i<6 ; i++)
	{
		if(i==1)
		{
			cout.width(25) ;
			x = 18 ;
		}
		else
		{
			cout.width(15) ;
			x = 12 ;
		}
		cout<< setiosflags( ios::left )
			<< setw(x) << gCard5[i] ;
	}
	cout << endl ;

}
Last edited on
THis is a great place to introduce structures:
1
2
3
4
5
6
7
8
9
struct Card 
{
  string brand;
  string model;
  float frequency;
  int core;
  int thread;
  float price;
}


Then you can define your inventory like so:
1
2
3
4
5
6
7
8
9
10
string topBar[] = {"-BRAND-","-MODEL-","-FREQUENCY-","-CORE-","-THREAD-","-PRICE-"} ;

Card inventory[] = 
{
  {"INTEL"     ,"Dualcore G630"   ,2.70f,2,2,164.00f},
  {"Ivybridge","Xeon E3-1245V2",3.40f,4,8,865.00f},
  {"Ivybridge","Core i3 - 3220"   ,3.30f,2,4,359.00f},
  {"Ivybridge","Core i5 - 3570"   ,3.40f,4,4,564.00f},
  {"Ivybridge","Core i7 - 3770"   ,3.40f,4,8,889.00f}
};


Then to sort it make your own function like this to detemine which comes first:
1
2
3
4
bool SortByCores(Card& a, Card& b)
{
  return (a.core < b.core);
}


And this is how to sort! (#include <algorithm>)
std::sort ( inventory, inventory + 5, SortByCores);
Last edited on
But to answer your question directly, you can do this:
1
2
3
4
5
6
7
	string topBar[] = {"-BRAND-","-MODEL-","-FREQUENCY-","-CORE-","-THREAD-","-PRICE-"} ;
	string gCard1[] = {"INTEL","Dualcore G630","2.70","2","2","164"} ;
	string gCard2[] = {"Ivybridge","Xeon E3-1245V2","3.40","4","8","865"} ;
	string gCard3[] = {"Ivybridge","Core i3 - 3220","3.30","2","4","359"} ;
	string gCard4[] = {"Ivybridge","Core i5 - 3570","3.40","4","4","564"} ;
	string gCard5[] = {"Ivybridge","Core i7 - 3770","3.40","4","8","889"} ;
	string* gCardNull[] = {topBar,gCard1,gCard2,gCard3,gCard4,gCard5} ;
Last edited on
Thanks Stewbond, at least I understand the concept that array can be placed inside another array, but referring to your reply above, actually, I learnt C++ around 2+ months and we need to come out with some kind of program, and we haven't learnt the part & like u use above

these are the topics we had covered
-basic (syntax, keywords bla bla bla ...)
-loop
-function & prototype
-array (multi dimensional)

but, I did some researches and try to learn new stuff even though it haven't be taught yet

if I want to display something in table, which 1 is better ?
using basic cout n type everything there or using this method ? because I want to save it at certain element
Last edited on
Topic archived. No new replies allowed.