Thread: Lesson #3 - Math

  1. #1
    Registered User oval's Avatar
    Join Date
    Jan 2006
    Location
    Texas
    Posts
    10

    Lesson #3 - Math

    In this sample console app program, we will do a few things (just as a reminder, I am using Visual Studio)
    - take a numerical user input and convert it to an integer
    - generate a random number
    - take a look at a simple if statement
    - and add 2 numbers.

    Before we begin, I would like to note that the only instance of math used is addition. However you might want to explore further by substituting the operators. (subtraction (-), multiplication (*) and division(/))

    I would also like to note that when doing division, if variabes of int type are used you will not get any remainders (i.e. n.345984) To be able to work with real numbers, numbers with decimals, you can use float. Below is a way to declare a float and assign it a value:

    float floatNumber;

    floatNumber = 22.34f;

    Code:
     
    using System;
     
     
     
    namespace lessonThree
     
    {
     
    	 ///<summary>
     
    	 /// In this lesson we will cover some basic math
     
    	 /// operations, look at an if statement and a few other
     
    	 /// useful things that should be understood when
     
    	 /// programming in C#
     
    	 ///</summary>
     
    	 class thirdLesson
     
    	 {
     
    			///<summary>
     
    			/// Ask an age.
     
    			/// If under 18 -> Exit Program
     
    			/// Give lucky number for the day
     
    			/// Predict when person will get rich.
     
    			///</summary>
     
    			[STAThread]
     
    			static void Main(string[] args)
     
    			{
     
    				 int age;
     
    				 //Random object createRand
     
    				 Random createRand = new Random();
     
    				 int luckyNumber;
     
     
     
    				 Console.Write("Enter your age: ");
     
    				 //grab the input from user and convert it to
     
    				 //an integer, hence the 'Convert.ToInt32();
     
    				 age = Convert.ToInt32(Console.ReadLine());
     
     
     
    				 //skip line
     
    				 Console.WriteLine();
     
    
     
    				 if (age > 17)
     
    				 {
     
    
     
    				 //Random generated numbers range from 0 to 1, 
     
    				 //therefore we multiply by ten (10) below
     
    				 //The .NextDouble() method helps retrieve the value
     
    				luckyNumber = (int)(createRand.NextDouble()*10);
     
    
     
    				Console.WriteLine("Your Lucky Number is: {0}", luckyNumber);
     
    				//Here you use the value of luckyNumber and add it to his age
     
    			 Console.WriteLine("You are {0} years old and will be rich at age: {1}", age, age+luckyNumber);
     
    			 //As you can see the zero in braces {0} holds the variable age
     
    			 //and the one in braces {1} holds the sum of the variables "age and luckyNumber" age+luckyNumber
     
    
     
    				 }//If the age is not 18 or above we conclude by saying
     
    
     
    				 Console.WriteLine("Thanks and Have a Great Day");
     
    				 Console.WriteLine();
     
    				 Console.WriteLine("Press [ENTER] to Exit");
     
    				 Console.ReadLine();
     
     
     
    			}//end Main()
     
    	 }//end class
     
    }//end namespace 
    As you can see this code was commented quite a bit. I will probably do a few more console app lessons and move onto windows apps. In the mean time I hope this is useful to someone.

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    
    namespace TestRand {
    
    	public class Program {
    
    		private Random randomField = new Random();
    
    		static void Main(string[] args) {
    			Program o = new Program();
    
    			//Start generating 10 random numbers
    			Console.WriteLine("Random from within main");
    			Random randinMain = new Random();
    			for (int i = 0; i < 10; i++) {
    				Console.WriteLine(randinMain.Next(100));
    			}
    
    			Console.WriteLine("Random from within function");
    			o.RandinFunction();
    
    			Console.WriteLine("Random with function random object passed as argument to function");
    			o.RandWithField(o.randomField);
    
    			Console.WriteLine("Random using a field no function");
    			for (int i = 0; i < 10; i++) {
    				Console.WriteLine(o.randomField.Next(100));
    			}
    
    			Console.WriteLine("Random using a field - function");
    			o.RandWithField(o.randomField);
    			
    			Console.ReadLine();
    
    		}
    
    		/// <summary>
    		/// generate 10 random numbers with random object in function
    		/// </summary>
    		public void RandinFunction() {
    			Random randInFunction = new Random();
    			for (int i = 0; i < 10; i++) {
    				Console.WriteLine(randInFunction.Next(100));
    			}
    		}
    		
    		/// <summary>
    		/// generate 10 random numbers with random object from field
    		/// </summary>
    		/// <param name="r"></param>
    		public void RandWithField(Random r) {
    			for (int i = 0; i < 10; i++) {
    				Console.WriteLine(r.Next(100));
    			}			
    		}
    
    
    	}
    }
    Now look at the output:
    Random from within main
    10
    42
    94
    90
    12
    24
    57
    21
    33
    99
    Random from within function
    10
    42
    94
    90
    12
    24
    57
    21
    33
    99
    Random with function random object passed as argument to function
    10
    42
    94
    90
    12
    24
    57
    21
    33
    99
    Random using a field no function
    55
    88
    49
    63
    39
    17
    92
    72
    83
    88
    Random using a field - function
    27
    1
    33
    67
    46
    14
    34
    1
    99
    36
    What im trying to point out is that it's best to use a random field in your class rather then having a local random object ( either in main or in some function ).

    Just wanted to add this since I've come across this thing several times and people keep wondering why they always have the same "random" numbers, it probably has something to do with how the rand is seeded but anyway ... solution is to just use a field instead of a local object.

  3. #3
    Registered User oval's Avatar
    Join Date
    Jan 2006
    Location
    Texas
    Posts
    10
    Thanks for the additional info GL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM