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");
}

Recommended Answers

All 9 Replies

typedef struct {
	char name[10];
	int weight;
	int neutered;
} CAT;

That fulfills requirement A
Requirement B is not found at this point.
Requirement C is not correct.

C. Write a function called printCat that outputs a CAT neatly. Pass the CAT by REFERENCE. Call your function passing in pMax

Do you understand what REFERENCE means here? If the prototype is:

void printCat(CAT myCat);

CAT myCat is passed as VALUE or a copy of the real CAT structure. A reference would be a pointer. Another hint is that you need to pass pMax which is a pointer to CAT
Try those, before we get into more.

something's not right, here. how are you gonna neuter a cat named "Betty" ?

:S

commented: It became Betty after the operation. Haha! +8
commented: Dude, FTW +4
commented: The same you neuter my dog named Rex, since she squats to pee. :P +11
typedef struct {
	char name[10];
	int weight;
	int neutered;
} CAT;

That fulfills requirement A
Requirement B is not found at this point.
Requirement C is not correct.

Do you understand what REFERENCE means here? If the prototype is:

void printCat(CAT myCat);

CAT myCat is passed as VALUE or a copy of the real CAT structure. A reference would be a pointer. Another hint is that you need to pass pMax which is a pointer to CAT
Try those, before we get into more.

I know that reference means a pointer but I'm not sure on how to write the code for it. He has given us no examples. I am so lost. :(

something's not right, here. how are you gonna neuter a cat named "Betty" ?

:S

sex change operation?
or maybe a hermaphroditic cat?

I know that reference means a pointer but I'm not sure on how to write the code for it. He has given us no examples. I am so lost. :(

okay, let's start over, shall we? start off with small steps, follow the instructions and get the first parts working before trying to do the rest. dont try and get it all done at once.

you already had A. I'm going to fix your code so that you now have B and C. notice what i've done, and try working from here.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define   TRUE 1
#define   FALSE 0

typedef struct {
	char name[10];
	int weight;
	int neutered;
} CAT;

void printCat (CAT *cat_ptr);           // passing by reference
void neuter   (CAT *cat_ptr);

char *names[6] = {"Fluffy" , "Tigger", "Max", "Betty", "Cat-27" , "Jake"};

int main (void)                         // main type is always "int"
{
    CAT *pMax;                          // declare a pointer

    pMax = malloc(sizeof(CAT));         // then allocate one CAT on heap

    strcpy(pMax->name,  names[2]);      // set name, weight, neuter to fixed values
    pMax->weight     =  12;
    pMax->neutered   =  FALSE;

    printCat(pMax);                     // print the one cat's info
    neuter(pMax);                       // neuter it
    printCat(pMax);                     // reprint the cat's info

    printf("\n\nhit Enter to quit.\n");     // in case window closes automatically.
    getchar();

    return 0;
}

void printCat(CAT *cat_ptr)
{
    printf("\n   NAME       WEIGHT      NEUTER    \n");
    printf("----------  ----------  ----------  \n");
    printf("%10s      %2d          ", cat_ptr->name, cat_ptr->weight);

    if (cat_ptr->neutered == TRUE)
        printf("Yes\n");
    else
        printf("No\n");

    return;
}

void neuter(CAT *cat_ptr)
{
    printf("\n...Im in ur funshun nooterin ur tomz...\n");

    // what do you need to do here?

    return;
}

Thank you so much!! Now I have something to go off of!! My instructor is horrible, its an online class so I can only contact him by email. It takes forever for him to respond and he never helps...Hopefully I will be able to figure it out from here..I'll let you know when I have more code written!! Thanks again!!

Sincerely,
geekgirl11

Ok been working with the code and I cannot figure out how to change the cat from being neutered or not!! Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define   TRUE 1
#define   FALSE 0

typedef struct {
	char name[10];
	int weight;
	int neutered;
} CAT;

void printCat (CAT *cat_ptr);           // passing by reference
int neuter   (CAT *cat_ptr);

char *names[6] = {"Fluffy" , "Tigger", "Max", "Betty", "Cat-27" , "Jake"};

int main (void)                         // main type is always "int"
{
	CAT *pMax;                          // declare a pointer

int result;
	

	pMax = malloc(sizeof(CAT));         // then allocate one CAT on heap

	strcpy(pMax->name,  names[2]);      // set name, weight, neuter to fixed values
	pMax->weight     =  12;
	pMax->neutered   =  FALSE;

	printCat(pMax);                     // print the one cat's info
	neuter(pMax);                       // neuter it
	printf(" r = %i \n",result);
	printCat(pMax);                     // reprint the cat's info

	printf("\n\nhit Enter to quit.\n");     // in case window closes automatically.
	getchar();

	return 0;
}

void printCat(CAT *cat_ptr)
{
	printf("\n   NAME       WEIGHT      NEUTER    \n");
	printf("----------  ----------  ----------  \n");
	printf("%10s      %2d          ", cat_ptr->name, cat_ptr->weight);

	if (cat_ptr->neutered == TRUE)
		printf("Yes\n");
	else
		printf("No\n");

	return;
}


int neuter(CAT *cat_ptr)
{
	int r = 0;

	if(cat_ptr->neutered == TRUE){
		printf("The Cat is neutered!!\n");
		r = 0;
	}
	if(cat_ptr->neutered == FALSE){
	printf("\nThe Cat is not neutered!!\n");
	r = 1;
	}

	// what do you need to do here?

	return (r);

}

According to requirements.

void neuter (CAT *feline)
{ 
    /* neutered? */
    if (feline->neut) {
        printf("%s has been neutered\n", feline->name);
    } else {
        printf("%s might still have balls\n", feline->name);
        feline->neut = TRUE; /* the bit you're missing */
    }
}
commented: Priceless +4

this is what i was thinking.

void neuter(CAT *this_cat)                 // you dont need to return an int.
{                                          // note also, we can call the char pointer any name
                                           // we want as long as we're consistent in its use.

    if (strcmp(this_cat->name, "Betty") == 0)
    {
        printf("Hey get off me you crazy freak, my name is Betty!   Duh??\n");
        this_cat->neuter = TRUE;                // for lack of a better category
    }

    else if (this_cat->neutered == TRUE)        // already neutered
    {
        printf("Yo, you best open your eyes and step off my grill, dawg!\n");
    }

    else if (this_cat->neutered == FALSE)       // now it's party time
    {
        printf("RRRRROOOWWWWLLL!  PFFFZZZZTTT!   YOOWwwwlll...\n");
        this_cat->neuter = TRUE;                // and the deed is done  :-(
    }

    return;                                     // no need to return an int, 
                                                // the struct is already modified
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.