Organizing Lab Exercises

  • Let’s join all our lab exercises into one repository. Register in the link https://classroom.github.com/a/PsMRcCci and get a generated repository that you will use to keep all your lab exercises at one place.
  • Clone the generated repository.
  • In the repository, make new directories (folders) for lab01 (of last week) and lab02 (today’s).
mkdir lab01 lab02
  • Add your files of last week lab (calculation.cpp & pythagoras.cpp) to lab01
mv /path/to/calculation.cpp /path/to/pythagoras.cpp lab01
  • Add lab01 and lab02 to the repository database, commit changes, and push
git add lab01 lab02
git commit -a -m "add first and second labs"
git push origin master

Application 1: Range Summation

As mathematical historians have told the story, the German mathematician Carl Friedrich Gauss (1777-1855) began to show his mathematical talent at a very early age. When he was in elementary school, Gauss was asked by his teacher to compute the sum of the numbers between 1 and 100. Gauss is said to have given the answer instantly: 5050. Write a program that computes the answer to the question Gauss’s teacher posed.

Starting from the following code (name the file as range_sum.cpp):

int range_sum( int start, int end )
{

}

int main(int argc, char **argv)
{

}

Finally compile your file:

g++ range_sum.cpp -o summer.out

Test your program:

./summer.out
1 100
5050

Improvement 1: Using Command Line Arguments

  • Receive the start and end of the range from the user using the command line arguments instead of std::cin.

Improvement 2: Header Files

  • Add a header file in the same folder and name it mylib.hpp.
  • Move your range_sum function to mylib.hpp header file.
  • In the range_sum.cpp file, add #include "mylib.hpp" at the beginning.
  • Now compile and test again.

Application 2: Find the roots

Write a program to solve quadratic equations. A quadratic equation is of the form \(ax^2 + bx + c = 0\)

If you don’t know the quadratic formula for solving such an expression, do some research. Remember, researching how to solve a problem is often necessary before a programmer can teach the computer how to solve it. Use doubles for the user inputs for a, b, and c. Since there are two solutions to a quadratic equation, output both x1 and x2.

Starting from the following code (name the file as root.cpp):

void root( double a, double b, double c, double &x1, double &x2)
{
    

}

int main(int argc, char **argv)
{
    
    
}

Improvement 1: Using Command Line Arguments

  • Receive the equation parameters a, b, and c from the user using the command line arguments instead of the std::cin.

Improvement 2: Header Files

  • Move your root function to mylib.hpp header file that you made previously.
  • In the root.cpp file, add #include "mylib.hpp" at the beginning.
  • Now compile and test again.