| | |
problem creating a for loop for an array
![]() |
•
•
Join Date: Oct 2004
Posts: 26
Reputation:
Solved Threads: 0
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
.....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;}
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
.....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;}
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: 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
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++)
Hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Oct 2004
Posts: 26
Reputation:
Solved Threads: 0
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
* * * * * *
* * * * * *
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
Ah, Simple.
We would have to break this up into different pieces of code. The first piece would be: 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: We add this after our initialization. Hope this helps, and makes sense.
If you have further questions, please feel free to ask.
- Stack Overflow
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]='*';
// 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; }
If you have further questions, please feel free to ask.
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
Similar Threads
- how to use ctime ,random function,for loop and array??? (C++)
- char** for string array problem (C)
- problem in creating menu bar in asp.net2003 (ASP.NET)
- Problem with string search of an array (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ raster files (writing structs to a binary file)
- Next Thread: for loop help
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets





