C++ While Loops

Sentinel

Diamond Member
Jun 23, 2000
3,714
1
71
So I have to write a list of while loops for my obj. oriented class proj.

I cant figure out how to write these:

You dont need to give the answer I just need clarification, ie. example.

// Output: (n - 1) ... 7 4 1

// Output: 1 2 3 ... (n / 2)

// Output: -(n / 2) ... -2 -1 0

// Output: -(n / 2) ... -2 -1 0 1 2 ... (n / 2)

// Output: 1 2 4 ... 2^(n - 1)
// First n powers of 2

// Output: 1 2 6 24 120 ... (n - 1)!
// First n factorials

// Output: 1 -1 1 -1 ... 1 -1
// Length of output is n

// Output: 0 1 0 1 ... 0 1
// Length of output is n

// Output: 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10
// Goes up by 2 and then down by 1 alternatively
 

z0mb13

Lifer
May 19, 2002
18,106
1
76
hint: use a counter, and print this counter for each iteration of the while loop

 

Sentinel

Diamond Member
Jun 23, 2000
3,714
1
71
I know they are easy I am just having a hard time finding out how to write them

Im not a software engineer dont worry this is just required for my degree ;)
 

purbeast0

No Lifer
Sep 13, 2001
53,636
6,510
126
dude this is not a joke ...

if you are having trouble with problems like that, you might seriously want to consider another field. and again, i'm not trying to be an ass or anything. cause if you can't figure that out yourself, then you are in for a llloooonnnngggg ride.
 

Sentinel

Diamond Member
Jun 23, 2000
3,714
1
71
omg this is just a project due for a low level course I am taking required for my degree I am horrible at math and logic i just have to get through it
 

purbeast0

No Lifer
Sep 13, 2001
53,636
6,510
126
int counter = 1;

while ( statement here ) {

int answer = equation * counter (do your equation putting the counter in there);
cout << answer << endl;
counter ++;

}

return 0;
 

Sentinel

Diamond Member
Jun 23, 2000
3,714
1
71
well thats what i have for all the rest of the problems i just didnt get the (n/2) and how do you write that given the answer he wants, 1 2 3
 

EmperorIQ

Platinum Member
Sep 30, 2003
2,003
0
0
Hey Sentinel,

How long have you been looking at the problem? Its fairly easy (and I had a hard time the first time starting off programming )but give it some time. If you've taken a long time already i'll help you a bit.


for those who are giving Sentinel sh1t. psych majors at ucsd are required to take C++, some econ majors take C++, and they don't even need it. If its not their thing but a small requirement let him be.
 

Sentinel

Diamond Member
Jun 23, 2000
3,714
1
71
ive been looking at it for only about 1 hr, usually if i stare at it long enough i get it
 

purbeast0

No Lifer
Sep 13, 2001
53,636
6,510
126
Originally posted by: Sentinel
well thats what i have for all the rest of the problems i just didnt get the (n/2) and how do you write that given the answer he wants, 1 2 3

int answer = (counter * 2) / 2 .... duh :p

2/2 = 1

4/2 = 2

6/2 = 3

4/2 = 4

....
 

jman19

Lifer
Nov 3, 2000
11,225
664
126
Originally posted by: EmperorIQ
for those who are giving Sentinel sh1t. psych majors at ucsd are required to take C++, some econ majors take C++, and they don't even need it. If its not their thing but a small requirement let him be.

That's really no excuse. Sorry.
 

EmperorIQ

Platinum Member
Sep 30, 2003
2,003
0
0
Originally posted by: purbeast0
Originally posted by: Sentinel
well thats what i have for all the rest of the problems i just didnt get the (n/2) and how do you write that given the answer he wants, 1 2 3

int answer = (counter * 2) / 2 .... duh :p

2/2 = 1

4/2 = 2

6/2 = 3

4/2 = 4

....

winnar!
 

purbeast0

No Lifer
Sep 13, 2001
53,636
6,510
126
Originally posted by: EmperorIQ
Originally posted by: purbeast0
Originally posted by: Sentinel
well thats what i have for all the rest of the problems i just didnt get the (n/2) and how do you write that given the answer he wants, 1 2 3

int answer = (counter * 2) / 2 .... duh :p

2/2 = 1

4/2 = 2

6/2 = 3

4/2 = 4

....

winnar!

now get back to your 5th grade math class !!! :p :p j/k
 

EmperorIQ

Platinum Member
Sep 30, 2003
2,003
0
0
Originally posted by: jman19
Originally posted by: EmperorIQ
for those who are giving Sentinel sh1t. psych majors at ucsd are required to take C++, some econ majors take C++, and they don't even need it. If its not their thing but a small requirement let him be.

That's really no excuse. Sorry.

ah. . . . I mis interpreted purbeast0. Sorry, I thought you were telling Sentinel to get out of his/her major based on this problem. I just wanted to point out the fact that some people who are required to take a programming course will never have to use it again and logic might be hard for them to start with.
 

purbeast0

No Lifer
Sep 13, 2001
53,636
6,510
126
Originally posted by: EmperorIQ
Originally posted by: jman19
Originally posted by: EmperorIQ
for those who are giving Sentinel sh1t. psych majors at ucsd are required to take C++, some econ majors take C++, and they don't even need it. If its not their thing but a small requirement let him be.

That's really no excuse. Sorry.

ah. . . . I mis interpreted purbeast0. Sorry, I thought you were telling Sentinel to get out of his/her major based on this problem. I just wanted to point out the fact that some people who are required to take a programming course will never have to use it again and logic might be hard for them to start with.

yea and i also didnt see he stated he wasn't a computer science major until after i had posted lol, but i assumed he was.
 

Sentinel

Diamond Member
Jun 23, 2000
3,714
1
71
lol if it was id be sh*t up a creek

I got through my python class just fine, this is the last bit of programming I have to take.