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).
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
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.
#include <iostream>
#include <vector>
usingnamespace 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;
}
elseif (r == 'n' || r == 'N')
{
grid[H][L] = '.';
L = L - 1;
}
else
{
grid[H][L] = '.';
L = L - 1;
}
}
elseif (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.
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';
}
}