# nested while and for loop programs

Page 1 of 1

## 11 Replies - 2124 Views - Last Post: 27 February 2011 - 05:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=218980&amp;s=edb852985cf945fa1a4c5aee4134c4c4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

# nested while and for loop programs

Posted 26 February 2011 - 04:16 PM

hi everyone

I've been given this homework assignment but I can't really figure out how to go about it. I don't really understand the problem to be solved. Can someone explain to me how to go about this? Any help or advice will be much appreciated.

Quote

* Find all integer solutions to the equation x2 + y2 = 1600, where x >= 0 and y >= 0 and
* both x and y are within the range of the int data type. Do not list a solution that is
* the same as another solution, but with the numbers reversed. To use exhaustive search for
* this problem, you will need to try pairs of values for x and y. While this can be done with a
* single loop, it may be more natural to use a nested loop (ex, one loop included in the body of another loop).

like when I solve the equation, its not an integer value??

Is This A Good Question/Topic? 0

## Replies To: nested while and for loop programs

### #2 Patrunjel

• D.I.C Regular

Reputation: 17
• Posts: 298
• Joined: 28-October 10

## Re: nested while and for loop programs

Posted 26 February 2011 - 04:23 PM

Well, take it from one end to another.You need to find the solutions.The solutions are combinations of x and y for witch the statement is true.
So, x must be greater than 0, but still fit in an int.
So 0<=x<32,767 0<=y<32,767 (32,767 is the int limit)
So the most simple thing to do is to cycle through all possible combinations of x and y with the criteria above.

Here you have the basic principle, dude :
```for(int i=0;i<32767;i++)
for(int j=0;j<32767;j++)
if(2*i+2*j==1600)
cout<<x<<" "<<y;//or do whatever you want

```

But it isn't done yet.You have to figure out by yourself how not to make the same combinations of i and j , good luck

This post has been edited by Patrunjel: 26 February 2011 - 04:26 PM

### #3 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: nested while and for loop programs

Posted 26 February 2011 - 04:33 PM

Please explain exactly what you don't understand.

Quote

like when I solve the equation, its not an integer value??

This equation can be solved for values that are not integers, however your assignment it to only find the integer values that solve the equation.

Jim

### #4 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: nested while and for loop programs

Posted 26 February 2011 - 04:36 PM

What bits do you understand?
What bits don't you understand?

Here are the requirements numbered:
REQ 1 - Find all integer solutions to the equation x2 + y2 = 1600, where x >= 0 and y >= 0 and both x and y are within the range of the int data type.
REQ 2 - Do not list a solution that is the same as another solution, but with the numbers reversed.
REQ 3 - To use exhaustive search for this problem, you will need to try pairs of values for x and y.

This is a hint, not a requirement:
HINT - While this can be done with a single loop, it may be more natural to use a nested loop (ex, one loop included in the body of another loop).

So of the 3 requirements which one/s do you not understand?

BTW.
This is incomprehensible.
What, in the name of Bob, were you trying to say or ask when you wrote this?

iqbalmmz, on 27 February 2011 - 09:16 AM, said:

like when I solve the equation, its not an integer value??

### #5 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: nested while and for loop programs

Posted 26 February 2011 - 04:42 PM

@Patrunjel

Quote

So, x must be greater than 0, but still fit in an int.
So 0<=x<32,767 0<=y<32,767 (32,767 is the int limit)

Note: This limit for ints is not always true. While it is true for older, usually 16 bit compilers, it is not generally true with most other compilers. The 32,767 is the minimal maximum value.

To see your actual maximum values either use the functions/macros in limits.h or use the numeric_limits<int>::max() (C++ only) from the limits include file.

The maximum value for int is: 2147483647 on my system.

Also why would you be looking at any variable greater than 1600, the maximum either of the variables could be is 1600.

Jim

This post has been edited by jimblumberg: 26 February 2011 - 04:45 PM

### #6 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

## Re: nested while and for loop programs

Posted 26 February 2011 - 06:22 PM

jimblumberg, on 26 February 2011 - 03:33 PM, said:

Please explain exactly what you don't understand.

Quote

like when I solve the equation, its not an integer value??

This equation can be solved for values that are not integers, however your assignment it to only find the integer values that solve the equation.

Jim

sorry for the ambiguity, what I don't understand is how to start this program. but now after doing some googling and reading over similar programs i've come up with this:

```#include <stdio.h>
main ()
{
//int arrayCounter;
int x;
int y;

//need an array of 1600 elements
//int soln[1600];

//arrayCounter = 0;
for (x = 0; x < 40; x++) {

for (y = 0; y < 40; y++) {
//printf("x=%d, y=%d\n", x, y);

//soln[arrayCounter] = x*x + y*y;
//arrayCounter++;

if (((x*x) + (y*y)) == 1600) {
printf("y=%d\n", y);
//return;
} else {
printf("%d^2 x %d^2 = 1600\n", x, y);
}

}

}

}

/*int j;
for (j=0; j<1600; j++){

if (soln[j] == 1600) {
printf("index at %d index is 1600\n", j);
}
}*/

}

```

I tried to use an array but I forgot we're not allowed to use arrays yet. I'm stuck as to how to formulate my program structure and algorithm.

### #7 akhena

Reputation: 34
• Posts: 93
• Joined: 20-January 11

## Re: nested while and for loop programs

Posted 26 February 2011 - 06:54 PM

My way of looking at this problem would be like this :
x2 + y2 = 1600, with x and y > 0. I'm looking for an extreme case first. sqrt of 1600 is 40. So we know that x or y have to be lower than 40, otherwise the other argument would be 0, and that's not allowed.
So let's say we start with x = 39. We have to find if there's an int which would be a solution to the equation 39^2 + y^2 = 1600 <=> y^2 = 1600 - 39^2. I would here use brute force. So I would try with y = 1, and then with y = 2, etc... until y^2 >= 1600 - 39^2. When that's the case, either it was just equal and I have found one solution, or it was not equal and I can try with x = 38, and start again with y = 1, then y = 2, etc until y^2 >= 1600 - 38^2. And I'll do that until ... x is 20, because from that point, it's like swapping y and x.

So in a sort of pseudo-code, it would be like :
```for x = 39 down to 20
y = 1
while y^2 < 1600 - x^2
increment y
if x^2 + y^2 = 1600
print this pair as a solution to the equation

```

The while loop could be written in terms of a for loop. Something like for(y=1; y^2 < 1600-x^2; ++y).

### #8 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

## Re: nested while and for loop programs

Posted 26 February 2011 - 07:46 PM

akhena, on 26 February 2011 - 05:54 PM, said:

My way of looking at this problem would be like this :
x2 + y2 = 1600, with x and y > 0. I'm looking for an extreme case first. sqrt of 1600 is 40. So we know that x or y have to be lower than 40, otherwise the other argument would be 0, and that's not allowed.
So let's say we start with x = 39. We have to find if there's an int which would be a solution to the equation 39^2 + y^2 = 1600 <=> y^2 = 1600 - 39^2. I would here use brute force. So I would try with y = 1, and then with y = 2, etc... until y^2 >= 1600 - 39^2. When that's the case, either it was just equal and I have found one solution, or it was not equal and I can try with x = 38, and start again with y = 1, then y = 2, etc until y^2 >= 1600 - 38^2. And I'll do that until ... x is 20, because from that point, it's like swapping y and x.

So in a sort of pseudo-code, it would be like :
```for x = 39 down to 20
y = 1
while y^2 < 1600 - x^2
increment y
if x^2 + y^2 = 1600
print this pair as a solution to the equation

```

The while loop could be written in terms of a for loop. Something like for(y=1; y^2 < 1600-x^2; ++y).

I see what you mean, but this just gives me the same output as I was getting before.

Quote

output
===========

32^2 x 24^2 = 1600
24^2 x 32^2 = 1600

I need all the solns but they can't be permutations of each other. Is 32 and 24 the only integer solns?

### #9 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: nested while and for loop programs

Posted 26 February 2011 - 07:59 PM

Quote

I need all the solns but they can't be permutations of each other. Is 32 and 24 the only integer solns?

Jim

### #10 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

## Re: nested while and for loop programs

Posted 26 February 2011 - 08:17 PM

jimblumberg, on 26 February 2011 - 06:59 PM, said:

Quote

I need all the solns but they can't be permutations of each other. Is 32 and 24 the only integer solns?

Jim

that'll work but the code i have doesn't give that as a soln.

### #11 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: nested while and for loop programs

Posted 26 February 2011 - 10:49 PM

Post your current code so we can possibly see where the problem lies.

Jim

### #12 akhena

Reputation: 34
• Posts: 93
• Joined: 20-January 11

## Re: nested while and for loop programs

Posted 27 February 2011 - 05:36 AM

First of all, for some reasons, I thought the assignment said that x and y had to be strictly positive. I see now that they can be 0. So in my little pseudo-code, you would have to start from 40, not 39. Then you made me realize that indeed there's a flaw in my code, as you get the same 2 pairs. The solution would be to restrict y to be less than or equal to x. Then we realize that it's useless to let x go down to 20. It you think again about the equation, keeping in mind that y is always lower or equal than x, there will be a point where x is not big enough to reach 1600 anymore. When does this happen ? Well the moment when x = y. In that case we can rewrite x^2 + x^2 = 1600 <=> x^2 = 800. That's the lowest value x could get before we start to see pairs of values already found. And the sqrt of 800 = 28.28... So you can stop the checks for x when its down to 29.

```for x = 40 down to 29
y = 0
while ( (y^2 < 1600 - x^2) && y <= x)
increment y
if x^2 + y^2 = 1600
print this pair as a solution to the equation

```

The condition in the while loop could again be used in a for loop.

Now I realize that my way of looking at this is maybe not what your teacher is expecting from you. I used all I knew from the equation to restrict the possible solutions. But of course, it totally unflexible like this.

PS : I'm still learning to program. So take my advices with some care.

This post has been edited by akhena: 27 February 2011 - 05:50 AM