2D array and nested for loop

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 7
Reputation: rikkie is an unknown quantity at this point 
Solved Threads: 0
rikkie's Avatar
rikkie rikkie is offline Offline
Newbie Poster

2D array and nested for loop

 
-1
  #1
Apr 12th, 2008
Hi guys,

I'm trying to use a nested for loop to change the first 'o' it finds while testing to an 's' and then exit the loop immediately after that. This is what I have, but it currently changes every second 'o' to 's'

  1. for (int i = 0; i <8; i++)
  2. {
  3. for (int j = 1; j<4; j++)
  4. if (tickets[i][j] == 'o')
  5. tickets[i][j++] = 's';
  6. }
  7. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: 2D array and nested for loop

 
0
  #2
Apr 12th, 2008
Why are there two j++ in your code?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: rikkie is an unknown quantity at this point 
Solved Threads: 0
rikkie's Avatar
rikkie rikkie is offline Offline
Newbie Poster

Re: 2D array and nested for loop

 
0
  #3
Apr 12th, 2008
Originally Posted by Salem View Post
Why are there two j++ in your code?
Hi Salem,

This is where I'm going wrong (I think) but not sure if this is even the right approach for what I'm trying to do.

The function containing the for loop above prints out:

Row 1 o s o s
Row 2 o s o s
Row 3 o s o s
etc

Another function in my code takes input from users to change the chart according to the user's input. So, starting with a "clean" chart where everything has been initialised to 'o', the userinput can make changes and the chart can look like this for example:

Row 1 s s o s
Row 2 s o s s
Row 3 s s o s

So, what I'm trying to do with the for loop is to test for the first 'o', which is in Row 1, third column in this example, and to change this to 's' and then exit the loop without making further changes.

Hope that makes sense.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 21
Reputation: theausum has a little shameless behaviour in the past 
Solved Threads: 1
theausum theausum is offline Offline
Newbie Poster

Re: 2D array and nested for loop

 
0
  #4
Apr 12th, 2008
why are there 2 j++
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: 2D array and nested for loop

 
0
  #5
Apr 13th, 2008
You need some additional condition to know when you've replaced an 'o', and make the loops quit.

If this action is the sole content of a function, this might be one good case to just return immediately upon doing the change.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC