11 Replies - 6973 Views - Last Post: 02 December 2008 - 09:37 AM Rate Topic: -----

#1 kingeedorah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-November 08

Exit "FOR" loop if number 0 is entered

Post icon  Posted 28 November 2008 - 03:35 PM

Hello!

1st of all, I'm new to the forums, and I would like to say that it's great and it helped me alot on many projects! :D

The problem is that I have to exit the for loop if user enters 0, but now it writes 0 for the whole lenght of field. :blush:

			

			int[] numbers = new int[10];
			int a, b, temp, length= 10;

			
			for (int i = 0; i < 5; indeks++)
				{
					Console.WriteLine("Enter {0}. number: ", i + 1);
					numbers[i] = int.Parse(Console.ReadLine());  
					
					if (numbers[i] == 0)
					{
					break;
					}
				}
			
			for (a = 1; a < length; a = a + 1)
			{
				for (b = length- 1; b >= a; b = b - 1)
				{
				if (numbers[b - 1] > numbers[b])
					{
						temp= numbers[b - 1];
						numbers[b - 1] = numbers[b];
						numbers[b] = temp;
					}
				}
			}

			for (int i = 0; i < length; i++)
			{
				Console.Write(" " + numbers[i]);
			}


Is This A Good Question/Topic? 0
  • +

Replies To: Exit "FOR" loop if number 0 is entered

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9576
  • View blog
  • Posts: 36,273
  • Joined: 12-June 08

Re: Exit "FOR" loop if number 0 is entered

Posted 28 November 2008 - 04:53 PM

Change this:
for (int i = 0; i < 5; indeks++)


To this:
for (int i = 0; i < 5; i++)

Was This Post Helpful? 0
  • +
  • -

#3 kingeedorah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 29 November 2008 - 03:36 AM

I forgot to change this when I was translating it to english. Otherwise I got it right :)
Was This Post Helpful? 0
  • +
  • -

#4 kingeedorah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 29 November 2008 - 03:46 PM

anyone? :/
Was This Post Helpful? 0
  • +
  • -

#5 WushuMonster  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 187
  • Joined: 29-February 08

Re: Exit "FOR" loop if number 0 is entered

Posted 29 November 2008 - 04:21 PM

It writes a bunch of zeros before the numbers the user enters because you set the array to 10 elements, but your loop for getting user input only goes for 5. So when you display the output all the unused elements are zero.
Was This Post Helpful? 0
  • +
  • -

#6 oasisjoel  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 274
  • Joined: 11-September 08

Re: Exit "FOR" loop if number 0 is entered

Posted 30 November 2008 - 12:34 AM

if (condition) then

{
exit for;
}
Was This Post Helpful? 0
  • +
  • -

#7 kingeedorah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 30 November 2008 - 06:37 AM

			int[] numbers = new int[10];
			int a, b, temp, length= 10;

			
			for (int i = 0; i < 10; indeks++)
				{
					Console.WriteLine("Enter {0}. number: ", i + 1);
					numbers[i] = int.Parse(Console.ReadLine());  
					
					if (numbers[i] == 0)
					{
					break;
					}
				}



ok it should be like this... (lots of errors at translating the code to english, now onder I wrote it at 2:35 :D )

But still, if length is 5, and for loop condition is up to 5, and lets say 3rd number that user enters is 0, it will still output 3 zeros and other 2 numbers.
Was This Post Helpful? 0
  • +
  • -

#8 WushuMonster  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 187
  • Joined: 29-February 08

Re: Exit "FOR" loop if number 0 is entered

Posted 30 November 2008 - 10:03 AM

In C++ I would create a dynamic array (an array that can take an unknown number of elements). I'm not sure how to do that in C# yet though :-P If you hard code a specific number of elements into an array then all the elements have to have values.

BTW, the easiest way to end a for loop at a certain moment is to just set the counter to whatever your cut off is.

Example:

for (i = 0; i < 10; ++i)
<code to get input>
if input = 0
i = 10

Or you could set up a while loop.

quit = true
while (!quit)
get input
if input = 0
quit = true
else
add number to array
increment array

This post has been edited by WushuMonster: 30 November 2008 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

#9 kingeedorah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 01 December 2008 - 09:48 AM

hm... well I've been thinking.... what if I could reduce the array length for each number.

If 2nd number = 0 then reduce length by 4
If 3th number = 0 then reduce length by 3
If 4th number = 0 then reduce length by 2
If 5th number = 0 then reduce length by 1
and so on...

But I dont have a clue how I could implement it in my code with a loop :unsure:

				

if (števila[indeks] == 0)
{
	  dolžina = dolžina - 1; // what a hell :D
	  break;
}



This post has been edited by kingeedorah: 01 December 2008 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#10 fremgenc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 119
  • Joined: 15-November 07

Re: Exit "FOR" loop if number 0 is entered

Posted 01 December 2008 - 01:42 PM

			

			int[] numbers = new int[10];
			int a, b, temp, length= 10;

			
			for (int i = 0; i < 5; indeks++)
				{
					Console.WriteLine("Enter {0}. number: ", i + 1);
					numbers[i] = int.Parse(Console.ReadLine());  
					
					if (numbers[i] == 0)
					{
					break;
					}
				}
			
			for (a = 1; a < length; a = a + 1)
			{
				for (b = length- 1; b >= a; b = b - 1)
				{
				if (numbers[b - 1] > numbers[b])
					{
						temp= numbers[b - 1];
						numbers[b - 1] = numbers[b];
						numbers[b] = temp;
					}
				}
			}

			for (int i = 0; i < length; i++)
			{
				Console.Write(" " + numbers[i]);
			} 



try this, i starred changes i made to your code
			

			int[] numbers = new int[10];
			int a, b, temp, 
			int length= 0;//****

			
			for (int i = 0; i < 10; i++)
				{
					Console.WriteLine("Enter {0}. number: ", i + 1);
					numbers[i] = int.Parse(Console.ReadLine());  
					
					if (numbers[i] == 0)
					{
					break;
					}

					  length++; //*******					 

				}
			
			for (a = 0; a < length; a++)
			{
				for (b = length- 1; b >= a; b = b - 1)
				{
				if (numbers[b - 1] > numbers[b])
					{
						temp= numbers[b - 1];
						numbers[b - 1] = numbers[b];
						numbers[b] = temp;
					}
				}
			}

			for (int i = 0; i < length; i++)
			{
				Console.Write(" " + numbers[i]);
			}


that should do the trick. The thing you were missing was the length++ part in your first loop. This will increment the length for each good number(NON-Zero) input.

then when sorting, it will only go up to the amount of numbers put it
and same for displaying.
Was This Post Helpful? 0
  • +
  • -

#11 kingeedorah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 02 December 2008 - 07:38 AM

Thanks alot for your help mates!!! Works like a charm :D

@fremgenc
Only had to change a from 0 to 1 in bubble sort, 1st for loop :)
Was This Post Helpful? 0
  • +
  • -

#12 fremgenc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 119
  • Joined: 15-November 07

Re: Exit "FOR" loop if number 0 is entered

Posted 02 December 2008 - 09:37 AM

Ahh yes I thought it was your mistake, evidently mine. Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1