Google Answers Logo
View Question
 
Q: C++ Programming how to trace a loop for each value ( Answered,   0 Comments )
Question  
Subject: C++ Programming how to trace a loop for each value
Category: Reference, Education and News > Homework Help
Asked by: ariella-ga
List Price: $20.00
Posted: 14 Oct 2005 18:17 PDT
Expires: 13 Nov 2005 17:17 PST
Question ID: 580455
Create two tables similar to the one attached and then trace the
values of i and j from the programs below.
__________________________________

Value of index i	Value of index j	 Test condition for I	Test condition
for J	What is displayed?
	
0	0	(I <= 3) TRUE	(j <= 2) TRUE	0 0	
	1		        (j <= 2) TRUE	0 1	
	2		        (j <= 2) TRUE	0 2	
	3		        (J <= 2  FALSE		
1	0	(I <= 3)  TRUE	(j <= 2) TRUE	1 0	
	1		        (j <= 2) TRUE	1 1	
			        (j <= 2)TRUE	1 2	
2					
?.	?.	?.	?.	?.	
?	?	?	?.	?.	
				
____________________________________________
#include <iostream.h>
main ()
{
int i = 0, j = 0;
while (i <= 3)
{
for (j = 0; j <=2; j++)
cout << i << j << endl;
i++;
}
return 0;
}
__________________________________________
#include (iostream.h>
main()
{
int i = 0, j = 0;
for (i = 0; i < 3; i++)
{
for (j = 3; j > 0; j-)
{
if (i ==0)
cout << j << endl;
else 
cout << i << j << endl;
}
}
cout << "Endl of loops\n";
return 0;
}

Request for Question Clarification by leapinglizard-ga on 14 Oct 2005 20:18 PDT
Is this a school assignment?

leapinglizard

Clarification of Question by ariella-ga on 14 Oct 2005 23:33 PDT
This question was a question I got wrong in my homework that I now
need to study for an exam.
Answer  
Subject: Re: C++ Programming how to trace a loop for each value
Answered By: leapinglizard-ga on 15 Oct 2005 14:22 PDT
 
Dear ariella,


The point of these exercises is to keep track of the value of each loop's
iterating variable. In both cases, the iterating variable of the outside
loop is i, while the inside loop iterates over j. In order to adhere to 
the pattern established by the sample table, we show the value of i only 
when it changes. Furthermore, when the iterating variable changes but the
loop does not execute again because its test condition has become false,
we display this in the table as well.


In the first exercise, the inside loop stops executing when j becomes 3,
and the outside loop stops when i becomes 4.

value of i  value of j  test for i      test for j      displayed
----------  ----------  ----------      ----------      ---------
0           0           (i <= 3) TRUE   (j <= 2) TRUE   0 0
            1                           (j <= 2) TRUE   0 1
            2                           (j <= 2) TRUE   0 2
            3                           (j <= 2) FALSE
1           0           (i <= 3) TRUE   (j <= 2) TRUE   1 0
            1                           (j <= 2) TRUE   1 1
            2                           (j <= 2) TRUE   1 2
            3                           (j <= 2) FALSE
2           0           (i <= 3) TRUE   (j <= 2) TRUE   2 0
            1                           (j <= 2) TRUE   2 1
            2                           (j <= 2) TRUE   2 2
            3                           (j <= 2) FALSE
3           0           (i <= 3) TRUE   (j <= 2) TRUE   3 0
            1                           (j <= 2) TRUE   3 1
            2                           (j <= 2) TRUE   3 2
            3                           (j <= 2) FALSE
4           3           (i <= 3) FALSE


In the second exercise, the inside loop stops executing when j becomes 0,
and the outside loop stops when i becomes 3.

value of i  value of j  test for i      test for j      displayed
----------  ----------  ----------      ----------      ---------
0           3           (i < 3) TRUE    (j > 0) TRUE    3
            2                           (j > 0) TRUE    2
            1                           (j > 0) TRUE    1
            0                           (j > 0) FALSE
1           3           (i < 3) TRUE    (j > 0) TRUE    1 3
            2                           (j > 0) TRUE    1 2
            1                           (j > 0) TRUE    1 1
            0                           (j > 0) FALSE
2           3           (i < 3) TRUE    (j > 0) TRUE    2 3
            2                           (j > 0) TRUE    2 2
            1                           (j > 0) TRUE    2 1
            0                           (j > 0) FALSE
3           0           (i < 3) FALSE


It has been a pleasure to address this question on your behalf.


Regards,

leapinglizard
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy