IPC144 Lab 2 - Numeric Data Types (Mr. Saul's Sections)

NOTE: Use &x (represents 'the address of x') to specify the address of a variable x.  Using just x represents 'the value of x'.  Use & with the variable name when calling the scanf function because scanf places input into a memory address.

Start by entering, saving and compiling the following program to be called lab2.c

   main( ) {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered %d. Now enter another number: ", num);
scanf("%d", &num);
printf("This time you entered %d\n", num);
}
  1. Run the lab2 program with each of the following inputs and record the numbers displayed.  Don't worry if less than one number is displayed.  You should be aware what will happen if you enter invalid input and will learn later how to handle non-numeric input better.

    NOTE: Write and save all of the following 3 questions in a file in your home directory called lab2.txt
                 and refer to the bottom section to submit your lab2 to your instructor.

    INPUTS OUTPUT DISPLAYED                                    
    YOUR EXPLANATION

    3 (press Enter key)

    4

       
    3  4  

     
    3 ab  

     
    ab 3 4  

     
    3 4 ab  

     
    3 4ab  

     

  2. Edit your program and remove the & (ampersand) from the second scanf( ) statement. Your program is now passing the value of the variable num to the scanf function instead of the address of num.  Compile and run the program and record what happens:  _____________________________
  3. Edit the program and make the changes marked in red so it looks like:
     
       main( ) {
    int num;
    	double num1;
    printf("Enter a number: ");
    scanf("%lf", &num1);
    printf("You entered %3.8lf in floating point format\n", num1);
    num = num1; printf("which is %d when converted to integer format\n", num);
    }

    Run the program using a different input each time as shown below. Record the output and try to explain why the output was produced.
    INPUT NUMBERS DISPLAYED YOUR EXPLANATION      
    1.0  

     
    1.99999999
     
     
    1.999999999

     
    -1.99999
     
     

Submission Requirements:

If you are in Murray Saul's class, issue the following command to send your lab #2 answers to Murray Saul:

mail -s "144lab2" -c $USER@learn.senecac.on.ca murray.saul@senecac.on.ca < lab2.txt

The option -s "144lab2" makes subject line appear as "144lab2" so instructor can filter these e-mails in a directory to collect all lab2 submissions.

The option -c $USER@learn.senecac.on.ca sends a copy of the e-mail message to YOUR learn account. The variable $USER is your Matrix id name assuming that you are issuing this command when logged into your Matrix account. Please keep this e-mail for the remainder of this term as proof that you sent your lab by the required deadline...