linked list bunnies problem

i need to know how to correct the error in the struct but i don't know how. please help me

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  #include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>


//Global variables
int maxAgeRAMVP = 50;
int maxAgeBunny = 10;
int adultAge = 2;
int maxPopulation = 1000;


typedef struct  Bunny *ptobunny;
struct Bunny{
	//specified attributes
	char *gender;
	char *name;
	char *color;
	bool radioactive_mutant_vampire;
	int age;
	ptobunny *next;
	bool alive;

};
	//Constructor

	char *genders()
	{
		int randomNumber = rand() % 2 + 1;
		char *gender = "";

		if (randomNumber == 1)
		{
			gender = "female";
		}
		else
		{
			gender = "male";
		}
		return gender;
	}

	char *names(char *gender)
	{
		int randomNumber = rand() % 4 + 1;
		char *name = "";

		if (gender == "male")
		{
			switch (randomNumber)
			{
			case 1: name = "Thumper";
				break;

			case 2: name = "Jack";
				break;

			case 3: name = "Bugs";
				break;

			case 4: name = "Julius";
				break;

			};
		}
		else if (gender == "female")
		{
			switch (randomNumber)
			{
			case 1: name = "Pink";
				break;

			case 2: name = "Lulu";
				break;

			case 3: name = "Fluffy";
				break;

			case 4: name = "Mini";
				break;
			};
		}
		return name;
	}

	char *colors()
	{
		int randomNumber = rand() % 4 + 1;
		char *color = "";
		switch (randomNumber)
		{
		case 1: color = "white";
			break;

		case 2: color = "brown";
			break;

		case 3: color = "black";
			break;

		case 4: color = "spotted";
			break;
		};

		return color;
	}

	bool radioActivity()
	{
		int randomNumber = rand() % 100 + 1;
		bool ramvb = false;

		if (randomNumber < 3)
		{
			ramvb = true;
		}

		return ramvb;
	}
	
	ptobunny newbunny()
	{
		char *gender = genders();
		char *name = names(gender);
		char *color = colors();
		bool radioactive_mutant_vampire = radioActivity();
		int age = 0;
		bool alive = true;
		ptobunny next = NULL;
		return NULL;
	}



void incrementAllBunniesAge(ptobunny first) {
	while (first != NULL)
	{
		first->age = first->age + 1;
		first = first->next;
	}
}

void addBunny(ptobunny bunny)
{
	int maleCount = 0;
	int totalPopulation = 0;
	ptobunny first = bunny;
	struct Bunny *mother;


	while (bunny != NULL)
	{
		totalPopulation++;
		if (bunny->gender == "male" && !bunny->radioactive_mutant_vampire && bunny->age >= adultAge && bunny->alive)
			maleCount++;
		bunny = bunny->next;
	}

	while (first != NULL)
	{
		struct Bunny *temp=newbunny();
		if (maleCount > 0 && first->gender == "female" && first->age >= adultAge && !first->radioactive_mutant_vampire && first->alive)
		{
			mother = first;
			temp->color = mother->color;
			temp->next = mother->next;
			mother->next = temp;
			first = temp->next;
		}
		else
		{
			first = first->next;
		}
	}
}



void printAllBunnies(ptobunny first) {
	int i = 1;
	while (first != NULL)
	{
		if (first->alive)
		{
			printf("name[%s] /t gender:%s/tage: %d/t vampire:%b/t", first->name, first->gender, first->age, first->radioactive_mutant_vampire);
		}
		first = first->next;
	}

}

void killBunnies(struct Bunny *first)
{
	bool alive = true;

	while (first != NULL)
	{
		if (!first->radioactive_mutant_vampire && first->age > maxAgeBunny)
		{
			
			first->alive = false;

		}
		else if (first->radioactive_mutant_vampire && first->age > maxAgeRAMVP)
		{
			
			first->alive = false;
		}
		first = first->next;
	}

}

void foodShortageMassacre(struct Bunny *first)
{
	struct Bunny *temp = first; //save first of list 
	int totalPopulation = 0;


	while (first != NULL)
	{
		if (first->alive) //count only living bunnies
		{
			totalPopulation++;
		}
		first = first->next;
	}

	int killings = totalPopulation / 2; //number of bunnies that need to die

	while (totalPopulation > maxPopulation) //condition for starting this function  - while or if??????
	{


		first = temp; //rewind to first of list
		printf("Due to food shortage following bunnies have died: ");

		while (totalPopulation > killings && first != NULL)
		{
			if (first->alive && first->age > 0)
			{
				first->alive = false; //kill bunny
				printf("%s",first->name);
				totalPopulation--; //reduce population
			}
			first = first->next;           //repeat until population == killings. i.e. half live, half die
		}
	}
}

bool terminates(struct Bunny *first)
{
	bool terminate = false;
	int countDead = 0;
	int totalPopulation = 0;

	while (first != NULL)
	{
		totalPopulation++;

		if (!first->alive)
		{
			countDead++;
		}

		first = first->next;
	}

	if (countDead == totalPopulation)
	{
		terminate = true;
	}

	return terminate;
}


