Bunny exercise?

Hello,

I am trying to make bunny object and give them a random name, but i get alot of program stopped working errors? and i don't now were they're coming from.


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
#include <iostream>
#include <cstdlib> 
#include <limits>
#include <ctime>
using namespace std;


class cBunny{  
public:
string bName;
cBunny();
}; 

cBunny::cBunny() {
       string names[] = {"mutant", "spider", "nuclear","vampire","werewolf","racoon","midget","rabbies","furry"};
       int j=(rand()%10);
       int k=(rand()%10);
       bName=names[j]+"_"+names[k]+"_"+"bunny";
}

int main ()
{
  srand(time(NULL));
  
  cBunny oBunny[9];    //create some bunny's
  
  for(int c=0; c<9;c++){
  cout<< oBunny[c].bName<<endl;               //show me there names
  }


  int i;                  //just to make the screen stay 
  cin>> i;
  
return 0;
}
Last edited on
You're doing random % 10 when it should be % 9 because you only have 9 elements in your array
counting your names. There're just 9 not 10. to avoid using wrong numbers you might want to use sizeof:
1
2
3
4
       const int count=(sizeof(names) / sizeof(*names));

       int j=(rand()%count);
       int k=(rand()%count);
thnx, is there a why that you can easly see these kind of errors and not get a program not working error? for example that the log says that my names[] is to small?
Unfortunately not. Avoid using a numeral constant more than once.

Like on line 25/27: Instead of using the meaningless number 9 just define a constant.
Then if you want more or less bunnies you just have to change the constant and you don't need to worry whether the loop goes wrong
okay thnx for all the help!!
i have an other question.

i want to add an object to the existing object array, but can't seem to get it to work?

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include <iostream>
#include <cstdlib> 
#include <limits>
#include <ctime>
using namespace std;


class cBunny{  
public:
string bName;
string bSex;
string bColor;
int bAge;
cBunny();
}; 

cBunny::cBunny() {
       string names[] = {"mutant", "spider", "nuclear","vampire","werewolf","racoon","midget","rabbies","furry","Prof"};
       const int count = (sizeof(names) / sizeof(*names));
       int j=(rand()%count);
       int k=(rand()%count);
       bName=names[j]+"_"+names[k]+"_"+"bunny";
       
        int g=(rand()%2);
        if (g==0){bSex="Female";}
        else{bSex="Male";}
        
        string colors[] = {"Brown", "Black", "Spotted", "White","Red"};
        const int count2 = (sizeof(colors) / sizeof(*colors));
        int n=(rand()%count2);
        bColor=colors[n];
        
        bAge=0;
}


int main ()
{
  srand(time(NULL));
  
  const int m=5;
  
  cBunny oBunny[m];         
  
  cout<< "There are " <<" "<<m<<" "<< "Bunny's created!" <<endl ;
  cout<<"---------------------------------------------------"<<endl;
  for(int c=0; c<m;c++){         
  cout<< oBunny[c].bName<<endl;
  cout<< oBunny[c].bSex<<endl;
  cout<< oBunny[c].bColor<<endl;
  cout<< oBunny[c].bAge<<endl;
  cout<<endl;
  }
cout<<"-------------------------------------------------------------"<<endl;

int turn=0;
int a=m;
while(turn<10){
               
                    
           for(int b=0; b<m;b++){         
                   cout<< oBunny[b].bName;
                   oBunny[b].bAge=oBunny[b].bAge+1;
                   cout<<" "<< "is" <<" "<< oBunny[b].bAge<<" "<<"Year's old!" <<endl;
                   cout<<endl;
 
  } 
               
               
               cout<<"New bunny created!"<<endl;
               cout<<"-----------------------------------------"<<endl;
               
               a=a+1;
               cout<<a;
             // /*
             // cBunny* oBunny = new cBunny[a];  //here i want to create an new object and add it to the existing array of bunny's
              
              // /*
             //  oBunny[a] = new cBunny();
             //   cout<< oBunny[a].bName<<endl;
             //   cout<< oBunny[a].bSex<<endl;
             //   cout<< oBunny[a].bColor<<endl;
             //   cout<< oBunny[a].bAge<<endl;//*/
                
                cout<<endl;
               cout<<"-----------------------------------------"<<endl;
               
               turn++;
               cout<<"---------------------- End of"<<" "<<turn<<" "<<"Turn!-------------------------"<<endl;
}


  int i;
  cin>> i;
  
return 0;
}




since you certainly also need to remove a bunny use a list:

http://www.cplusplus.com/reference/list/list/
i am trying to figure out how to manage a list, with a previous exercise (pancakes), but i am a bit stuck on the sorting of the list?


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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

#include <iostream>
#include <cstdlib> 
#include <limits>
#include <ctime>
#include <list>
#include <string>
using namespace std;

int main ()
{
    
  /*
Write a program that asks the user to enter the number of pancakes eaten 
for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the data and output 
which person ate the most pancakes for breakfast.

? Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

???? Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes
   */
    
struct Node{
    string name; // Name
    int number_pancakes; // number of pancakes eaten
    Node *next;
    }; 
Node *start_ptr = NULL; // Start Pointer (root)


cout<<"Enter the number of people: ";
int number_people;
cin>>number_people;
int g;


//make list for given number of people
for( g=1;g<=number_people;g++){   
        Node *temp, *temp2, *temp3;
        temp = new Node; 
        
        cout << "Please enter the name of the person: ";
        cin >> temp->name;
        cout << "Please enter the number of pancakes eaten: ";
        cin >> temp->number_pancakes;
        temp->next = NULL;
      
      // Set up link to this node
      if (start_ptr == NULL){
      start_ptr = temp;
      }
      else
       {
        temp2 = start_ptr; // We know this is not NULL - list not empty!
        while (temp2->next != NULL){    
            temp2 = temp2->next; // Move to next link in chain
            }
        temp2->next = temp;
        } 
        }
    
    
  //print list     
        if (start_ptr!=NULL ){  //output list 
           Node *temp3;     
              temp3 = start_ptr;
              cout << "Name : " << temp3->name << endl;
              cout << "Pancakes : " << temp3->number_pancakes << endl;
              cout << endl; 
        
              while (temp3->next != NULL){  
                  temp3 = temp3->next; // Move to next link in chain
                  cout << "Name : " << temp3->name << endl;
                  cout << "Pancakes : " << temp3->number_pancakes << endl;
                  cout << endl; 
                  }
              }    


//sorting list
    cout<<"Sort list, enter 1 for 'Yes' or 0 for 'No' ?";
    int sort_list;
    cin>>sort_list;
    if(sort_list==1){
                     Node *trail, *current; 
                     trail = start_ptr;
                     current = start_ptr;
                     while (current->next != NULL){ 
                           current = current->next;
                           if (current->number_pancakes < trail->number_pancakes){
                                                            
                                      trail->next=current->next;
                                      current=start_ptr;
                     
                                       }
                        }           
    }


  //print list  again   
        if (start_ptr!=NULL ){  //output list 
           Node *temp6;     
              temp6 = start_ptr;
              cout << "Name : " << temp6->name << endl;
              cout << "Pancakes : " << temp6->number_pancakes << endl;
              cout << endl; 
        
              while (temp6->next != NULL){  
                  temp6 = temp6->next; // Move to next link in chain
                  cout << "Name : " << temp6->name << endl;
                  cout << "Pancakes : " << temp6->number_pancakes << endl;
                  cout << endl; 
                  }
              }  




    int ii;
    cin>>ii;
    return 0;
}
I suggest that you put those sections you started with a comment like //print list in it's own function.
So you don't have code replication like //print list again

Since you use a singly linked list it would be the easiest to sort when an entry will be inserted. This means finding the first entry that is greater and link before that.

If you want to do the sort later you need a temporary start_ptr, unlink the first entry from your current list (until there's none) and insert sorted in the temporary list. Then make the temporary list the current.

One more tip. Simplify your code like so:
1
2
3
4
5
6
7
8
  //print list     
           Node *temp3 = start_ptr;
              while (temp3 != NULL){  
                  cout << "Name : " << temp3->name << endl;
                  cout << "Pancakes : " << temp3->number_pancakes << endl;
                  cout << endl; 
                  temp3 = temp3->next; // Move to next link in chain
                  }
thnx for the tips i'm gonna try them:)
The bunnies are to be sorted by age right? Just do "push_front" when adding a bunny, and this ensures they are sorted already.

An easy sort is an insertion sort, you basically make the list again, but insert items in a sorted way:
http://en.wikipedia.org/wiki/Insertion_sort
Last edited on
So i try to implement some functions now, but i have some trouble passing on and returning the start position of the list?

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <cstdlib> 
#include <limits>
#include <ctime>
#include <list>
#include <string>
using namespace std;

struct Node{
    string name; 
    int number_pancakes; 
    Node *next;
    }; 

void print_list(Node *start){
      if (start!=NULL ){  
           Node *temp= start; 
                cout << "Name : " << temp->name << endl;
                  cout << "Pancakes : " << temp->number_pancakes << endl;
                  cout << endl;     
              while (temp->next != NULL){  
                  temp = temp->next; // Move to next link in chain
                 cout << "Name : " << temp->name << endl;
                  cout << "Pancakes : " << temp->number_pancakes << endl;
                  cout << endl; 
                  }
              }  
     };

Node create_list(int list_length){
	Node *start_ptr = NULL;

	for(int g=1;g<=list_length;g++){   
        Node *temp, *temp2;
        temp = new Node; 
        
        cout << "Please enter the name of the person: ";
        cin >> temp->name;
        cout << "Please enter the number of pancakes eaten: ";
        cin >> temp->number_pancakes;
        temp->next = NULL;
      
      // Set up link to this node
      if (start_ptr == NULL){
      start_ptr = temp;
      }
      else
       {
        temp2 = start_ptr; //  list not empty!
        while (temp2->next != NULL){    
            temp2 = temp2->next; // Move to next link in chain
            }
        temp2->next = temp;
        } 
        }
	return *start_ptr;  //return start position
};

Node sort_list(Node *start_ptr){return *start_ptr;};
int main ()
{
 
cout<<"Enter the number of people: ";
int number_people;
cin>>number_people;

create_list(number_people);
cout<<"list is made!"<<endl<<endl;

cout<<"printing list...."<<endl;
print_list(*start_ptr);

int ii;  //just to pause
cin>>ii;
    
    return 0;
}
create_list is supposed to return a pointer to Node. Now you return a copy:
1
2
3
4
Node *create_list(int list_length){ // Note: *
...
	return start_ptr;  // Note: no *
}

The same applies to sort_list

On line 67: You need to assign the result. i.e.
Node *start_ptr = create_list(number_people);

On line 71: You're trying to pass the object itself (you dereference it when you do it like that), but you need to pass the pointer
print_list(start_ptr); // Note: no *
many thx again for your help! it works now:)

so my sorting functions works now:D

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Node *sort_list_up(Node *start_ptr){
					 cout<<"sorting list, low to high!......."<<endl<<endl;
					 Node *trail, *current, *front, *start_temp, *start_trail;  
					 
					 start_temp = start_ptr;          //init
					 start_trail = start_ptr;
                                         current = start_ptr;
					 front  = start_ptr;
					 trail =  start_ptr;

					 int length_list=1;
					 while (current->next != NULL){     //getting length of the list.
					 length_list++;
					 current = current->next;
					 };

					 int count=1;

				     again:
					 trail = start_temp;         //reset
					 current = start_temp;

                     while (current->next != NULL){ 
                           current = current->next;
                           if (current->number_pancakes < start_temp->number_pancakes ){
                                      
                                      if (trail==start_temp){
                                         start_temp->next=current->next;
                                         current->next=start_temp;
                                      }
                                      else{
                                           front=current->next;
                                           current->next=start_temp->next; 
                                           start_temp->next=front;
                                           trail->next=start_temp;
                                      }                   
                                      start_temp=current;
                                      trail=start_temp; 
									  front=start_temp; 
                           }
                          else{
                           trail=trail->next;
                           }
                        }
					 
					 
					 
					 if(count==1 && count < length_list){
						start_ptr=start_temp;          //use a start_temp now
						start_temp=start_temp->next;
						count++;

						goto again;
					 }
					
					 else if(count==2 && count < length_list){
						 start_ptr->next=start_temp;  // put start_ptr at front of the list list now sorted with the two smallest in front.

						 start_trail=start_temp;   //trail reset
						 start_temp=start_temp->next; //put start_temp a position further					 
						 count++;

						goto again;
					 }
					 
					 else if(count>=3 && count < length_list){
						start_trail->next = start_temp;  // close list 
						
						start_trail=start_temp;   //trail reset
						 start_temp=start_temp->next; //put start_temp a position further
						count++;

						goto again;
					 }  
					 
                      return start_ptr;
};
Last edited on
Topic archived. No new replies allowed.