Write a program in \"C Programming\" code that will allow a user to Input a list
ID: 665237 • Letter: W
Question
Write a program in "C Programming" code 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.
Write program in "C" from the provided pseudocode listed below:
Module Main()
Declare s_Names as String Array // Array to store names
Declare s_Age as Int Array // Array to store ages
Declare s_State as String Array // Array to store state of residence
Declare i_Count as Int // Variable to hold count of total family members
Declare f_avAvg as float // Variable to hold average family age
Declare lc as Int // loop counter
While (have more family members to enter) // loop to input family data
Display “Enter Member Name:”
Input s_Names(lc) // Input name
Display “Enter Member Age:”
Input s_Age(lc) // Input age
Display “Enter state”
Input s_State(lc) // Input state
Increment lc and i_Count
End while
calcAverageAge(s_Age, i_Count) // Call module to calculate and display average age
call printTexans(s_Names, s_State, i_Count) // Print names of Texans
End Main
Module calcAverageAge(s_Age, i_Count) // Module to calculate average family age
Declare f_Avg as float // Variable to hold average age result
Declare lc1 as Int // loop counter
Declare sum as Int // variable to hold sum of ages
While (lc1 < i_Count) // loop to sum all ages
Sum = Sum + s_Age
Increment lc1
End While
f_Avg = Sum/i_Count // Calculate average age
Return f_Avg // Return average age
End Module
Module printTexans(s_Names, s_State, i_Count) // Module to print names of Texans from the family
Declare lc1 as Integer // loop counter
While (lc1 < i_Count) // Loop to check all family members
If s_State(lc1) = “Texas” // Check if the member is Texan
Print s_Name(lc1) // Print the name of the Texan
End If
End While
End Module
Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXN 10
void calcAverageAge(int s_Age[],int i_Count)
{
float f_Avg;
int lc1=0,sum=0;
while(lc1<i_Count)
{
sum=sum+s_Age[lc1];
lc1++;
}
f_Avg=sum/((float)i_Count);
printf("Average: %f ",f_Avg);
}
void printTexans(char s_Names[MAXN][20],char s_State[MAXN][20],int i_Count)
{
int lc1=0;
while(lc1<i_Count)
{
if(strcmp(s_State[lc1],"Texas")==0)
{
puts(s_Names[lc1]);
}
lc1++;
}
}
int main()
{
char s_Names[MAXN][20];
int s_Age[MAXN];
char s_State[MAXN][20];
int i_Count=0;
float f_avAvg;
int lc=0;
while(1)
{
char te;
printf("Enter Member Name: ");
scanf("%s",s_Names[lc]);
scanf("%c",&te);
printf("Enter Member Age: ");
scanf("%d",&s_Age[lc]);
printf("Enter state: ");
scanf("%s",s_State[lc]);
scanf("%c",&te);
lc++;
i_Count++;
printf("Do you want to enter more member details: (Y/N)");
scanf("%c",&te);
if(te=='N')
break;
}
calcAverageAge(s_Age,i_Count);
printTexans(s_Names,s_State,i_Count);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.