DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   problem creating a for loop for an array (http://www.daniweb.com/forums/thread13021.html)

SyLk Oct 27th, 2004 2:15 pm
problem creating a for loop for an array
 
sup guys
im trying to implement a nested for loop for a 2 dimension array that initializes the array to the character '*'. but its just printing out garbage, i think im messing up on my syntax :sad:.....some help would be appreciated

here is what i got so for:

char initialize[12][5];
int row;
int col;

for(row=0;row<13;row++)
{for(col=0;col<6;col++)
initialize[row][col]='*';
cout<<initialize[row][col]<<endl;}

Stack Overflow Oct 27th, 2004 2:30 pm
Re: problem creating a for loop for an array
 
Greetings,

This can be fixed quite easily. Array subscripts always start at zero in C, so elements are initialize[0], initialize[1], ..., initialize[11]. Meaning, you cannot write to the array index of 12, since it does not exist. To break it down, take the number 12. Start at 0 and count up 12 times, you come up with 11. Your program crashes because you are trying to write out-of-bounds to the array. You can simply fix it by changing the number within your for loop:
for(row=0;row<12;row++)
        for(col=0;col<5;col++)
The reason this works is because you are starting at 0, and looping to 11. The last number before 12 is 11, and since we did < 12 instead of <= 12 your loop will stop at 11. That applies to row of course, and the same concept with column.

Hope this helps,
- Stack Overflow

SyLk Oct 27th, 2004 2:49 pm
Re: problem creating a for loop for an array
 
ty stack...i changed the values like u suggested...but im still unable to get the desired results. it should be looking like:

* * * * * *
* * * * * *
etc........but it still gives me a single column of junk......i get the feelin my parenthesis might be wrong....and i've tried changin them to see if my results change but it just does'nt work

Stack Overflow Oct 27th, 2004 3:07 pm
Re: problem creating a for loop for an array
 
Ah, Simple.

We would have to break this up into different pieces of code. The first piece would be:
for(row=0;row<12;row++)
        for(col=0;col<6;col++)
                initialize[row][col]='*';
The reason we don't print here is because we are going to need a different way and concept to print all 6 columns, but only 2 rows of each. That's why we are going to take another approach. Firstly, lets create another integer called i. Now, lets make a loop out of it. We will start at 0, and loop until 1; or in this case < 2. Inside this loop we will call on our column loop to loop through all 6 characters, print it and add a space at the end. Each time our first loop passes, we will add a new line. If this makes sense, then this should too:
// Two rows
for(i=0;i<2;i++) {
        // Six columns
        for(col=0;col<6;col++)
                cout << initialize[i][col] << " ";
        // Six columns are over; time for new line
        cout << endl;
}
We add this after our initialization. Hope this helps, and makes sense.

If you have further questions, please feel free to ask.


- Stack Overflow

SyLk Oct 27th, 2004 3:28 pm
Re: problem creating a for loop for an array
 
ty stack...it works


All times are GMT -4. The time now is 1:26 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC