Biyernes, Marso 15, 2013

3.6 A C Program




3.6
            A C Program
             
            Recall that in Chapter 1, we formulated our algorithm to compute for the final grade.
            Now that we have seen the different parts of a C program and we have discussed some simple statements in C, let us now put it together.
           
            /************************************************
           
            File Name
           
            : grade.c
           
           
            Author
           
            : Nathalie Rose Lim
           
            Last Modified : April 25, 2002
           
           
            This program computes the final grade.
            ***********************************************/
           
            #include <stdio.h>
           
            #define GREETING “Hello, Let’s compute your grade.”
           
            main()
            {
           
            float fQ1, fQ2, fMp1, fMp2, fFE, fTE, fFG;
           
            printf(“%s”,
            GREETING);
           
           
            /* get quiz grades from user */
            printf(“Enter quiz 1: “);
            scanf("%f", &fQ1);
            printf(“Enter quiz 2: “);
            scanf("%f",
            &fQ2);
           
           
           
              
              
              
              
              
              
              
              
              
              33
             Chapter 3
            /* get project grades from user */
           
            printf(“Enter project 1: “);
            scanf("%f",
            &fMp1);
           
            printf(“Enter project 2: “);
            scanf("%f",
            &fMp2);
           
           
            /* get final exam grade from user */
           
            printf(“Enter final exam: “);
            scanf("%f", &fFE);
           
            /* get teacher’s eval from user */
            printf(“Enter teacher’s evaluation: “);
            scanf("%f",
            &fTE);
           
           
           
            /* compute final grade */
            fFG = 0.50 * ((fQ1 + fQ2)/2) +
            0.15 * ((fMp1 + fMp2)/2) +
            0.3 * fFE + 0.05 * fTE;
           
           
            printf("Final Grade = %.2f", fFG);
            }
             
             
            Frequently Asked Questions
             
             1.
             What is a statement?
            All C programs are made up of a sequence of instructions. In C, instructions are also called statements. In the English language, a statement is terminated by a period, a question mark, or an exclamation point. In C, a statement is terminated by a semicolon.
           
             2.
             What is the use of { } symbols?
            The symbol { and } are called open and close brace, respectively. They signify the start and the end of a block. Note that the braces always come in pairs.
           
             3.
             What is the use of /* */ symbols?
            These symbols group comments inside the program. Comments are not instructions, rather they simply provide additional information (a textual description) such as what the program is doing, what are the inputs and outputs, etc. Comments are optional and may be placed anywhere within the program source code.
           
             4.
             What is a variable?
            A variable is an entity that is used to store data. It is the name that the programmer uses to indicate what space in the memory he wants to access.
            For this space to be accessed, it should be reserved first. Reserving a memory space and giving it a name is called variable declaration. The type 34
             The Basic Program Structure
            of data (data type) to be stored in the memory will indicate how much space is going to be reserved. Each variable has a name, an associated physical memory space (in RAM), a data type, a value, a scope, and a lifetime.
              
             5.
             What is a data type?
            A data type specifies :
            • the kind of values that can be assumed by a variable of that type
            • the range of values that can be assumed by a variable of that type
            • the amount of memory (in bytes) needed by a variable to store a value of that type
              
             6.
             Can I use any name for the variables?
            Yes, as long as you follow the naming conventions, and do not use the reserved words in C. It is recommended, however, as a good programming practice, for you to use a name that is descriptive or suggestive. Refer to Appendix C to make the format of your variables more readable.
              
             7.
             What is the value of the variable initially?
            By default, the value of a variable is garbage, i.e., there is something stored in that memory space but that something is invalid for the intended use.
            Using variables with garbage values will cause logical errors.
              
             8.
             What will happen if I assigned a value whose data type is different from the data type of the receiving variable?
            The value will be converted. In general, one that requires smaller memory will fit into the variable with a larger space. The opposite will result to loss of information (which could cause a logical error). The basic rules of data type conversion are as follows:
            • int to float/double : value will be converted to floating-point value
            • float/double to int : the fractional part is dropped
            • char to int/float/double : the char value (based from ASCII character set) will be converted automatically to int/float/double with no problem
            • int to char : if the int value is within the possible range of char values (0 – 255) then that value is assigned and converted to the ASCII character equivalent; otherwise, the value assigned could be unpredictable
            • float/double to char : the fractional part will be discarded and with the whole number part, the same rule as int to char will be applied
              
             9.
             Does the C language include commands for input/output?
            No. Actually, the C programming language by itself does not include commands for input/output of data. However, there is a predefined standard input/output library that can be linked with the programmer’s object file.
            That is the reason why we need to include stdio.h.
              
              
              
              
              
              
              
              
              
              
              35
             Chapter 3
             
            Common Programming Errors
           
            1. Assigning a variable to a literal is an error. For this reason, it is an error to write 123 = a;
           
            2. Assigning the value of a variable of the same name is syntactically correct, but is useless and actually does not make sense. For example:
           
            int a;
            a = 10;
            a = a;
           
            3. The logical operator and should be &&, not &. The logical operator or should be | , not |.
            Using & and | will not result to a syntax error, but will cause a logical error.
           
           
            4. In defining a constant, you do not need to use the assignment operator, =, nor end the statement with a semicolon (;). Thus, the following is incorrect
           
            #define PI = 3.1416;
           
            5. A character literal is enclosed in a pair of single quotes. To represent null character, you have to use ‘’. It is wrong to have a pair of single quotes containing nothing.
            Therefore, do not write
           
            char ch;
            ch = ‘’;
           
            6. It is incorrect to omit the format string in a printf  or scanf  statement. For this reason, it is wrong to write
           
            int nValue;
           
            scanf( nValue );
            printf( nValue );
           
            7. There should be an ampersand (&) sign before a variable when used as an input variable.
            This specifies the memory location where the input will be stored. Thus, it is wrong to write
           
            int nValue;
            scanf( “%d”, nValue );
           
            8. The number of input variables should correspond to the number of conversion specification. The following statements will produce unpredictable results.
           
            int nValue1, nValue2;
             36
             The Basic Program Structure
           
            scanf( “%d%d”, &nValue1 );
            scanf( “%d”, &nValue1, &nValue2 );
            printf( “%d%d”, nValue1 );
            printf( “%d”, nValue1, nValue2 );
             
            9. A variable declared to contain a double value should use %lf  as the conversion character in a scanf  statement. However, the conversion character should be %f  in a printf statement, whether a variable is declared as a float or double. Thus, incorrect results will occur if you write:
           
            double dTotal;
           
            scanf( “%f”, &dTotal );
            printf( “%lf”, dTotal );
           
            10. String literals are enclosed in a pair of double quotes (“). String literal cannot span more than one line. Thus, it is incorrect to write
           
            printf( “Hello
            World” );
           
             
            Self Evaluation Exercises
           
            1.
            Write a program that inputs a 3-digit number and then display each of the digits.
           
            2.
            Write a program that asks the user to enter the radius of a circle and then computes for its area. Recall that the formula to compute for the area is AREA = 3.1416 * R2
            where R is the radius. The output of the program must be similar to the one below.
           
           
           
            The area of the circle with radius 2 cm. is 12.56 sq. cm.
           
            3.
            Write a program that will ask the user that asks for a distance in kilometers and converts it to its metric equivalent.
           
            4.
            Write a program that inputs two real numbers then exchange their values.
           
            5.
            Workers at a particular company were given a 15.5% salary increase. Moreover, the increase was effective 2 months ago. Write a program that takes the employee’s old salary as input and output the amount of retroactive pay (the increase that wasn’t given for the past 2 months) and the employee’s new salary.
           
           
           
           
           
           
              
              
              
              
              
              
              
              
              
              37
             Chapter 3
            Chapter Exercises
           
            1.
            Create a program that will get as input from the user the base and the height of a triangle. Compute the area of the triangle.
           
            Example Run:
           
            Enter base: 15
           
            Enter height : 3
           
            The area of the triangle is 22.5
           
            2.
            Create a program that converts a Fahrenheit measure to a Celsius measure (C = 5/9
            x (F – 32)).
           
            3.
            Philippine currency is broken down into 1000, 500, 200, 100, 50, 20, and 10 peso bills, and 5 and 1 peso coins. Write a program that would input an integer amount in pesos and output the smallest number of bills and coins that would add up to that amount.
           
            4.
            Write a program that displays the reverse of an input 3-digit number while retaining it as a whole.
           
            5.
            Ten young men agreed to purchase a gift worth Php10,000 for their boss. In addition, they agreed to continue their plan even if at least one of them dropped out.
            Write a program that would input the number of men who dropped out (assume 0 to 9 only) and output how much more will each have to contribute toward the purchase of the gift. The display should the format in the sample run below: Example Run:
           
            Enter number of persons who dropped out: 3
           
            Original contribution per person: Php1000.00
           
            Additional contribution per person: Php428.57
           
            Computed contribution per person: Php1428.57
           
           
           
             38
             




Walang komento:

Mag-post ng isang Komento