Homework Tips

We're going to spend the first part of today going over how to add files to your project in Visual Studio. Specifically, we'll take the two files that Mike has supplied for your homework 4, and create a program that makes a HomeworkList object and does some basic stuff with it. The finished files can be found below.

HomeworkList.cpp
HomeworkList.h
main.cpp

As per usual, the best way to work on larger projects is to do things a bit at a time. For this particular code, you're asked to write some kind of loop. I'd recommend first writing the part inside the loop, testing it, then building the loop around it.

Back to if statments: Practice problems

Problem 1

Use cin to input 3 doubles from the user. Print to the screen the largest of them.

Problem 2

Use cin to input 2 doubles from the user. Print out how many of them are positive.

After doing this, change your code so it takes 3 doubles, then do 4.

prob2badversion.cpp
prob2betterversion.cpp

While loops

Syntax for a while loop:

while ( someBooleanExpressionHere) {
    // Put code that you want to execute in here
}

What this does:

  1. Check the Boolean Expression. If it's true, go to step (2). If false, quit looping and go on to the next part of the program.
  2. Execute the code, then go to step (1).

Do-while loops

Syntax for a do-while loop:

do {
    // Put code that you want to execute in here.
} while ( someBooleanExpressionHere)

What this does:

  1. Execute the code, then go to step (2).
  2. Check the Boolean Expression. If it's true, go to step (1). If false, quit looping and go on to the next part of the program.

In other words, a do-while loop is almost the same as a while loop. Instead of checking the condition first, it executes the code inside the loop first. So the only real difference between the two is that the do-while loop is guaranteed to run the code at least once, whereas a while loop may run the code zero times.

Examples:

// Example 1
#include<iostream>
using namespace std;

int main() {

    int a = 5;
    int b = 0;
    
    while (b < a) {
        b = b+1;
    }

    cout << b << endl;
    return 0;
}
// What does this code do?
// What if we changed it to a do-while loop?



// Example 2
#include<iostream>
using namespace std;

int main() {

    int a = 5;
    int b = 0;
    
    while (a < b) {
        b = b+1;
    }

    cout << b << endl;
    return 0;
}
// What if we changed it to a do-while loop?



// Example 3

#include<iostream>
using namespace std;

int main() {

    int a = 5;
    int b = 0;
    
    while (b < a) {
        a = a+1;
    }

    cout << b << endl;
    return 0;
}
// What if we changed it to a do-while loop?

Some Practice Problems:

Multiplication the old-fashioned way

Input two integers from the user. Call them a and b. For simplicity, assume that both are bigger than zero. Calculate a*b by adding a+a+a+...+a.

An addition problem

Input an integer from the user. Call it n. Find the sum 1+2+3+...+n. (There is a nice math formula for this, but we're not going to use it. Instead we'll actually add each number).

A longer problem: Find initials

Input a string from the user (using getline), and print to the screen the first letter of each word. For example, if I input my name:

Geoffrey Sankar Iyer

The program should output my initials

GSI
initials.cpp