Very simple C program ( running total)

The above code does no input.In summary, the conversation is about a C program that initializes two int variables, total and amount, and reads in three values into amount, adding each value to the total as it is entered. The code provided by the person asking the question was missing the input function and did not read in any values. The expert suggests using an input function like scanf to properly input the values at runtime.
  • #1
liquidicy
3
0
Very simple C program ( running total) [solved]

Homework Statement


Question is:

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 into amount , it is added to the value in total (that is, total is incremented by the value in amount ).


Homework Equations





The Attempt at a Solution



My answer is this:

total = 0;
amount = 0;
do{
++amount;
total += amount;
}while (amount < 3);

Their response is:
Remarks:
⇒ Did you read in all three values?
Remarks:
⇒ At Execution

What did I do wrong?
 
Last edited:
Physics news on Phys.org
  • #2
Apparently I was just supposed to copy paste it three times with scanf("%i",&amount) instead of amount++
 
  • #3


liquidicy said:

Homework Statement


Question is:

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 into amount , it is added to the value in total (that is, total is incremented by the value in amount ).


Homework Equations





The Attempt at a Solution



My answer is this:

total = 0;
amount = 0;
do{
++amount;
total += amount;
}while (amount < 3);

Their response is:
Remarks:
⇒ Did you read in all three values?
Remarks:
⇒ At Execution

What did I do wrong?
It's a good idea to put [ code] and [ /code] tags (without leading spaces) around your code. I've done it below, and indented your code to make it more readable.
Code:
total = 0;
amount = 0;
do
{
   ++amount;
   total  +=  amount;
} while (amount < 3);

In answer to the questions above, no, you didn't read in any of the three values. The values should have been entered at program run time.

Yes, you should use some input function, such as scanf, to do input.
 

Related to Very simple C program ( running total)

1. What is a "running total" in a C program?

A running total in a C program refers to a variable that keeps track of the cumulative sum of a set of numbers. It is updated each time a new number is added, and can be used to find the total sum at any point in the program.

2. How do I initialize a running total variable in a C program?

To initialize a running total variable, you can simply assign it an initial value of 0. For example, int total = 0; This will ensure that the running total starts at 0 before any numbers are added to it.

3. How do I update the running total variable in a C program?

To update the running total variable, you can use the addition assignment operator +=. For example, if you want to add the value of a variable x to the running total, you can use total += x; This will add the value of x to the current value of total.

4. How do I use a running total in a loop in a C program?

In order to use a running total in a loop, you will need to update the variable within the loop. This can be done by adding the new value to the running total variable each time the loop runs. At the end of the loop, the running total will contain the cumulative sum of all the numbers processed within the loop.

5. Can a running total be used for other operations besides addition in a C program?

Yes, a running total can be used for other operations besides addition, such as subtraction, multiplication, and division. The method for updating the running total variable will depend on the specific operation being performed. For example, to subtract a value from the running total, you can use the subtraction assignment operator -=. For more complex operations, you may need to use a combination of operators and variables to update the running total.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
913
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
Back
Top