11 Replies - 1767 Views - Last Post: 27 February 2011 - 05:36 AM Rate Topic: -----

#1 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • 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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • 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
Was This Post Helpful? 1
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • 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?

View Postiqbalmmz, on 27 February 2011 - 09:16 AM, said:

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

Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • 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

Was This Post Helpful? 1
  • +
  • -

#6 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: nested while and for loop programs

Posted 26 February 2011 - 06:22 PM

View Postjimblumberg, 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];
	
	int alreadyUsedX;
	//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) {
				alreadyUsedX = x;
				printf("alreadyUsedX=%d\n", alreadyUsedX);
				printf("y=%d\n", y);
				if (y == alreadyUsedX) {
					//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.
Was This Post Helpful? 0
  • +
  • -

#7 akhena  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 69
  • 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).
Was This Post Helpful? 1
  • +
  • -

#8 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: nested while and for loop programs

Posted 26 February 2011 - 07:46 PM

View Postakhena, 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?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • 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?


What about 0 and 40?

Jim
Was This Post Helpful? 0
  • +
  • -

#10 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: nested while and for loop programs

Posted 26 February 2011 - 08:17 PM

View Postjimblumberg, 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?


What about 0 and 40?

Jim


that'll work but the code i have doesn't give that as a soln.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • 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
Was This Post Helpful? 0
  • +
  • -

#12 akhena  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 69
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1