User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 425,971 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,678 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2388 | Replies: 4
Reply
Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

problem creating a for loop for an array

  #1  
Oct 27th, 2004
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;}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: problem creating a for loop for an array

  #2  
Oct 27th, 2004
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
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
Reply With Quote  
Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

Re: problem creating a for loop for an array

  #3  
Oct 27th, 2004
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
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: problem creating a for loop for an array

  #4  
Oct 27th, 2004
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
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
Reply With Quote  
Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

Re: problem creating a for loop for an array

  #5  
Oct 27th, 2004
ty stack...it works
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:04 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC