I have tried to understand writing C code and I've struggled to grasp this. I'm looking for any help I can get at this point. Below is a problem I need to write the C code for. Is there anyone that can help me?

Design a program that will allow a user to Input a list of your family members along with their age and state where they reside. Determine and print the average age of your family and print the names of anyone who lives in Texas. This simple program will utilize a good design process and incorporate sequential, selection and repetitive programming statements as well as at least one function call and the use of at least one array.

I greatly appreciate any help anyone can offer.

Recommended Answers

All 4 Replies

Sorry, but we don't do your homework for you. I assume you have some C coding text books? There is also plenty of tutorial stuff on the internet for this. In any case, first writing out the processes and procedures you program will implement (pseudo code) is a good first step to take. It will help you determine what you will need to do when you get to the coding phase. Remember the axiom - 90% modeling/design + 10% coding == functional software.

Let's look at the elements of this program. It looks like you will have an input phase, where you will have to store some entred information, and an output phase, where you display some analysis of your input data.

The "sequential" programming means that instructions are to follow each other logically, i.e. how we normally do things. "Selection" is saying you have to use if and/or switch. "Repetitive" means you are using loops; I would normally recommend for loops when working with arrays like this.

It looks like you need 3 pieces of infomation for each person: name, age and state of residence. I would recommend 3 arrays to store the input information: 1 integer and 2 string.

#define MAX_PEOPLE  20

int iAge[MAX_PEOPLE];
char sName[32][MAX_PEOPLE];
char sState[2][MAX_PEOPLE];

The naming convension I am using indicates what type of data each array is expecting, i for integer and s for string (array of char). MAX_PEOPLE is a constant used to define how large the arrays are. Make sure you do not try to put in or get information past the end of
the arrays.

When getting the information from the user, check to make sure you know when the user is done entering names, etc. Perhaps checking if the entered name is empty, or if the input stream has closed (EOF). Use a break; to get out of your input loop.

Avoid using gets() and scanf(), for user input, unless your teacher has specifically told you to use them. gets() does not check for the length of the array you are reading into and could easily go off the end of the array. Use fgets() instead. scanf() does not recover well from bad input. Instead, read one line of input with fgets() and parse it with sscanf(), or ask individual questions and read the information with fgets(), parsing numeric data with atoi() family of functions.

As you get the information from the user, validate it, such as making sure the ages are not negative and that the strings are not empty, unless the user is ending input.

In your output phase, go through your arrays and look for who is from Texas. Also add up all the ages. After the loop, divide the total age by the number of people to get the average age.

Thank you for the help Nutster. I'm still trying to work out the kinks in this code. It has been a headache trying to learn this stuff for me. It's just not registering with me.

To rubberman, did not need a smart ass comment. I never asked anyone to do my homework. If you want to be of no help, then don't comment. I have spent a lot of time on tutorials and watched videos on youtube. I'm just not picking it up.

Oh, I just realized you have to allow for the trailing NULL in your strings. So,

    #define MAX_PEOPLE 20
    int iAge[MAX_PEOPLE];
    char sName[32][MAX_PEOPLE];
    char sState[3][MAX_PEOPLE];

This will allow for 31-character names and 2-character state codes.

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.