i have this code and for an class work task and you have to do a walkthrough table and work out the output. But i dont know how you do a walkthrough table. Can someone help?

int c = 0, sum = 0;
while (sum < 8)
{
c++;
sum += c;
}
cout << sum;

:)

Can you give a specific example of the task or better explain the problem narratively?

The word table when used in the context of C++ frequently refers to a 2 dimensional array of values and your code implements an algorhithm to accumulate value in variable called sum by adding the value of c to sum each time through the loop, so I suspect you might want to be writing code to walk through a table of values adding the value of each item in the table to generate a total value of all elements in the table. But then again I could be way of base, too.

Can you give a specific example of the task or better explain the problem narratively?

The word table when used in the context of C++ frequently refers to a 2 dimensional array of values and your code implements an algorhithm to accumulate value in variable called sum by adding the value of c to sum each time through the loop, so I suspect you might want to be writing code to walk through a table of values adding the value of each item in the table to generate a total value of all elements in the table. But then again I could be way of base, too.

This is how the question was given and next to it was a table with 3 columns and some amount of rows and i know your ment to go through each step that it would take to work out the final value.

Q1. Given the code below:-

int c = 0, sum = 0;
while (sum < 8)
{
c++;
sum += c;
}
cout << sum;

What is the final value of sum when the loop terminates? (1 mark)
Use the walkthrough table above.
a) 8
b) 9
c) 10
d) 11
This is the only example I could find on it and I don't really understand it.
http://i129.photobucket.com/albums/p213/momanrox/deskcheck.jpg?t=1273802928

Well without the table to look at it's hard to say what the values in it are. It might have been something like a table that represented the value of c and sum each time through the loop, but who knows.
# c sum
1 1 1
2 2 3

and so on.

What its asking you is to make a walk through table -- a step by step table
where each row corresponds to each iteration of the loop. Here, I'll get you started

Given this code :

int c = 0, sum = 0;
while (sum < 8) {
  c++;
  sum += c;
}
 cout << sum;

a walk through table might look like this to start :

iteration # , value of c , value of sum
---------------------------------------
   0             0               0
   1             1               1
   2             2               3
   3             3               6
   .             .               .
   .             .               .
   .             .               .

I think that is what its asking for.

yerp thats it! thanks so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.