Lab2 BasicIO CSEB114 Sem2 20112012

download Lab2 BasicIO CSEB114 Sem2 20112012

of 4

Transcript of Lab2 BasicIO CSEB114 Sem2 20112012

  • 8/11/2019 Lab2 BasicIO CSEB114 Sem2 20112012

    1/4

    C Programming LabUniversiti Tenaga Nasional

    2 Basic Input Output

    Question 1

    What is the output of the programs below, and do you understand the concept of printf?

    /* 1()*/#

    ()

    (" ");

    (" .");(" ");

    (" ");(" 2 3 ");

    0;

    /* 1() */#

    ()

    (" . .");

    Question 2

    What are the errors of these programs? Try to repair the error (yourself) and try to execute theprograms.

    /* 2() */

    # ()

    (" ");

    /* 2() */#

    ()

    (" ")

    /* 2() */

    # ()

    (" ");

    0;

    NOTES:As long as you do not put \n in your printf, you output will be printed out in one

    line.

  • 8/11/2019 Lab2 BasicIO CSEB114 Sem2 20112012

    2/4

    C Programming LabUniversiti Tenaga Nasional

    Question 3

    There are 4 basic data types: integer (int), float (float), double (double), character

    (char).

    Printing numerical output: integer (int)

    What is the output of the programs below? Can you differentiate the way of writing integerusing printf?

    /* 3() */# ()

    (" 2 3, 5");

    /* 3() */

    # ()

    (" 2 3, %", 2+3);

    /* 3() */#

    ()

    (" % %, %", 2, 3, 2+3);

    According to your understanding, explain what is the purpose of %d?

    NOTES:Conversion character Type of output%c

    Character%s String%d Integer%e Floating point number /exponent%f Floating point number%lf Double%g Floating point number%o Octal%s String (group character)%u Unsigned integer%x Hexadecimal%% to print symbol %

    Try changing the data type from integer to float. What happen to the output?

  • 8/11/2019 Lab2 BasicIO CSEB114 Sem2 20112012

    3/4

    C Programming LabUniversiti Tenaga Nasional

    Question 4

    scanfis the statement that gets the input from the user. The format of scanfis

    When you get a value from a user, you should store it in some memory address. Try runningthis program, what did you get?

    /* 4() */# ()

    ;(" ");

    ("%", &);(" %", );

    Now you have understood the concept of scanf(if you still dont, PLEASE ask!), try to solve

    this problem.

    Each egg cost RM0.25. Calculate the amount that should be paid by a user based on thenumber of eggs bought. Show your problem analysis, algorithm and your source code.

    Question 5

    Write a program that calculates the area of a square after obtaining from the user the lengthof its sides.

    Program Analysis

    Input: Square length

    Output: Area of a square

    Constraint: All values should be positive

    Formula: Area = length * length

    Question 6

    Write a program, Population.cpp, to compute the rate of growth, expressed as a

    percentage, of an insect population. Take as input the initial size of the population and its sizeone week later. Then predict the size of the population in yet another week, assuming thatgrowth continues at the same rate. At the beginning of the program, please write yourprogram analysis in comments.

    Rate of growth = (size (week 2) size (week 1))/size (week 1) * 100

    scanf (ctrl char 1, ctrl char 2, .., ctrl char n, argument 1, argument 2, .., argument n);

  • 8/11/2019 Lab2 BasicIO CSEB114 Sem2 20112012

    4/4

    C Programming LabUniversiti Tenaga Nasional

    Question 7

    Find errors on the source codes:

    /* 7 */

    # ()

    123 = 3+2;

    ;

    ( ")

    ("%", &);

    Question 8

    Write, compile and execute the following code:

    /* 8 ()*/

    # ()

    (" %", 15);(" %", 15);(" %", 15);(" %", 15);

    (" %", 15.0);(" %", 15.0);(" %", 'A');

    (" %", 15.0);(" %", "");

    Note the differences of the output formats %d, %i, %x, %o, %e, %E, %c, %f,and %s.

    /* 8 ()*/#

    ()

    (" "); /* */

    ("" " "); /* */("' ' "); /* */(" "); /* */(" "); /* */

    (" :) "); /* */