beginner for loop question

Apr 10, 2013 at 8:59pm
Hi, I recently started for loop in C++ and I am stuck on a question. I'm hoping that someone will be able to help me with this.


Last edited on Apr 13, 2013 at 6:26pm
Apr 10, 2013 at 9:08pm
This is more easily done with a while loop than a for loop. If anything, the for loop is excessive.
Apr 10, 2013 at 9:18pm
Yes that also what I thought, but since i'm learning for loops right now, my teacher want us to do it using for loops, so it's quite annoying
Apr 10, 2013 at 9:48pm
That is unfortunate, learning how to use a pizza cutter by using it to remove a splinter.

What code have you written so far?
Apr 10, 2013 at 9:55pm
Why is it done easier with a while loop then a for loop?

I thought for loops are better for algorithms because they execute faster?


Also show us the code you have written so far in order for us to help you :P
Last edited on Apr 10, 2013 at 9:56pm
Apr 10, 2013 at 10:37pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
	std::cout << "Enter an integer number: ";
	int x = 0;
	std::cin >> x;

	const int base = 10;
	int y;

	for ( y = x; y > base;  y = y / base - y % base ) { /* empty */ }

	if ( y == 0 ) std::cout << x << " is divisible by 11." << std::endl;
	else std::cout << x << " is not divisible by 11." << std::endl;

	return 0;
}
Last edited on Apr 10, 2013 at 10:38pm
Apr 10, 2013 at 10:40pm
@vlad from moscow

Thats using modulus. Im pretty sure he wants to use that algorithm.
Apr 10, 2013 at 10:40pm
I suppose you could cram the whole reduction into an empty for loop:
1
2
3
    for(numToTest = userInput;
        numToTest has more than 2 digits;
        numToTest = numToTestShortened - whatWasChoppedOff) {}

This is most of the work. The rest is getting the user input and determining whether or not 11 evenly divides the value after the loop executes.

EDIT: It was practically a giveaway if I left valid C++ code.
Last edited on Apr 10, 2013 at 10:44pm
Apr 11, 2013 at 9:28am
By the way my code contains a bug. I did not take into account that x can be a negative number. So instead of the condition y > base in the loop there should be

y < -base || y > base


or maybe it will be simpler to write

y / base
Last edited on Apr 11, 2013 at 9:30am
Apr 11, 2013 at 1:29pm
@Anmol444 there is no difference in execution speed between for and while loops ;)
Apr 11, 2013 at 8:59pm
@LB

There isnt? Then why are their two? Cant the exact same thing be done in both?
Apr 11, 2013 at 9:11pm
closed account (3CXz8vqX)
Readability Anmol.

For loops are typically used for things which we know are of a fixed limit.

While loops are used for things that could continue forever.

...To be honest, they are exactly the same, but it's how the programmer 'reads' it that is important in this case. Both can be made to do exactly the same thing. An example is that a for loop is used to cycle through an array, typically a while loop is not. Mostly because an array should have a fixed number... ....until you get to dynamic memory allocation in which case....
Apr 11, 2013 at 9:28pm
Oh ok thank you
Apr 13, 2013 at 3:04am
hi everyone, sorry for the late reply. This is not due until next week, but i tried it one my own and i made something like this:

#include <iostream>
using namespace std;

int main()
{
int x;
cout << "Enter an integer number: ";
cin >> x ;

int y ;
for ( y = x ; y > 99; y = y / 10 - y % 10 )
;

cout << x << " reduced to " << y << "\n" ;

if ( y % 11 == 0 )
{
cout <<y << " is evenly divisible by 11" ;

}
else
{
cout << y << " is not evenly divisible by 11 " ;

}

return 0;
}
}

I not sure if it is right since I want to make it look like this when i input 12345678901234567900:

12345678901234567900
1234567890123456790
123456789012345679
12345678901234558
1234567890123447
123456789012337
12345678901226
1234567890116
123456789005
12345678995
1234567884
123456784
12345674
1234563
123453
12342
1232
121
11

Last edited on Apr 13, 2013 at 3:06am
Apr 13, 2013 at 3:19am
closed account (3CXz8vqX)
and does it?
Apr 13, 2013 at 1:05pm
no i dont think so because i think i just deleted a number once and i need it to delete the last digit until it reaches 11
Apr 13, 2013 at 1:32pm
I think that this value 12345678901234567900 exceeds the maximum exceptable value for type int .
You should use an integer value that is not greater than INT_MAX (#include <climits>) or std::numeric_limits<int>::max (#include <limits>).
Last edited on Apr 13, 2013 at 1:32pm
Apr 13, 2013 at 2:28pm
ok thanks for the suggestment, but i was wondering that someone was able to suggested a way to fix my code to delete every last digit until it divides into 11
Apr 14, 2013 at 2:08pm
@xsxs, have you already found a solution to your problem?
Topic archived. No new replies allowed.