Question
Hi there, I coded this program in C language and it works perfectly. I am wanting to use functions so that the main is pretty
Hi there, I coded this program in C language and it works perfectly. I am wanting to use functions so that the main is pretty much empty. I got stuck and I am completely unable to make the functions work. I wish to make two more functions in my program. One that prints the instructions and scans the data from the user, and lastly one that prints the output. I did make a function that works already which manipulates the data to order it correctly. I have put in my code where I want the functions to begin:
Here is my code:
#include
#include
#include
//prototype for the function that sorts names and ages
void sortInputData(char name[20][100], int age[20], int n, char lastName[20][100]);
int main()
{
char name[20][100];
char lastName[20][100];
int age[20];
int numberEntries = 0;
int c = 0;
//***************Function to Print Instructions *****************//
//print initial instructions to user
printf("Please enter the number of people you wish to enter into the database: ");
//scan how many entries user wishes - store in numberEntries
scanf("%d", &numberEntries);
//loop for how ever many entries user decided on
for (int i = 0; i < numberEntries; i++)
{
//clears out the input buffer each loop
while ((c = getchar()) != ' ')
{
}
//print instructions to user
printf(" For person #%d: ", i + 1);
printf("Enter name: ");
scanf("%s",name[i]);
printf("Enter last name: ");
scanf("%s",lastName[i]);
printf("Enter age: ");
scanf("%d", &age[i]);
}
//***************Function to Print Instructions *****************//
// call the sortNamesAndAges() function
sortInputData(name, age, numberEntries, lastName);
//*******************Function to print Output****************//
printf(" --------------- Printing sorted names --------------- ");
for (int i = 0; i < numberEntries; i++)
{
// print the user names and ages
printf("name: %s ", name[i]);
printf("last name: %s ", lastName[i]);
printf("age: %d ", age[i]);
//*******************Function to print Output****************//
}
return 0;
}
void sortInputData(char name[20][100], int age[20], int n, char lastName[20][100])
{
// temporary char array 's' and int 'temp' to use for copying
char s[100];
char a[100];
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
// swap names
strcpy(s, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], s);
//swap the order of the age
temp = age[i];
age[i] = age[j];
age[j] = temp;
strcpy(a, lastName[i]);
strcpy(lastName[i], lastName[j]);
strcpy(lastName[j], a);
}
}
}
}
I will for sure thumbs up any help on this topic!!! Thank you so much
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started