void makeRadioActive(struct Bunny *first)
{
	struct Bunny *temp = first;
	int rAMVBcount = 0;
	int totalPopulation = 0;

	//count number of vampire bunnies
	while (first != NULL)
	{
		if (first->alive)
		{
			totalPopulation++;
			if (first->radioactive_mutant_vampire && first->age > 0)
			{
				rAMVBcount++;
			}
		}
		first = first->next;
	}

	int nonVamps = totalPopulation - rAMVBcount;
	int toBeInfected = 0;

	//number of nonVamps who will be infected
	if (nonVamps >= rAMVBcount)
	{
		toBeInfected = rAMVBcount;
	}
	else
	{
		toBeInfected = nonVamps;
	}


	if (toBeInfected > 0)
	{
		for (int i = 0; i < toBeInfected; i++)
		{
			while (temp->radioactive_mutant_vampire && temp != NULL)
			{
				//point to next bunny;
				temp = temp->next;
			}
			temp->radioactive_mutant_vampire = true; //infect bunny with radioactivity mutation vampirism
			

			temp = temp->next;

			while (!temp->alive && temp != NULL)
				temp = temp->next;
		}
	}
}


int main()
{
	srand((unsigned)time(NULL));
	struct Bunny *b1;
	struct Bunny *b2;
	b1->next = b2;
	struct Bunny *b3;
	b2->next = b3;
	struct Bunny *b4;
	b3->next = b4;
	struct Bunny *b5;
	b4->next = b5;


	while (!terminates(b1))
	{
		killBunnies(b1);
		foodShortageMassacre(b1);
		addBunny(b1);
		makeRadioActive(b1);
		printAllBunnies(b1);
		printf("==================================================");
		incrementAllBunniesAge(b1);
		//press enter to continue
		getchar();
	}

	getchar();
	return 0;
}
whats the error you are facing.can you post it down.
closed account (z1CpDjzh)
ERROR: CODE IS TO BIG TO POINT OUT EVERY SINGLE FLAW
I received a PM from the OPer asking for help. Private help is discouraged because it prevents other people with the same problem from getting help - asking for private help is like saying you're more important than everyone else who also needs the same help.
I received a PM from the OPer asking for help. Private help is discouraged because it prevents other people with the same problem from getting help - asking for private help is like saying you're more important than everyone else who also needs the same help.


It also limits your potential feedback.
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>

typedef struct  Bunny Bunny;

struct Bunny{
    //specified attributes
	//char *gender;
	char gender[8] ;
	
	// char *name;
	char name[16] ;
	
	// char *color;
	char color[16] ;
	
	bool radioactive_mutant_vampire;
	int age;
	
	// ptobunny *next;
	Bunny* next ;
	bool alive;
};

const char *genders() // literal string is const
{
    static const char* gender_str[] = { "Male", "Female" } ;
    return gender_str[ rand() % 2 ] ;
}

const char *names( const char *gender )
{
    if( strcmp( gender, "Male" ) == 0 ) // compare strings with strcmp
    {
        static const char* name_str[] = { "Thumper", "Jack", "Bugs", "Julius" } ;
        static const int num_names = sizeof(name_str) / sizeof( name_str[0] ) ;
        return name_str[ rand() % num_names ] ;
    }
    
    else
    {
        static const char* name_str[] = { "Pink", "Lulu", "Fluffy", "Mini" } ;
        static const int num_names = sizeof(name_str) / sizeof( name_str[0] ) ;
        return name_str[ rand() % num_names ] ;
    }
}

const char *colors()
{
        static const char* clr_str[] = { "white", "brown", "black", "spotted" } ;
        static const int num_clrs = sizeof(clr_str) / sizeof( clr_str[0] ) ;
        return clr_str[ rand() % num_clrs ] ;
}	

bool radioActivity() { return rand()%100 < 20 ; } // 20%

Bunny* newbunny()
{
    Bunny* pb = malloc( sizeof(Bunny) ) ; // allocate memory
    
    // initialise bunny
    strcpy( pb->gender, genders() ) ;
    strcpy( pb->name, names(pb->gender) ) ;
    strcpy( pb->color, colors() ) ;
    pb->radioactive_mutant_vampire = radioActivity() ;
    pb->age = 0 ;
    pb->next = NULL ;
    pb->alive = true ;
    
    return pb ;
}

int main()
{
	srand((unsigned)time(NULL));
	
    // create 11 bunnies
	Bunny* first = newbunny() ;
    int i = 0 ;
	for( Bunny* p = first ; i<10 ; ++i ) 
    { 
        p->next = newbunny() ; 
        p = p->next ; 
    }  
    
    // print out info
    for( Bunny* p = first ; p != NULL; p = p->next ) 
    {
        const char* mutant = p->radioactive_mutant_vampire ? "Radioactive" : "" ;
        printf( "%s %s %s %s\n",  p->name, p->color, p->gender, mutant ) ;
    }
    
    // free the memoty
    Bunny* p = first ;
    while( p != NULL )
    {
        Bunny* next = p->next ;
        free(p) ;
        p = next ;
    }

	return 0 ;
}

http://coliru.stacked-crooked.com/a/cf7d2b6e73053087

Take it forward from there.
what if i want to write it in a binary file ?
> what if i want to write it in a binary file ?

When you feel the urge to design a complex binary file format, or a complex binary application protocol, it is generally wise to lie down until the feeling passes. - ESR in 'The Art of Unix Programming', Chapter 5 'The Importance of Being Textual' http://www.faqs.org/docs/artu/ch05s01.html
Topic archived. No new replies allowed.