Beginner Exercises

Pages: 1... 678910... 16
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. :)
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
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.
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.
Ok, thanks I will try that. What about making it run in real time?
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.
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 


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 :-)
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
@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
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.
curses ftw
Yeah, if you are doing a (serious) CUI program, use (pd/n)curses.
Use pdcurses on windows or ncurses on UNIX/Linux.
I thought we had discussed this already.
Discussed what?
That that's not true.
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.
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".
closed account (S6k9GNh0)
I fail to see how that explains yourself. I think I missed something.
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