Jump to content

computer science C++ questions

Featured Replies

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 ).

 

 

Instructor's notes: If you use a loop, it must be a for loop.

And if you use a loop control variable for counting, you must declare it.

  • Author

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.

 

 

Instructor's notes: This problem requires either a while or a do-while loop.

  • Author

Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a do...while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j .

  • Author

Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total . Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total . Use no variables other than n , k , and total .

C++? Icky! Here's the Ruby solutions:

 

#1) total = 0; 3.times { total += gets.to_i }

#2) total = 0; 3.times { val = gets.to_i; total += val if val >= 0 }

#3) n.times { print "*" }

#4) total = 0; n.times { |k| total += k ** 3 }

 

Hope that helps! :)

The rules of the homework forum say that before you receive help, you need to first show that you have attempted the problem.

 

Okay, here's my attempt at 4. :)

std::cout<<(n*n*n*n+n*n*n+n*n*n+n*n)/0x04;

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.