Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
okay i think i got it guys!! thanks :D
my prof asked us to give an output for a code, and its a while loop that has x = 2, x>0 and x+= 3, so obviously it goes on forever and ever and ever, think its appropriate if i just put the first few numbers like 2,5,8,11 and then put "etc?"
 
okay i think i got it guys!! thanks :D
my prof asked us to give an output for a code, and its a while loop that has x = 2, x>0 and x+= 3, so obviously it goes on forever and ever and ever, think its appropriate if i just put the first few numbers like 2,5,8,11 and then put "etc?"

a while loop or a for loop?
 
okay i think i got it guys!! thanks :D
my prof asked us to give an output for a code, and its a while loop that has x = 2, x>0 and x+= 3, so obviously it goes on forever and ever and ever, think its appropriate if i just put the first few numbers like 2,5,8,11 and then put "etc?"

Depends on X's type and your platform's size for that type. Also depends if X is signed or unsigned.
 
a while loop or a for loop?


its a while loop, heres the code fragment:

Code:
	int x = 2;
	while ( x > 0 )
	{
		cout << “The value of x is: “ << x << endl;
		x += 3;
   	}

and heres the question: 11. What output would be produced by the following 3 problems?


so obviously the output will be:

"The value of x is: 2
The value of x is: 5
The value of x: 8
etc" all the way to forever pretty mch.

so i think its appropriate to simply state the first few outputs as ive done above and then write "etc" or "and so on" or whatever. yes?
 
its a while loop, heres the code fragment:

Code:
	int x = 2;
	while ( x > 0 )
	{
		cout << “The value of x is: “ << x << endl;
		x += 3;
   	}

and heres the question: 11. What output would be produced by the following 3 problems?


so obviously the output will be:

"The value of x is: 2
The value of x is: 5
The value of x: 8
etc" all the way to forever pretty mch.

so i think its appropriate to simply state the first few outputs as ive done above and then write "etc" or "and so on" or whatever. yes?

Jeez, that's kind of mean to ask for the output of an infinite loop. State the first few outputs and then explicitly say that it is an infinite loop and will continue forever.
 
Jeez, that's kind of mean to ask for the output of an infinite loop. State the first few outputs and then explicitly say that it is an infinite loop and will continue forever.

hahaha yeah i was like hmmmmmm how do i do this lol thanks!!
 
Jeez, that's kind of mean to ask for the output of an infinite loop.

It's not an infinite loop because x is signed. Here's an example of why using chars for obvious reasons :

Code:
$ cat test.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv)
{
	char x = 2;
	printf("x has a size of %lu bytes or %lu bits if you will\n\n", sizeof(char), sizeof(char) * 8);
	printf("x now has a value of %d, loop wants it greater/ equal to 0 and we increment it by 3\n", x);
	printf("This should loop forever right ?\n\n");
	while(x >= 0)
	{
		printf("%d ", x);
		fflush(stdout);
		x += 3;
	}
	printf("\n\nThe loop broke, because x is now %d\n", x);
	return EXIT_SUCCESS;
}
$ gcc -Wall -o test test.c
$ ./test
x has a size of 1 bytes or 8 bits if you will

x now has a value of 2, loop wants it greater/ equal to 0 and we increment it by 3
This should loop forever right ?

2 5 8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 56 59 62 65 68 71 74 77 80 83 86 89 92 95 98 101 104 107 110 113 116 119 122 125 

The loop broke, because x is now -128

It's a trick question.

EDIT : Even using unsigned ints, since the prof made the condition x > 0 and not x >= 0, there's also a chance that the loop would eventually break. It's even possible to calculate when.
 
Excellent point. It'll overflow at some point, obviously. Man I feel stupid now. :eek:

Edit: sorry for giving wrong advice oxshannon, I was thinking 'too fast.'

It's not an infinite loop because x is signed. Here's an example of why using chars for obvious reasons :

Code:
$ cat test.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv)
{
	char x = 2;
	printf("x has a size of %lu bytes or %lu bits if you will\n\n", sizeof(char), sizeof(char) * 8);
	printf("x now has a value of %d, loop wants it greater/ equal to 0 and we increment it by 3\n", x);
	printf("This should loop forever right ?\n\n");
	while(x >= 0)
	{
		printf("%d ", x);
		fflush(stdout);
		x += 3;
	}
	printf("\n\nThe loop broke, because x is now %d\n", x);
	return EXIT_SUCCESS;
}
$ gcc -Wall -o test test.c
$ ./test
x has a size of 1 bytes or 8 bits if you will

x now has a value of 2, loop wants it greater/ equal to 0 and we increment it by 3
This should loop forever right ?

2 5 8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 56 59 62 65 68 71 74 77 80 83 86 89 92 95 98 101 104 107 110 113 116 119 122 125 

The loop broke, because x is now -128

It's a trick question.
 
soooooooooo what would i put then?
hahaha cuz when i ran the code it was like 30000 last time i looked at it :confused:
 
