So, we have this homework assignment that builds off of last weeks. We are supposed to create separate arrays that hold pointers to all the people of a certain job type. Here is the code I have so far. I was wondering if any one can help me out. Thanks!
#include <stdio.h>
#include <stdlib.h>
#include "Person.h"
#include "data.h"
#define new
char * randomVal(char * arr[], int elements)
{
int value = (int)(rand() % elements);
char * name = arr[value];
return name;
}
int main (int argc, const char * argv[])
{
Person * Population[100];
int i;
for (i = 0; i < 100; i++)
{
Population[i] = new person(randomVal(Names, 10), randomVal(Vehicles, 5), randomVal(Jobs, 5), randomVal(Job_Pace, 3), randomVal(Education, 4), randomVal(Work_Ethic, 3), randomVal(Sleep_Habits, 3));
Population[i]->print(Population[i]);
}
for (i=0; i<100; i++) {
destroy(Population[i]);
}
//Person * p1 = new person(randomName(), "Camero", "Lawyer", "Fast", "Doctorate", "Poor", "Poor");
//p1->print(p1);
return 0;
}
/*
* Person.h
* Person2
*
* Created by Tracie Marshall on 10/15/09.
* Copyright 2009 Me. All rights reserved.
*
*/
typedef struct
{
//data
//dynamic sized vars
char * name;
char * vehicle;
char * job;
char * job_pace;
char * education;
char * work_ethic;
char * sleep_habits;
//static-sized vars
double bank_account;
int leadership;
int IQ;
int aggro;
int outfit;
int alertness;
//methods
void (*print)(void *);
} Person;
Person * person(char * n, char * v, char * j, char * jp, char * ed, char * w, char * s);
void printPerson(Person* p);
void destroy(Person * p);
/*
* Person.c
* Person2
*
* Created by Tracie Marshall on 10/15/09.
* Copyright 2009 Me. All rights reserved.
*
*/
#include <string.h>
#include <stdlib.h>
#include "Person.h"
#include <stdio.h>
char * newString(char * dest, char * source)
{
dest = (char *)malloc((strlen(source) + 1) * sizeof(char));
strcpy(dest, source);
return dest;
}
void printPerson(Person* p)
{
printf("\n%s:\n\tvehicle:\t%s\n\tjob:\t\t%s\n\tjob pace:\t%s\n\teducation:\t%s\n\twork ethic:\t%s\n\tsleep habit:\t%s", p->name, p->vehicle, p->job, p->job_pace, p->education, p->work_ethic, p->sleep_habits);//, p->age, p->job, p->spouse);
}
Person * person(char * n, char * v, char * j, char * jp, char * ed, char * w, char * s)
{
//allocate self
Person * p = (Person *) malloc(sizeof(Person));
//allocate dynamic variables on stack
p->name = newString(p->name, n);
p->vehicle = newString(p->vehicle, v);
p->job = newString(p->job, j);
p->job_pace = newString(p->job_pace, jp);
p->education = newString(p->education, ed);
p->work_ethic = newString(p->work_ethic, w);
p->sleep_habits = newString(p->sleep_habits, s);
//initalize static-size variables
p->bank_account; // = ba;
p->IQ;
p->aggro;
p->outfit;
p->alertness;
//initialize function pointers
p->print = &printPerson;
return p;
}
void destroy(Person * p)
{
free(p->name);
free(p->vehicle);
free(p->job);
free(p->job_pace);
free(p->education);
free(p->work_ethic);
free(p->sleep_habits);
free(p);
}
/*
* data.h
* Person2
*
* Created by Tracie Marshall on 10/15/09.
* Copyright 2009 Me. All rights reserved.
*
*/
char * Names[10] = {"Lisa", "Pamela", "Amanda", "Gerard", "Dan", "Patrick", "Peter", "Michael", "Sally", "Tracie"};
char * Vehicles[5] = {"Feet", " Old Bike","New Bike", "Cheap Car", "Expensive Car"};
char * Jobs[5] = {"Unemployed", "Burger World", "Junior Programmer", "Senior Programmer", "Academia"};
char * Job_Pace[3] = {"Easy Going", "9-5 Grind", "Whirl Wind"};
char * Education[4] = {"Bum", "High School", "College", "Graduate School"};
char * Work_Ethic[3] = {"Drifter", "Hard Worker", "Workaholic"};
char * Sleep_Habits[3] = {"Good", "Medium", "Poor"};