I am so lost in this programming assignment. Here is what I am suppose to do:
A. Define a struct that represents a CAT as follows:
A CAT has a weight, name, and a neutered flag, i.e., those are the components of the struct. Use a typedef.
B. Declare a CAT* pMax. Allocate space for the CAT from the heap – assign its components to 12 (his weight), “Max”, FALSE (he is not neutered). Look at the cat in the debugger.
C. Write a function called printCat that outputs a CAT neatly. Pass the CAT by REFERENCE. Call your function passing in pMax
D. Write a function called neuter that takes a pointer to a CAT. If the cat is already neutered, print a message to indicate that. If the cat is not neutered, output the comments that the cat might have (G-rated, as I am easily embarassed ) and change the cat’s properties so that it is now a neutered cat. Pass max to your neuter function, and then pass him to the printCat function again to verify that he is now neutered.
E. Declare an array of 10 CATs on the stack. Fill in the cats in a loop, by randomly assigning their properties. Each Cat will have one of these names (below), will weigh between 1 and 30 pounds (randomly), and will either be neutered or not (randomly).
char *names[6] = { “Fluffy” , “Tigger”, “Max”, “Betty”, “Cat-27” , “Jake”};
F. Output the cats by passing them to the printCat function one by one.
G. Neuter all the cats. Output the Cats again.
H. Allocate an array of CATs from the heap:
a. You will use the datatype CAT**.
b. Allow space for 5 CATs.
c. Write a factory function that will allocate the space for a CAT from the heap, and return a pointer to the CAT that was created.
d. Output all of the CATs
e. Neuter the array of cats that was dynamically allocated.
f. Output all of the cats again.
I am realtively new to C programming and my instructor is not clear on his instructions. I've sent him an email but it usually takes him awhile to get back to me. Any ideas on how to start. Any example code you could give. Below is what I have so far just am unsure if I am going in the right direction. Any help would be greatly appreciated!!
Thanks!
geekgirl2011
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
#define FLUFFY 1
#define TIGGER 2
#define MAX 3
#define BETTY 4
#define CAT27 5
#define JAKE 6
typedef struct {
char name[10];
int weight;
int neutered;
} CAT;
void fillInCat(CAT catNumber[] ,int* pmax);
void printCat(CAT[], int* pmax);
void neutered(CAT[], int* pmax);
void fillInCat(CAT catNumber[], int*pmax){
int i, neut, catName, pounds;
for(i = 0; i < *pmax; i++){
catName = rand () % 6;
pounds = rand () % 30;
neut = rand () % 2;
if( catName == 1){
strcpy(catNumber[*pmax].name,"Fluffy");
}
if( catName == 2){
strcpy(catNumber[*pmax].name,"Tigger");
}
if( catName == 3){
strcpy(catNumber[*pmax].name,"Max");
}
if( catName == 4){
strcpy(catNumber[*pmax].name,"Betty");
}
if( catName == 5){
strcpy(catNumber[*pmax].name,"Cat-27");
}
if( catName == 6){
strcpy(catNumber[*pmax].name,"Jake");
}
catNumber[*pmax].weight = pounds;
catNumber[*pmax].neutered = neut;
(*pmax)++;
}
}
main() {
CAT max;
srand(time(NULL));
strcpy(max.name,"MAX");
max.weight = 12;
max.neutered = FALSE;
printCat(max);
}
void printCat(CAT myCat){
if(myCat.neutered == FALSE)
printf("Cat Name is %s\nWeight is %i\nNeutered is False\n", myCat.name, myCat.weight);
else
printf("Cat Name is %s\n Weight is %i\n Neutered is True\n", myCat.name, myCat.weight);
system("pause");
}