Lab 1
Part 1 – Understand your compiler
1.1 – Compiling a program via Command Line
We will primarily use the Linux terminal in examples. Any IDE may be used, but programs must be able to compile in the B-125 computer lab.
Step 1: Open your Terminal
Step 2: Type “cd”
Step 3: Type “mkdir Lab1”
Step 4: Type “cd Lab1”
Step 5: Type “nano hello.cpp” and enter the following code
1 2 3 4 5 6 7 8 9 10 11 12 |
// This is a comment #include <iostream> using namespace std; int main() { cout << "Hello World\n"; return 0; } |
Step 6: Save the .cpp file by typing “Ctrl+O”, then [Enter]
Step 7: Exit nano by typing “Ctrl+X”
Step 8: Compile the C++ Program by typing “g++ -o hello hello.cpp” where hello is the executable or object file
Step 9: Run the executable by typing “./hello” in the terminal
1.2 – Flags for command line
Most common GCC and Clang compiler flags
std Flags | Specify the C++ version or ISO standard version. |
-std=c++11 | Specifies ISO C++11 |
-std=c++14 | Specifies ISO C++14 |
-std=c++1z | |
-std=c++20 | |
-std=gnu++ | |
Verbosity Flags | W stands for warning |
-Wall | Turns on different compiler warning flags such as -Waddress, -Wcomment, -Wformat, -Wbool-compare, -Wuninitialized, -Wunknown-pragmas, -Wunused-value, … |
-Werror | Turns any warning into a compilation error |
-Wextra | Enables flags that are not enabled with -Wall |
-pendantic or -Wpendantic | Issue all warning required by ISO C and ISO C++ standard, it issues warning whenever there are compiler extensions non compliant to ISO C or C++ standard. |
-Wconversion | |
-Wcast-align | |
-Wunnused | |
-Wshadow | |
-Wold-style-cast | |
-Wpointer-arith -Wcast-qual -Wmissing-prototypes -Wno-missing-braces |
1.3 – Writing your first Makefile
In this section we are going to continue working with the hello.cpp file that was created in Section 1.1.
Step 1: Type “nano Makefile” and type the contents from below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
CC=g++ CFLAGS=-g -Wall -Wpedantic -Werror all: main hello.o: hello.cpp ${CC} ${CFLAGS} -c hello.cpp main: hello.o ${CC} ${CFLAGS} -o main hello.o clean: ${RM} *.o main |
Step 2: Save the file and exit
Sections Explained:
Line 1: CC=g++ – Specifies the compiler. gcc for C programs and g++ for C++ programs
Line 3: CFLAGS – Specifies the compiler flags to be applied when compiling the programs.
The first flag that is used when compiling is the -g flag which tells the compiler to add debugging information to the executable file. The -Wall flag, as described above, turns on most of the warning flags for the compiler. The -Wpedantic flag will issue warnings when there are noncompliant compiler extensions used. The -Werror flag will turn any warning from the compiler into an error.
Line 5: all: main – Specifies the targets to be made
Line 7: hello.o: hello.cpp – Creates the object file hello.o from the source file hello.cpp
Line 8 uses the CC and CFLAGS variables that we defined earlier in the Makefile and invokes the compiler with the -c flag
Line 10: main: hello.o – Creates the executable file from the previously generated object file
Line 11 uses the CC and CFLAGS variables that was defined earlier in the Makefile and links the generated object file to the executable main
Line 13: clean: – Specifies what is executed when “make clean” is called
Line 14: Tells “make clean” to remove all object files (files ending in .o) and the target executable main
1.4 – Compiling a program with a Makefile
In order to compile with the Makefile that we just created type “make” into the terminal. When you examine your working directory you will notice two new files titled “hello.o” and “main”. “main” is an executable file that can be run by typing “./main” into the terminal.
The “clean” section we defined in the Makefile can be used to clean our working directory from all files ending in “.o” and the file main. This can be utilized by typing “make clean” into the terminal.
Part 2 – Writing Programs
When writing the following programs you will want to take in user input as arguments to your program. This can be done by changing main() to main(int argc, char **argv). argc is an integer value representing the number of command line arguments the user enters, including the name of the program. argv stands for ARGument Vector and is an array of character pointers which contains all the command line arguments including the name of the program at argv[0]. An example of using both the argc and argv values can be seen as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> using namespace std; int main(int argc, char** argv) { cout << "You have entered " << argc << " arguments:" << "\n"; for (int i = 0; i < argc; ++i) { cout << argv[i] << "\n"; } return 0; } |
Say we compiled the above program with the following command
g++ -o main example.cpp
and then ran the executable as follows
./main programming methodology is fun
you will achieve the following output.
You have entered 5 arguments:
./main
programming
methodology
is
fun
2.1 – Question 1
Given a positive integer denoting n, do the following:
- If 1 <= n <= 9, then print the lowercase English word corresponding to the number (e.g., one for 1, two for 2, etc.)
- If n > 9, print “Greater than 9”
INPUT: A single integer denoting n (Assume n is positive)
CONSTRAINTS: 1 <= n <= 10^9
OUTPUT: If 1<=n<=9, then print the lowercase English word corresponding to the number (e.g. one for 1, two for 2, ….); otherwise print ‘Greater than 9’ instead.
2.2 – Question 2
Write a program that calculates and displays a person’s body mass index (BMI). The BMI is often used to determine whether a person with a sedentary lifestyle is overweight or underweight for his or her height. A person’s BMI is calculated with the following formula:
BMI = weight x 703 / height^2
where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. A sedentary person’s weight is considered to be optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered to be underweight. If the BMI value is greater than 25, the person is considered to be overweight.
INPUT: Positive integer denoting weight and another positive integer denoting height
CONSTRAINTS: 1 <= weight <= 10^9 and 1 <= height <= 10^9
OUTPUT: If BMI < 18.5, output “underweight”. If 18.5 <= BMI <= 25, output “optimal weight”. If BMI > 25, output “overweight”.
2.3 – Question 3
Write a program that will take three arguments from the user, a starting temperature in Celsius, an ending temperature in Celsius and a step size. Display the value of Celsius and the converted value in Fahrenheit for each step on a newline. The formula that converts Celsius temperature to Fahrenheit is
F= 9/5 * C + 32
where F is the Fahrenheit temperature and C is the Celsius temperature.
If the step size does not match exactly with the final ending Celsius value, you may skip printing the value.
INPUT: ./q3 start step end where start is the beginning Celsius value, step is the step size, and end is the final Celsius value
CONSTRAINTS: Step size > end – start. Set a minimum limit for which you will not accept starting temperatures lower than and a maximum limit which start values cannot be higher than. These two limits should be defined as constants in your code.
OUTPUT: ./q3 0 2 4
0 32
2 35.6
4 39.2
What to submit?
Lab1_LastName_FirstName.zip
|
|—–>Lab1
| |
| |—–>hello.cpp
| |
| |—–>Makefile
| |
| |—–>Q1
| | |
| | |—–>q1.cpp
| | |
| | |—–>q1.h (if used)
| | |
| | |—–>Makefile
| |
| |—–>Q2
| | |
| | |—–>q2.cpp
| | |
| | |—–>q2.h (if used)
| | |
| | |—–>Makefile
| |
| |—–>Q3
| | |
| | |—–>q3.cpp
| | |
| | |—–>q3.h (if used)
| | |
| | |—–>Makefile