CSCI 159 Intro to Variables and I/O Lab

CSCI 159: Fall 2024 Intro Lab

(Due Sept 17 (NO5) or 18(NO6) at the beginning of lab)

    Part I

  1. In your home directory, make a new directory called csci159dev, move into it, create lab1 directory, move into it, and start editing a file called lab1_io.cpp:

    In future labs, you will use a makefile that invokes a git "clone" command, which creates a specially tagged directory for you. That one will have to be called csci159. So make sure that you don't have a directory called csci159 in your home directory when you come to lab next week. We'll call this one "csci159dev" (for "development") so you know it's not your git stuff.


  2. 
    		mkdir csci159dev
    		cd csci159dev
    		ls 
    		mkdir lab1
    		cd lab1
    		vi sizes.cpp
    		

  3. Put in a comment that gives the name of the file, your name, the date, and the purpose of the program. The purpose is to use iostream functionality cout, endl, <<, >>, and \n, and to find out the size of int, float, double and char data types.

  4. #include the iostream library:
    
    		#include 
    		
    and set the namespace to std. (Use the semicolon after that last part.)

  5. Why not test it early? Save the file with an empty "main" routine, and try compiling it with the command
     g++ sizes.cpp -o sizesx 
    Keep editing and compiling until it compiles without errors.

  6. Write the program to print out the sizes of each of the following types:
    
    		int, float, double, char, short, long, long long, bool
    		
    You will need an expression that gives you the answer, which you can write to the screen. It is provided for you here, but you still have to embed this expression in a cout statement -- treat it like an integer (because that is it's return type).
    
    		sizeof(int) 
    		
    Add some code that tells how many bits are in a byte. Use CHAR_BIT, as if it is an integer. Do you need a header for it's use? Look here to determine what header to use:
    https://en.cppreference.com/w/c/types/limits

  7. After viewing that site, and updating your program, you may want to explore cppreference.com by clicking the "cppreference.com" at the top of the page. This site is a reference site for the C++ language.

    Part II


  8. You may have had to compile several times. Let us write a simple makefile to "make" the executable. The file is called makefile. The contents are:
    
    		sizesx:
    			g++ sizes.cpp -o sizesx
    		
    The whitespace before the "g++" MUST be a tab! Now try the command
    make

  9. For more information from your compiler, change the makefile so that the following flags make the compiler do more for you:
    
    		sizesx: sizes.cpp
    			g++ -Wall -Wextra sizes.cpp -o sizesx
    		

    Part III

  10. Create a new program in a file called average3.cpp. In it, get three integers from the user, and calculate and output their sum and their average. Use "inline" calculation to cout the average: cout << ... << sum/3 << ... ; See what happens -- does it do give the average as an integer?

  11. Change your makefile so that it can also make average3.cpp, by adding the line
    
    		average3: average3.cpp
    			g++ -Wall -Wextra average3.cpp -o average3x
    		
    Now the makefile has two options, and you will have to tell it what you want on the command line:
    		make average3a
    		make sizesx
    		

Handing it in

Make printouts of the two C++ files and the makefile, and hand them in before the start of the lab the following week. The instructor may ask you to demonstrate your code in the lab of the following week.


Useful Links: Gara Pruesse's Homepage
Computing Science Homepage
Vancouver Island University Homepage
X2Go