Lab 1
******
Use your CS account number to log in: https://www.cs.uic.edu/bin/view/ComputerSupport/Accounts(or google "UIC CS ACCOUNT")
Password is your UIN, the 9-digit number
******
Today's lab is about compiling and running simple C programs, using some file editor and/or the command line on the CS Department Mac Lab in SEL 2254.
To get credit you must work with a partner, taking turns every ~10 minutes on who types ("driver") and who gives input ("navigator").
You must finish question 0. Each question after that is worth one point. You should aim to also finish questions 1 and 2, for a total of 2 points. Finishing question 3 is extra credit.
Please raise your hand and show me you and your partners' work during the last 10 mins of lab so we can grade your work. Late submissions are not accepted.
Instructions on Writing and Executing Code
You can write your code on any text editor program on your machine. If you are using the lab machines, you can use the Xcode program to write your code.
Once you finish writing the code, remember to save the file with a .cpp extension (eg. lab1.cpp ). Also remember the directory where you saved the file
Search for and open the "Terminal" program by doing the following:
Press "command + space" (In other words, press and hold the "command" key just to the left of the space key, and while you are still holding it down also press the spacebar.) Another way you might see this written some time is: ^ space
Type "Terminal" (without the quotes)
Press Enter
Navigate to the directory where you saved the program. For eg. if you saved your program in the "Documents" folder, you would do the following
cd Documents (where <username> is your username)
Ensure the file you saved is in the directory by entering the following "listing" command:
ls
You should see the file you saved.
Now compile your program by typing
g++ <filename>
eg. if your filename is lab1.cpp, type
g++ lab1.cpp
If your program compiles correctly, you should not see any error messages
Run your program using
./a.out
You should see the output from the program's execution on the screen.
Sample Program
Use the sample code shown below as a starting point:
#include <stdio.h>
void displayIdentifyingInformation()
{
printf("My name is: Inigo Montoya \n");
printf("My lab is: \n");
printf("Today's date is: \n");
}
int main()
{
// Question 0
displayIdentifyingInformation(); // Call that function
// For questions 1,2,3 prompt for a number and read it in to a variable
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Loop to display multiple characters
// For questions 1,2,3 modify the code below
for( int i=0; i<number; i++) {
printf("*");
}
// The end
printf("\n Done with program. \n");
return 0;
}
Stages of What you Need to write
0. Change the function displayIdentifyingInformation() to display you and your partner's names and other information.
Notice the code that uses a loop to display multiple asterisks (***)
Change and add code to display a left-justified triangle that grows from one up to the entered size. For instance if 4 were the number, the output should be:
*
**
***
****
You will need a nested loop (loop inside a loop) to get this to work.
Now change and add code so that it displays a vertically symmetrical triangle with odd number input (which you can assume is provided). For instance if 5 were the number, the output should be
*
***
*****
Notice that this prints every-other line (1, 3, 5).
Extra Credit: Same as #2, but it displays a diamond shape. For instance if 5 were the number, the output should be
*
***
*****
***
*
Notice that this prints every-other line (1, 3, 5).