Regarding coding style: remember that it is EXTREMELY IMPORTANT that all of your code is correctly indented. Consistency is important for keeping everything organized and readable for not only yourself, but for the future when instructors or co-workers will look at your work. Consider the following program that asks for your current age and prints it on the screen:
#include <iostream> using namespace std; int main() { cout << "Enter your age: " << endl; int age = 0; cin >> age; cout << "Your age is " << age << "." << endl; }
To compile the program, be sure to save the file and name it something. For this example, name the file code.cpp. To compile, the program, and call the executable myprog, type in the terminal:
g++ -o myprog code.cpp
The "-o myprog" means that you are using the -o "output" flag and assigning the output a name.
To run the produced compiled program:
./myprog
Alternatively, if you compile the program without giving the output file name (such as -o myprog), the executable file will be called a.out or a.exe (based on operating system), which you can execute the same way:
./a.out
Write separate programs for each part of the assignment. Submit only the source code .cpp files, not the compiled executables. Each program should start with a comment that contains your name and a short program description, for example:
/* Author: your name Course: CSCI-136 Instructor: their name Assignment: title, e.g., Lab1A Here, briefly, at least in one or a few sentences describe what the program does. */
Write a program smaller.cpp
that asks the user to input two integer numbers and prints out the smaller of the two.
$ ./smaller Enter the first number: 15 Enter the second number: -24 The smaller of the two is -24
Write a program smaller3.cpp
that asks the user to input three integer numbers, and prints out the smallest of the three.
(Hint: There are many possible solutions here. One possible strategy: Given numbers x, y, and z, you can first compare x and y, take whichever is smaller and compare it with z.)
$ ./smaller3 Enter the first number: 23 Enter the second number: 76 Enter the third number: 37 The smaller of the three is 23
In C++, operator % computes the remainder of the division of x by y. For example, 37 % 10 returns 7, because this is the remainder of 37 when divided by 10.
Write a program leap.cpp
that asks the user to input an integer representing a year number (1999, 2016, etc.). If the
input year is a leap year according to the modern Gregorian calendar, it should print Leap year,
otherwise, print Common year.
In the modern Gregorian calendar, a year is a leap year if it is divisible by 4, but century years are not leap years unless they are divisible by 400. Here is the pseudocode:
if (year is not divisible by 4) then (it is a common year) else if (year is not divisible by 100) then (it is a leap year) else if (year is not divisible by 400) then (it is a common year) else (it is a leap year)
This means that 2012, 2016, 2020, and 2040 are all leap years. However, the century years 1800, 1900, 2100, 2200, 2300 and 2500 are NOT. Yet, 2000, 2400, 2800 are still leap years.
$ ./leap Enter year: 2016 Leap year
$ ./leap Enter year: 2017 Common year
This means that 2012, 2016, 2020, and 2040 are all leap years. However, the century years 1800, 1900, 2100, 2200, 2300 and 2500 are NOT. Yet, 2000, 2400, 2800 are still leap years.
Write a program month.cpp
that asks the user to input the year and the month (1-12)
and prints the number of days in that month (taking into account leap years).
You may not use switch case or arrays even if you know these language constructs.
$ ./month Enter year: 2017 Enter month: 5 31 days