6 Replies - 875 Views - Last Post: 07 November 2009 - 11:29 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 408
  • Joined: 14-December 08

Result of For Loop

Posted 05 November 2009 - 04:31 PM

Chao theory.

Take a double or float value X between 0 and 1 as a starting value. Generate a new value for X by the equation: X = 2 * X * (1 - X); Display X. Write a program that asks the user for a starting value X, then perform the above process in loop that runs through 50 cycles, while display the value of X.

Read my question. I HAVE DONE THIS ALREADY. This seems pretty easy to do. But after I did everything correctly, I got the very strange result.

The result I got is:
X = 1.xxxxxxxxxxxxxxxx + Exxx;
X = -infinity.

I got all the minus(- infinity) infinity result. I hope you guys could do this thing and run this code and tell me what the result you guys got.

Console.Write("enter a number: ");
int X = int.Parse(Console.ReadLine());   

for(int i = 1; i <= 50; i++)
{
  X = 2 * X * (1 - X);
  Console.Write(X);
}


This post has been edited by kenryuakuma: 05 November 2009 - 04:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Result of For Loop

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Result of For Loop

Posted 05 November 2009 - 06:13 PM

What's the very first line of your problem? What did you declare X as?
Was This Post Helpful? 0
  • +
  • -

#3 KeyboardKowboy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 142
  • Joined: 15-December 08

Re: Result of For Loop

Posted 05 November 2009 - 09:02 PM

Quote

Take a double or float value X between 0 and 1 as a starting value.

Why are you parsing X as an int if you specify that it should be a double or a float?
Was This Post Helpful? 0
  • +
  • -

#4 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 408
  • Joined: 14-December 08

Re: Result of For Loop

Posted 05 November 2009 - 11:02 PM

oh mistake, yes it should be float and double. SORRY! Yes, the program code was double when I did it but I forgot to use double to post my problem because I get used to int already.
Was This Post Helpful? 0
  • +
  • -

#5 KeyboardKowboy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 142
  • Joined: 15-December 08

Re: Result of For Loop

Posted 05 November 2009 - 11:21 PM

Ok... Well I'm still not sure what you are trying to do, but I took your code and modified it so that it actually read in a double value.
I also chose to output a WriteLine, instead of Write, for readability.

class Program
	{
		static void Main(string[] args)
		{
			Console.Write("enter a number: ");
			double X = double.Parse(Console.ReadLine());

			for (int i = 1; i <= 50; i++)
			{
				X = 2 * X * (1 - X);
				Console.WriteLine(X);
			}
		}
	}



The results are below: (click to expand image)

Attached Image
Was This Post Helpful? 0
  • +
  • -

#6 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 408
  • Joined: 14-December 08

Re: Result of For Loop

Posted 07 November 2009 - 11:22 PM

Thanks the problem has been resolved in class. However, if you enter a great or big number, the result is gonna infinity and is kind of weird.
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Result of For Loop

Posted 07 November 2009 - 11:29 PM

That's why the problem says a number between 0 and 1 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1