Can someone convert this to C language? Module Main() Declare s_Names as String
ID: 664804 • Letter: C
Question
Can someone convert this to C language?
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 <stdio.h>
#include <string.h>
float calcAverageAge(int s_Age[], int i_Count){
float f_Avg = 0.0;
int lc1 = 0;
int sum = 0;
while(lc1 < i_Count){
sum += s_Age[lc1];
lc1++;
}
f_Avg = sum/i_Count;
return f_Avg;
}
void printTexans(char *s_Names[], char *s_State[], int i_Count) {
int lc1 = 0;
while (lc1 < i_Count){
char *name = s_Names[lc1];
char *state = s_State[lc1];
char st[] = {};
strcpy(st, state);
if(strcmp(st,"Texas") == 0){
printf("Name of Texan: %s ",name);
}
lc1++;
}
}
int main(void) {
// your code goes here
char *s_Names[] = {};
int s_Age[] = {};
char *s_State[] = {};
int i_Count = 0; //Declare i_Count as Int // Variable to hold count of total family members
float f_avAvg = 0.0; //Declare f_avAvg as float // Variable to hold average family age
int lc = 0; //Declare lc as Int // loop counter
char members = 'y';
while(members == 'y'){
char *name;
int age;
char *state;
printf("Enter Member Name ");
scanf(" %[^ ]s",name);
printf("Enter Member Age ");
scanf("%d",&age);
printf("Enter State ");
scanf(" %[^ ]s",state);
s_Names[lc] = name;
s_Age[lc] = age;
s_State[lc] = state;
printf("Have more family members to enter? (y/n) ");
scanf("%c",&members);
lc++;
i_Count++;
}
float avg = calcAverageAge(s_Age,i_Count);
printf("Average age of family is %f ",&avg);
printTexans(s_Names,s_State,i_Count);
return 0;
}