Computer Science Canada Formatting Output and Type Conversions |
Author: | BigBear [ Fri Feb 08, 2008 3:31 pm ] | ||||||
Post subject: | Formatting Output and Type Conversions | ||||||
This Tutorial will explain how to formate the output of Strings, Integers and Reals and how to convert variable from one type to another. Formatting Output Strings %Output put "Hello" :10 Hello_ _ _ _ _ % 10 characters put "Hello" : 2 Hello % 5 characters Integers put 541 : 7 _ _ _ _ 541 % 7 characters put 541 : 1 541 % 3 characters Reals put 3.4 : 6 _ _ _ 3.4 % 6 characters put 3.4 : 0 : 3 3.400 put 3.4 : 10 : 3 _ _ _ _ _ 3.400 %10 characters put 34 : 0 : 3 : 2 3.400e+01 % Sci. Notation -------------------------------------------------------------------------------------------------------- Type Conversion Type conversion means converting a variable from one type to another. Real to Integer %Output ceil(x) - returns the smallest integer not less than x. ceil(5.3) 5 ceil(-4.1) -4 floor(x) - returns the largest integer not greater than x. floor(8.8) 8 floor(-0.1) -1 round(x) - rounds to the nearest integer round(3.1) 3 round(3.5) 4 Integer to Real %Output intreal(x) - returns the value of the integer as a real. intreal(25) 25.0 Integer to String %Output intstr(10) "10" String to Integer %Output strint(x) - converts a string x to its corresponding integer. strint("100") 100 String to Real %Output strreal("7.4") 17.4 Using strintok to Error Trap When you prompt the user for a integer you usually want it to be within say between 0 and 100 if you want to calculate the average mark across 4 tests. Your code will look like this
We could put a loop on each mark to identify the error immediately
Just repeat this for each mark we would the user to enter But what happens if the user enters "hello"? It crashes so we use strintok Basically the program looks the same but we declare the variables as string
|
Author: | BigBear [ Fri Feb 08, 2008 3:36 pm ] |
Post subject: | Re: Formatting Output and Type Conversions |
While I was making this I was wondering how do I make code look that in appears in Turing? |
Author: | Nick [ Fri Feb 08, 2008 3:51 pm ] |
Post subject: | Re: Formatting Output and Type Conversions |
[syntax="turing"][/syntax] |
Author: | BigBear [ Fri Feb 08, 2008 4:02 pm ] |
Post subject: | Re: Formatting Output and Type Conversions |
Really then what is the point of have the [code] business? |
Author: | Nick [ Fri Feb 08, 2008 4:34 pm ] |
Post subject: | RE:Formatting Output and Type Conversions |
psuedo code, small lenghs of code, formatting an ASCII image (since codes font keeps everything spaced), identation, etc... |
Author: | HeavenAgain [ Fri Feb 08, 2008 4:55 pm ] | ||
Post subject: | RE:Formatting Output and Type Conversions | ||
for formatting, you can just briefly explain what is right justified, and left justified, and where they are used, on numbers and string etc and also Quote: ceil(x) - returns the smallest integer not less than x.
ceil(5.3) 5 ceil(-4.1) -4 floor(x) - returns the largest integer not greater than x. floor(8.8) 8 floor(-0.1) -1 have a look at that again ![]() and the if statement in your code looks awfully long, and didn't really show how to use strintok, instead try something like
|