Monday, June 18, 2012

Homework 6: RunningTotal

1. Write a statement that increments the value of the  int variable  total by the value of the  int variable  amount . That is, add the value of  amount to  total and assign the result to  total .

total = total + amount ;

2. Given that two  int variables,  total and  amount , have been declared, write a sequence of statements that: 
 initializes  total to 0 reads three values into  amount , one at a time. After each value is read in to  amount , it is added to the value in  total (that is,  total is incremented by the value in  amount ). 

total = 0 ; cin >> amount ; total += amount ; cin >> amount ; total += amount ; cin >> amount ; total += amount ;

3. Given that two int variables, total and amount, have been declared, write a loop that reads integers into amount and adds all the non-negative values into total. The loop terminates when a value less than 0 is read into amount. Don't forget to initialize total to 0.

total = 0 ; cin >> amount ; while ( amount >= 0 ) { total += amount ; cin >> amount ; }

No comments:

Post a Comment