Quick question

is there a way to set a whole array to a value initially?
You can set an automatically allocated array to zero like this: int arr[100] = { 0 };

You can initialize a vector's elements to any value like this: vector<int> vec( 100, 42 ); (preallocates 100 elements and sets them all to 42).
Last edited on
There's also std::fill(), a call to which would look something like
std::fill(array,array+size,value);

Finally, there's memset(), which can set each byte to some value:
memset(array,byte_value,sizeof(*array)*size);
Last edited on
And if your array is short enough you can set each element individually: int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; If the array is longer than your list of values, the remaining elements will be set to zero (that's how int arr[100] = { 0 }; works).
Last edited on
Well if you declare a static array then it is already initialized to zero but if you are using an automatic array ( the default ) then you could either specify individually each value in the array such as int array1[5] = { 5, 6, 2, 0, 5 };, if you want to simply initialize all of the values to zero then you could use int array2[5] = {};, finally and to conclude my rather tedious entry, to set a single value to all of the arrays parts( akward wording I know), I suggest
1
2
3
4
for( count = 0; count <ARRAY_SIZE; count++ )
{
     array2[ count ] = 6;
}


edit: beaten to it
Last edited on
Well if you declare a static array then it is already initialized to zero but if you are using an automatic array


Right. I meant automatic array. I've edited my original post.
what about this? how do I "preset" this array?

1
2
3
4
grid.resize(height);

	for (int i = 0; i < height; ++i)
	grid[i].resize(length);


sorry would it be?

1
2
3
4
 

grid[height][length]={2};
Last edited on
If grid is a std::vector<> then resize will resize the vector to contain exactly length elements. If the resulting vector is larger than the original (ie, growing) then the "extra" elements are default constructed at the end.
If the vector is a vector<int>, then the extra elements will be initialized to zero.
Let me explain what I'm trying to do.
I want to let the user create a grid of their choice.
I then would like that grid to be set to a default value
and let the user choose where a different value goes.

here is my code so far

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
#include <iostream>
#include <vector>

using namespace std;
using std::vector;

int main()
{
	  vector<vector<char> > grid;

	int length;
	int height;
	int L;
	int H;
	char r;
	int c;
	int d;
	double per;
	double w;
	double u;
	double p;

	cout << "How long do you want your grid? "; //set up length of grid
	cin  >> length;

	cout << "How tall do you want your grid? "; //set up height of grid
	cin  >> height;

	cout << endl;

	cout << "What percentage of nodes do you want? ";
	cin  >> per;
	cout << endl;

	w = length*height;

	cout << "Out of " << w << " grid markers, you have ";
	u = (per/100);
	p = w * u;
	
	int rounded = static_cast<int>(p+.5);

	cout << rounded << " nodes." << endl;  //find out how meny nodes you need

	grid.resize(height);

	for (int i = 0; i < height; ++i)
	grid[i].resize(length);



	height = height - 1;
	length = length - 1;

	H = height;

	do									// assign grid
	{
		L = length;
		do
		{
			if (rounded > 0)
			{
				c = H + 1 ;
				d = L + 1 ;
				cout << "Grid location " << c << ", " << d 
					 << " would you like to place a node here? Y/N" << endl;
				cin  >> r;


				if (r == 'y' || r == 'Y')
				{
					grid[H][L] = '*';
					L = L - 1;
					rounded = rounded - 1;

					cout << rounded << " nodes left." << endl;
				}

				else if (r == 'n' || r == 'N')
				{
					grid[H][L] = '.';
					L = L - 1;
				}
				else
				{
					grid[H][L] = '.';
					L = L - 1;
				}
			}
			else if (rounded == 0)
			{
				grid[H][L] = '.';
				L = L - 1;
			}
		}
		while( L > -1);

		H = H - 1;
	}
	while(H > -1);

	H = height;
	L = length;

	cout << H << endl;
	cout << L << endl;

	do							//display grid
	{
		do
		{
			cout << grid[H][L];
			L = L - 1;
		}
                while ( L > -1);

		H = H - 1;
		cout << endl;
	}
	while(H > -1);

	cout << endl;



	cout << endl;

	return 0;
}


this is the result

How long do you want your grid? 4
How tall do you want your grid? 4

What percentage of nodes do you want? 100

Out of 16 grid markers, you have 16 nodes.
Grid location 4, 4 would you like to place a node here? Y/N
y
15 nodes left.
Grid location 4, 3 would you like to place a node here? Y/N
y
14 nodes left.
Grid location 4, 2 would you like to place a node here? Y/N
y
13 nodes left.
Grid location 4, 1 would you like to place a node here? Y/N
y
12 nodes left.
Grid location 3, 4 would you like to place a node here? Y/N
y
11 nodes left.
Grid location 3, 3 would you like to place a node here? Y/N
y
10 nodes left.
Grid location 3, 2 would you like to place a node here? Y/N
y
9 nodes left.
Grid location 3, 1 would you like to place a node here? Y/N
y
8 nodes left.
Grid location 2, 4 would you like to place a node here? Y/N
y
7 nodes left.
Grid location 2, 3 would you like to place a node here? Y/N
y
6 nodes left.
Grid location 2, 2 would you like to place a node here? Y/N
y
5 nodes left.
Grid location 2, 1 would you like to place a node here? Y/N
y
4 nodes left.
Grid location 1, 4 would you like to place a node here? Y/N
y
3 nodes left.
Grid location 1, 3 would you like to place a node here? Y/N
y
2 nodes left.
Grid location 1, 2 would you like to place a node here? Y/N
y
1 nodes left.
Grid location 1, 1 would you like to place a node here? Y/N
y
0 nodes left.
****
Press any key to continue . . .


so there seems to be a problem around the second do while in the display grid part but I cant figure it out.
Last edited on
Yes, you have to reassign length to L after the do loop since the first time through will cause L to be set to -1.
Oh yes thank you so much that was awesome.
You shouldn't use do-while loops when a for loop will suffice. It just complicates things. And there seems to be no reason for you to process the rows and cols backwards. Also, functions are a good thing and vectors know their size. With that in mind, try something like this:

1
2
3
4
5
6
7
8
9
typedef vector<vector<char> > Grid;

void displayGrid( Grid& grid ){
    for( int row = 0; row < grid.size(); ++row ){
        for( int col = 0; col < grid[row].size(); ++col )
            cout << grid[row][col];
        cout << '\n';
    }
}

I'm not sure how A for loop works, but ur right I did have my grid upside down but I got it fixed.
Topic archived. No new replies allowed.