Hi everyone, I am very new to C++ I have never done any type of programing also. I am really having a hard time learning how to write programs. I am using a book called C++ programming by larry ullman and Andreas Signer. The problem is I am following the instructions and examples in the book pretty good but my professor assigns problems that are nothing like the examples in the book. I am having trouble with loops and While loops. The questions he is asking

1. write a program that uses loop structure to run a adding total untill it hits a negative number and then stops showing the total of positive numbers

2. write a program that prints out in reverse such as I enter five four three and it prints out three four five

3. Using a loop write a program that prints integers 1-10 and there sqrt's and cubes.

I am not looking for anyone to write the programs for me I just want to see if someone can give me any resources that would be helpful in trying to figure out how to do these problems. Thanks in advance for you help, John

Speaking in terms of Logic, there are 2 basic loop types; the determinate loop and the indeterminate loop.

A determinate loop is a loop that must run X number of times. For example, you have 10 items and you need to do something to each of those 10 items.

An indeterminate loop is a loop that has an abstract ending condition. It does not end until some event or a certain input occurs. For example, you need to enter student grades for 5 different classes, but each class is a different size. You need to somehow tell the program that you are done entering the grades for that class. Another example would be that you need to perform the action(s) as long as the status of a piece of data is valid/invalid. When the status changes, the loop ends.

In C++, there are 3 control structures that produce loops: While, Do-While, and For. While and Do-While are good for either loop type. Generally, For is better for determinate loops, but can do indeterminate as well.

Question 1 is an indeterminate loop controlled by a sentinel value. The sentinel is any negative value. I'm betting there is an example of this in your book pretty early in the chapter, it's a very common one.

Question 2 will have 2 loops, an input loop and an output loop. The second should be determinate. Whether the first is determinate or indeterminate will depend on the rest of the program details. Will there be a specific number of inputs (hint, hint)?

Question 3 would be a determinate loop that runs 10 times. Whether you use a while loop or a for loop is entirely up to you.

This article has been dead for over six months. Start a new discussion instead.