Beginner Exercises

Pages: 1... 678910... 16
Jan 24, 2010 at 4:33pm
Ok, i am sorry about typing like that, just got used to it i guess.

"works inside functions only" - by that i ment that if i place srand outside main() for example (as a global variable), i get a compiler error.

Thanks for taking your time to explain mcleano. :)
Jan 25, 2010 at 1:11am
yea just put it in main, at the start like a local variable. Not in any functions and not in any loops. that way it will never seed twice.
------------------
edit: on another note I did the bunny exercise in java yesterday because I was bored and wanted to see how it would go... took me 24 hours to get around an iterator problem with my arraylist. but apart from that runs fine.... Do I win?
Last edited on Jan 25, 2010 at 1:14am
Jan 25, 2010 at 11:31pm
I'm doing the bunnie excersice, i'm up to the part where I have to make it run in real time. I was wondering how do I do that, and also the part where if someone types 'k', it kills half the bunnies. I wanted to use cin, but I don't think that will work since, it pauses the screen.
Jan 25, 2010 at 11:36pm
Yeah, you would have to use an OS-specific command to get keystrokes like that. Do a search, I think that question has been asked and answered before.
Jan 25, 2010 at 11:40pm
Ok, thanks I will try that. What about making it run in real time?
Jan 26, 2010 at 8:06am
I dunno about making it run in "real time" but what you could do is put in a 1 or 2 second pause between each turn. That way you wouldn't end up with 1000 bunnies in a blink of an eye.
Feb 4, 2010 at 7:07pm
you can make it run in real time, that is, real time is just "one sec away". time is an illusion, even in programing :)

I got a question on the third one, the pancake one..
i'm having troubles with the final modification.
This is what i have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<sstream>
#include<string>
#include<math.h>
using namespace std;

int input()
{string temp;
 int n;
 getline(cin,temp);
 stringstream(temp) >> n; 
 return n;
}

void max(int person[],int i)
{    int x=0;
     for ( i=1;i<=9;i++)
          {
            x =  (person[x] > person[i])  ? x : i; 
          }
   cout << "person " << x+1 << " ate " << person[x] << " pancakes, that is the highest amount\n";
}
void min(int person[],int i)
{    int x=0;
     for ( i=1;i<=9;i++)
          {
            x =  (person[x] < person[i])  ? x : i; 
          }
     cout << "person " << x+1 << " ate " << person[x] << " pancakes, that is the lowest amount\n";
}

int main()
{// opens main
int person[10];
int i; // counter for the people


cout << "please enter how many pancakes each person ate\n";

for (i=0;i<=9;i++)
{ // opens the for loop to insert pancake quantities
cout << "how many did person " << i+1 <<" eat?\n";
person[i]=input();
} // closes the for loop to insert pancake quantities

max(person, i);
min(person, i);

cin.get();
return 0;
}// closes main 


Feb 19, 2010 at 2:27pm
Hi, new user here (did a Google search for C++ exercises :-) I have a question about the graduation question:

I'm wondering what would be the best way in this case to generate the rabbits? I have had two thoughts so far. Bear in mind I am a beginner to programming concepts, in that I have never put my knowledge to practice but have done an amount of reading.

So I had two thoughts of how it could be done: First, would be to use the new and delete commands to make new Bunny objects and remove them as and when they are needed. This solution bothers me because I am not familiar with the repercussions or requirements to make this work, but it does seem to offer the most amount of flexibility.

My second thought, was that since the docs state that 1000 bunnies is the 'limit', is to make 1000 bunny objects and give them an alive/dead state, so born bunnies are really just existing 'dead' bunnies which have their alive state switched, and their stats modified to become 'new' bunnies.

I was hoping someone could offer some thoughts or suggest other ways of tackling this problem, thanks :-)
Feb 19, 2010 at 6:19pm
closed account (S6k9GNh0)
merkaba48, it's actually already decided what you should use. In the requirements, it mentions that you should use a linked list. C++ provides a standard stl class for lists call std::list but this is a double-linked list. That's fine but it's up to you if you want to create your own for fun or simply use the double-linked list.

http://richardbowles.tripod.com/cpp/linklist/linklist.htm - if you want to create your own linked list. This also explains what a linked list is. If you understand pointers, this concept is easy.

http://cplusplus.com/reference/stl/list/ - if you want to use std::list.
Last edited on Feb 19, 2010 at 6:19pm
Feb 22, 2010 at 12:43pm
@fermies - how to check if a key is pressed during loops.

if( kbhit() ) { //returns 0 if no key is hit 
 char ch;
 ch=getch() // gets the key you press and gives it to ch


and now you can use ether a switch or an if statement to do stuff with the key you pressed.

 switch(ch) {
  case 'k': 
   bunnygeddon() // your function to kill half the bunnys
   ... etc
}
Last edited on Feb 22, 2010 at 12:44pm
Feb 22, 2010 at 1:08pm
No, that's not how to check if a key is pressed. That's how to check if a key is press the wrong way.

Sometimes I feel like rewriting the whole conio library from scratch... oh wait, no need. Use pdcurses on windows or ncurses on UNIX/Linux.
Feb 22, 2010 at 11:36pm
curses ftw
Feb 22, 2010 at 11:59pm
Yeah, if you are doing a (serious) CUI program, use (pd/n)curses.
Feb 23, 2010 at 12:50am
Use pdcurses on windows or ncurses on UNIX/Linux.
I thought we had discussed this already.
Feb 23, 2010 at 8:22am
Discussed what?
Feb 23, 2010 at 12:19pm
That that's not true.
Feb 23, 2010 at 12:58pm
You don't make sense... You can't qualify advice as false, it has no true/false value. "Use pdcurses on windows or ncurses or UNIX/Linux" isn't a true/false statement. It's advice.

Explain yourself.
Feb 23, 2010 at 2:45pm
Alright, it's not false. It's just pointless. There's no reason to prefer either on either platform.
It's the equivalent of "use your watch on your right wrist when north of the equator, but on your left when south of it".
Feb 23, 2010 at 5:21pm
closed account (S6k9GNh0)
I fail to see how that explains yourself. I think I missed something.
Feb 23, 2010 at 6:18pm
There's no reason to prefer either on either platform.

My mistake, I thought PDCurses only worked on windows, and NCurses only worked on *NIX. I just did a Google search and apparently PDCurses works on both, and NCurses works on windows through Cygwin. So you can just choose one.
Pages: 1... 678910... 16