Ya its screwing up Xcode lol. I can't even see the final output.

I too didn't realize it was a trick question until Knight pointed it out.
 
soooooooooo what would i put then?
hahaha cuz when i ran the code it was like 30000 last time i looked at it :confused:

You're in a programming class. The answer should be obvious : cheat to get the result. ;)

Code:
$ cat test.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv)
{
	int prevx;
	int loopiterations = 0;
	int MAX_LOOPS = 4;
	int runonce = 1;
	int x = 2;
	while(x >= 0)
	{
		if(loopiterations < MAX_LOOPS)
		{
			printf("The value of x is : %d\n", x);
			loopiterations++;
		}
		if(runonce && loopiterations >= MAX_LOOPS)
 		{
			printf("...\n");
			runonce = 0;
		}
		prevx = x;
		x += 3;
	}
	printf("The value of x is : %d\n", prevx);
	printf("The value of x is : %d\n", x);
	return EXIT_SUCCESS;
}
$ gcc -Wall -o test test.c
$ ./test
The value of x is : 2
The value of x is : 5
The value of x is : 8
The value of x is : 11
...
The value of x is : 2147483645
The value of x is : -2147483648

You can even know how long the cheat version takes to run :

Code:
$ time ./test
The value of x is : 2
The value of x is : 5
The value of x is : 8
The value of x is : 11
...
The value of x is : 2147483645
The value of x is : -2147483648

real	0m3.141s
user	0m3.131s
sys	0m0.005s

3 seconds to give out the answer if you cut off the printing at some point. ;)
 
Last edited:
I'll add three to x until it goes over 32767. At this point it'll overflow, x will be negative, and the loop will stop.

ints haven't been 16 bit since the days of the 286 though. ;)

----------

One thing to add : the program as your prof gave you will never print the negative value of X, I added that for style points to show why the loop broke.
 
my brain hurts guys hahahaha

Numbers are stored in binary form in computers, and each type (int, float, etc.) are given a certain amount of memory in which they are stored. ints are given 32 bits of memory. If they are signed, one bit is reserved for the sign (positive or negative), and this leaves 31 bits left in which to store the number. There are 2^(31) numbers that can be held in 31 bits, and so the highest number that can be held in a signed int is 2^(31)-1 = 2147483647 (subtract 1 because you start at 0). If you don't care about negative numbers, you can declare an unsigned int, and use that extra bit to store more numbers. In this case there are 2^(32) numbers that can be held in 32 bits, and the highest number that can be held in an unsigned int is 2^(32)-1 = 4294967295.

Although, I probably just confused you more.

Edit: I said 32767 earlier because when I learned programming ints were 16 bits......
 
Sorry to say the answer is NOT a trick question - it's just unresearched so it seems so.

The following code determines at runtime, the only way it can be done really, the maximum value of an (signed) 'int' as it is machine dependent.

So the asnwer will be within -3 of the value returned by 'numeric_limits<int>::max()'.

Code:
#include <iostream>
#include <limits>

int main()
{
    std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << std::endl;
    std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << std::endl;

    return 0;
}

Hope this helps!
 
Last edited:
Sorry to say the answer is NOT a trick question - it's just unresearched so it seems so.

I still qualify it as a trick question in an entry level class, where the students aren't yet accustomed into thinking in terms of computer and memory storage areas.

The following code determines at runtime, the only way it can be done really, the maximum value of an (signed) 'int' as it is machine dependent.

So the asnwer will be within -3 of the value returned by 'numeric_limits<int>::max()'.

Code:
#include <iostream>
#include <limits>

int main()
{
    std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << std::endl;
    std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << std::endl;

    return 0;
}

Hope this helps!

Why use C++ when he course is on C ?
 
its a while loop, heres the code fragment:

Code:
	int x = 2;
	while ( x > 0 )
	{
		cout << “The value of x is: “ << x << endl;
		x += 3;
   	}

and heres the question: 11. What output would be produced by the following 3 problems?


so obviously the output will be:

"The value of x is: 2
The value of x is: 5
The value of x: 8
etc" all the way to forever pretty mch.

so i think its appropriate to simply state the first few outputs as ive done above and then write "etc" or "and so on" or whatever. yes?

It's quite a horrible trick question. Eventually increasing x by 3 again and again will lead to an overflow, and a signed int overflow produces "undefined behaviour" which means _anything_ can happen.

For example, if you use gcc with highest optimisation, the compiler may figure out that you start with x > 0, the only time you ever change it it gets increased by 3, so clearly x > 0 will always be true and the test (x > 0) can be removed.
 
Most often, however, a raptor will jump out of your computer and maul you.

But with everything getting smaller these days, it's usually a nano-raptor, which any cat, or even a semi-attentive parrot[1], can easily dispatch. In the old days, a VAX-raptor was a thing to be feared.


[1] as long as it's not stunned or pining for the fjords.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.