Casio Z1 Gr User Manual
Have a look at the manual Casio Z1 Gr User Manual online for free. It’s possible to download the document as PDF or print. UserManuals.tech offer 338 Casio manuals and user’s guides for free. Share the user manual or guide on Facebook, Twitter or Google+.
91 Entering Values Now, let’s try a program that calculates the sine and cosine of values that you enter from the keyboard. Since we can be expecting decimal values for input and output, we will be defining the variable as floating-point. The program to accomplish this task would appear as follows: /* Compute sine & cosine */ /* #include */ /* #include */ main(){ float x; printf(“Input Value(DEG)“); scanf(“%f”,&x); angle(0); /* Degrees */ printf(“sin(%f)=%f¥n“,x,sin(x)); printf(“cos(%f)=%f¥n“,x,cos(x)); } First, we must specify the variable x as floating point. The scanf() function is used to get the entry of a value from the keyboard. Exactly the opposite as the printf() function, the first argument of the scanf() determines the type, and the value is assigned to the second argument. In the program here, we can see that the first argument of scanf() is “%f”, to indicate floating point format. The second argument is &x which is a pointer to the variable x. A pointer indicates the address in memory of a variable. The second argument of the scanf() function must be a pointer. The statement “angle(0);” specifies the unit of angular measurement. You can specify the unit by changing the argument of angle() as follows: • angle(0) – degrees • angle(1) – radians • angle(2) – grads The next statements are the now familiar printf(). After you input this program, enter the interpreter and execute it. When the computer reaches the scanf() function, it displays the message “Input Value(DEG)” and waits for input of a value. Here, let’s enter 60 and press the . . key. This value is assigned to variable “x”, and the following appears on the display. .R U N . . 60 . . sin(60.000000)=0.866025 cos(60.000000)=0.500000 >_
92 6.2.4 Using selection statements Using the “if” selection statement You can use the “if” statement to tell the computer to shift the flow of control if certain conditions are met. The “if” statement has two basic formats. 1. if (condition) statement. Here, the statement is executed if the condition is met (true = any value other than 0), and not executed if the condition is not met (false = 0). 2. if (condition) statement 1 else statement 2 In this case, statement 1 is executed if the condition is met (true = any value other than 0), while statement 2 is executed if the condition is not met (false = 0). The “if – else” statement may contain multiple statements for the “if” and the “else” statements. In the following example, please also note the proper format for easy reading. Note that the statements are aligned and also note the position of the braces. if (condition){ Statement 1 Statement 2 } else{ Statement 3 Statement 4 } A program to solve quadratic equations Here, we will create a program that produces two solutions for the following quadratic equation: ax2 + bx + c = 0 (a ≠ 0) In the above equation, it is assumed that a, b and c are real numbers. The solution for the formula is: - b ± √ (b2 – 4ac) x= 2a Also, the following is the discriminant which determines the following concerning the solutions of the above equation: • D = b2 – 4ac • D>0 – Two different real solutions • D=0 – Single real solution • D
93 /* Quadratic equation */ /* #include */ /* #include */ main(){ double a,b,c,D,q,r; scanf(“%lf %lf %lf“,&a,&b,&c); D=b*b-4.0*a*c; if (d>=0){ q=(-b+sqrt(D))/a/2.0; r=(-b-sqrt(D))/a/2.0; printf(“%If, %If¥n“,q,r); } else{ r=sqrt(-D)/a/2.0; q=-b/a/2.0; printf(“%lf+%lfi “,q,r); printf(“%lf-%lfi¥n“,q,r); } } The variables “a”, “b” and “c” correspond to the “a”, “b”, and “c” in the quadratic equation, while the “D” variable is the “D” of the discriminant. Variables “q” and “r” are used for the two solutions. Note that all of the variables are specified as “double”, meaning that they are for double-precision floating point values. Note that the % construction in the scanf() function is “%lf”. The “f” portion specifies a floating-point value, while the “l” stands for long, and means that the integer part of the value is longer than normal. Relational operators The condition “D>=0” is called a relational operator. This tells the computer to compare the value assigned to variable “D” with 0. If the value of “D” is greater than or equal to 0, a value of 1 (TRUE) is returned to indicate true. If it is less than zero, a 0 (FALSE) is returned to indicate false. The following are the other relational operators that can be used with C. Operator Example Meaning > < >= j i=j i
94 6.2.5 Using loops Using the “while” loop The “while” loop makes it possible to repeat execution of statements until a specific condition is met. The format of the “while” loop is as follows: while (condition) Statement The “while” loop first executes the condition. If the condition is met (true, returning a value other than 0), the statement is executed, and execution loops back to the “while” loop to evaluate the condition again. Whenever the condition is not met (false, returning 0), execution of the “while” loop is ended. You can have multiple statements in a while loop, but note the following format that you should use in order to make the program easier to read: while (condition){ Statement 1 Statement 2 . . Statement n } The closed brace should be directly under the “w” of the “while”. Keep the statements indented. Now, let’s create a program that uses the “while” loop. The program will output the character codes for the characters “0” through “Z” as illustrated below: Char(0) = Hex(0x30) Char(1) = Hex(0x31) … Char(Z) = Hex(0x5A) The following shows the program that produces such result. /* Character codes */ /* #include */ #define STR ‘0’ #define END ‘Z’ main(){ char ch; ch=STR; while (ch
95 Using the #define statement Lines 3 and line 4 of the program contain the #define statement. The #define statement defines a name for a particular string of characters. In the above program, “#define STR ‘0’ “ tells the computer that anytime it comes across the name “STR”, it should replace it with the character “0”. Likewise, “#define END ‘Z’ “ tells the computer that the name “END” is to be replaced by the character ‘Z’. The reason for using a #define statement is to make the program easier to read and to change later. If, in the above program, we used the character ‘0’ a number of times throughout the program and we wished to change all of them to ‘A’, it would be much easier to change the #define statement once, rather than making multiple changes. Incrementing and decrementing The program here establishes the initial value for trhe variable “ch” as “0”, and tells it to keep repeating the loop “while (ch
96 Using the “do – while” loop The “do – while” loop is another method that you can use for repeat execution. The format of the “do – while” loop is as follows: do Statement while (condition); Unlike the “while” loop, the “do – while” loop executes the statement first and then checks whether or not the condition has been met or not. Note also the semicolon at the end of the “while” line cannot be omitted. Let’s use the “do – while” to find the Greatest Common Measure for two values. /* Greatest Common Measure */ /* #include */ main(){ int gcm,x,y; x=56; y=63; printf(“¥nGCM(%d,%d) = ”,x,y); do{ gcm=x; x=y%x; y=gcm; }while (x!=0) printf(“%d¥n”,gcm); } When you execute this program, the following result should be produced: GCM(56,63) = 7 >_ Using the “for” loop You can also use the “for” loop for repeat execution of statements. The format of the “for” loop is as follows: For (expression 1; expression 2; expression 3) Statement Note that there are three expressions inside of the parentheses of the “for” loop. • Expression 1 initializes the counter variable, and is executed only once, before the first pass of the loop. • Expression 2 is the condition of the loop, so the loop continues to execute until this condition is not met. • Expression 3 performs an operation on the counter variable before the statement in the loop is executed.
98 Nested loops The term nested loop means simply “loops inside of loops”. To better understand how nested loops work, let’s have a look at a very short, simple representative program. /* nested loops example */ /* #include */ main(){ float a[3][3]; int i,j; for (i=0; i
99 We have now the value assignment nested loop we saw before, followed by a similar set of loops to read and display the values after they are stored. The only new item is the “%8.2f” which specifies that each value will be displayed in an area of at least 8 characters, with two decimal places (right flush). .R U N . . 1 . . 2 . . 3 . . 4 . . 5 . . 6 . . 7 . . 8 . . 9 . . 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 >_ 6.2.6 Defining functions Function definition and program modules Besides the standard functions, the C programming language lets you define your own routines as functions to be called by later programs. We have seen a hint of this already in the “main()” first line of our programs to define them as main functions. Using the procedures presented here, you will ba able to break large programs down into modules and then call up each module from the main() function. This means that you will eventually build up your own library of functions that you can call up from various programs as you need them. Creating functions The following is the format for function definition: function type declaration function name (arguments) argument type declaration; { declaration of variables used in the function statements… } Function type This part of the function declares the type of data that will be returned by the function. This line may be omitted if the value to be returned is an integer (int) or if there is no data returned. But a good programming discipline is to formally declare an “int” type or a “void” type when nothing is returned. Function name You cannot use reserved words as function names, and you should not use names already used for standard function names The following is a table of reserved words. auto default float register (struct) (volatile) break do for return switch while case double goto short (typedef) char else if signed (union) const (enum) int sizeof unsigned continue extern long static void The standard functions are listed on page 113
100 Arguments Arguments are used to pass values to the function when it is called. If no values are passed, the arguments can be omitted. If the arguments are omitted, the following argument type declaration is also omitted. Argument type declaration Declares the types of the arguments specified above. Note that ANSI C allows declaring arguments directly inside the parenthesis. The C interpreter does not allow such a declaration. Statements The portion of the function between the braces is executed when the function is called. Use the same format as that which we have seen for our main() programs. EXAMPLE 1: For illustrative purposes, let’s modify the program that squares all integers from 1 to 100 (see page 97) by performing the squaring calculation with a function. /* 100 squares 2 */ /* #include */ int isquare(x) /* square of int */ int x; { return (x*x); } main(){ int i; for (i=1; i