Hi I need help with my homework assignment and a little lost of how to have the
ID: 3771638 • Letter: H
Question
Hi I need help with my homework assignment and a little lost of how to have the program search and print the members that live in Texas.
Document a simple program that utilizes a good design process and incorporates sequential, selection and repetitive programming statements as well as at least one function call and the use of at least one array. The specific problem you need to solve for the final project is:
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 is what I have thus far:
#include <stdio.h>
int main(void)
{
char UName;
char nameDatabase[50];
char UState;
char stateDatabase[50];
int UAge, i, count, sum;
int ageDatabase[50];
float average;
i = 1;
count = 1;
sum = 0;
printf("Enter a family member's name, two character state they reside in and age (Sally 46 VA for example): ");
for (i = 1; i < 50; i++)
{
/*Input Name, State, and Age*/
scanf("%c", &UName);
scanf("%c", &UState);
scanf("%d", &UAge);
/*Set values equal to array*/
nameDatabase[count] = UName;
stateDatabase[count] = UState;
ageDatabase[count] = UAge;
count = count +1;
}
/*Print Results*/
for(i = 1; i<count; i++)
{
printf("%c", nameDatabase[i]);
printf("%c", stateDatabase[i]);
printf("%d", ageDatabase[i]);
sum += ageDatabase[i];
}
//Print average age of family
average = sum/(count-1);
printf("The average age of the family is %f.", average);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Global Variables
int i=0, a=0, sum=0;
float avg=0.0;
struct family
{
char name[15];
int age;
char state[15];
}FAMILY[50];
void avgage()
{
for (i=0; i<a; ++i)
{
sum = FAMILY[i].age+sum;
}
}
int main()
{
// struct family FAMILY[50];
strcpy(FAMILY[i].state, "texas");
while (a<=0)
{
printf("Enter number of family members: ");
scanf("%d", &a);
if(a>50)
printf("You can not enter more than 50. ");
}
printf(" ");
for (i=0;i<a;++i)
{
printf(" First name only please: ");
scanf("%s", FAMILY[i].name);
printf("Age: ");
scanf("%d",&FAMILY[i].age);
printf("State of residence: ");
scanf("%s", FAMILY[i].state);
}
printf(" ");
printf("This is the information that you entered.");
for (i=0;i<a;++i)
{
printf(" Family Member #:%d ", i);
printf("Name entered: %s ", FAMILY[i].name);
printf("Age entered: %d ", FAMILY[i].age);
printf("State entered: %s ", FAMILY[i].state);
}
for (i=0;i<a;++i)
{
if (!strcmp(FAMILY[i].state, "texas"))
{
printf("The family members that live in texas are: ");
printf(" Family Member #:%d ", i);
printf("Name entered: %s ", FAMILY[i].name);
printf("Age entered: %d ", FAMILY[i].age);
printf("State entered: %s ", FAMILY[i].state);
}
}
avgage();
printf("The average age of your family is: %d", sum);
return 0;
}
thank you