• Want to help us with this year's BoS Trials?
    Let us know before 30 June. See this thread for details
  • Looking for HSC notes and resources?
    Check out our Notes & Resources page

C++ Problems, please help! (1 Viewer)

~*Rach*~

New Member
Joined
Apr 7, 2005
Messages
4
Gender
Female
HSC
2005
Hey all, Im doing my SDD homework and im stuck with this C++ question:

Rewrite the following code segment without using break and continue.

for (int i=0; i < 30; i+=2)
{
if (i % 2)
continue;

if (i % 3)
cout << i << endl;

if (i % 7)
break;
}

The problem is I've tried so many times with different coding but can't get the exact output (which is 2).

This is one of the example I tried, but it came back with infinite numbers of 2 :(

for (int i=0; i < 30 || ((i % 7) == false);i+=2)
{
while (((i % 2) == false) && (i % 3))
{
cout << i << endl;

}

}


Would someone please help me out with this problem, thanks in advance :)
 

redruM

Breathe and Stop
Joined
May 11, 2004
Messages
3,954
Gender
Male
HSC
2003
what is the code supposed to do, i am having trouble understanding it (from the exact output you have given)

never done if statments like the one in the "segment"

but i think the problem might be if you swap the while with an if in your example. once the number enters that loop, it is going to be continuously be looping, no terminating value, can you see that?
 

~*Rach*~

New Member
Joined
Apr 7, 2005
Messages
4
Gender
Female
HSC
2005
Our class is practising how to write C++ codes without using 'break' and 'continue'. Our teacher wants us to re-write the code above without using 'break' and 'continue' yet the codes will still produce the same output.

I put the codes into Visual C++ and the output is 2. However, I can't rewrite the codes without using 'break' and 'continue' to produce the same result. I've tried to swap the while with an if like you said, but it came back with a totally different output. :(
 

redruM

Breathe and Stop
Joined
May 11, 2004
Messages
3,954
Gender
Male
HSC
2003
could you explain to me what the given code is supposed to do? ie- in words? its a bit late and i cant really think straight. :p
 

|Axis_

Member
Joined
Jul 20, 2004
Messages
64
Location
Sydney
Gender
Male
HSC
2003
Usually 'break' and 'continue' is used in place of properly formed if-then-else statements. Your teacher probably wants you guys to learn how to properly structure your programs using an if-then-else structure rather than 'jumping around'.

You don't need to bother about what the progam does, just how it flows. Try this:

for (int i = 0; i < 30; i += 2) {
if (!(i % 2)) {
if (i % 3) cout << i << endl;
if (i % 7) i = 30;
}
}
 

Slidey

But pieces of what?
Joined
Jun 12, 2004
Messages
6,600
Gender
Male
HSC
2005
Just some comments to help you, Rach:

a+=b means a = a+b, so here,
i+=2 means i = i+2 (it's just adding 2 to the value of the variable i)

a%b means... find the remainder, essentially. It's actually a function called modulus. Learn by examples:
4%2=0 because when 4 / 2 there is no remainder
7%3=1 because when 7 / 3 there is a remainder of 1.

In the first if statement's condition, you see !(i%2). This means NOT i%2 - I'm not sure if this returns true on a remainder or no remainder, though.
 

HellVeN

Banned
Joined
Jun 26, 2004
Messages
532
Gender
Male
HSC
2005
What compiler or IDE do you guys use? Visual C++?
Do you guys just make console applications in C++?
 

~*Rach*~

New Member
Joined
Apr 7, 2005
Messages
4
Gender
Female
HSC
2005
Hey all, its me again....

Im just wondering how to represent 5^5 (5*5*5*5*5) in C++ without using the power function?

The teacher asked us to write a program when the user enter a number, it will display the power of that number, so if the user enters 4, the output would be 4*4*4*4, 3, 3*3*3 and so on. I declared a variable calls userInput. Thanks :)
 
Last edited:

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
something like:

int n = userInput;
for(int i = 1; i < n; i++)
{
n= n*n;
}

(n will end up being userInputuserInput)

Note: I havent tested this code, just wrote it off the top of my head, so it may not work how its supposed to
 

|Axis_

Member
Joined
Jul 20, 2004
Messages
64
Location
Sydney
Gender
Male
HSC
2003
hehe! acmilan's program will never end if userInput >= 2.

hahah :p sorry.. :p

acmilan's logic will work if u replace the "n" in the 'for' condition with "userInput".

EDIT: Um, no that won't work either
 
Last edited:

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
|Axis_ said:
hehe! acmilan's program will never end if userInput >= 2.

hahah :p sorry.. :p

acmilan's logic will work if u replace the "n" in the 'for' condition with "userInput".
Yeah that'd be right

*bashes head against wall*

meh too much java programming slowly eats away at my like for programming
 

|Axis_

Member
Joined
Jul 20, 2004
Messages
64
Location
Sydney
Gender
Male
HSC
2003
really? java does that to u? :p hehe.. i dont really like java compared to c.. meh

anyway, about the problem - its just iteration! it cant b that hard Rach!! :p

I guess this would work..

int userInput, n = 1, i;
cin >> userInput;
for (i = 0; i < userInput; ++i) {
n = n * userInput;
}
cout << n << endl;
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top