In: Computer Science
****C language****
char lName[][15] = {"Brum","Carroll","Carter","Dodson","Garbus", "Greenwood", "Hilliard", "Lee", "Mann", "Notz", "Pastrana", "Rhon", "Rodriguez", "Wilson", "Zimmerman"};
char fName [][15] = {"Natalie","Cody","Sophia","Dominic","Chandler","Caleb","Sydnee","Peyton","Brianna","Zachery","Kevin","Luke","Juan","Kelci","Adam"};
char middleInitial[15]={'N','L','X','L','O','L','M','B','S','T','J','C','P','D','Z'};
char dob[][11]={"05/27/1935","11/27/1971","10/17/2003","12/08/1990","11/25/1991","10/30/1992","09/22/1993","08/04/1994","07/11/1995","06/18/1996","05/28/1997","04/07/1998","03/12/1999","02/23/2000","01/15/2001"};
How would we make a list ordered by their age, oldest first, Print the patient's full name and then their age. Left justify the name and right justify the age.
Example:
Johnson, Fred N 80
**Half of the code is provided**
int patientAge[15] = {0};
for(int p = 0; p <15; p++)
{
int year = ((dob[p][6] - '0') * 1000) + ((dob[p][7] - '0') *100) +
((dob[p][8] - '0') * 10) + ((dob[p][9] - '0') * 1);
patientAge[p] = 2019 - year;
printf("%s, %s %c Age: %d\n",lName[p], fName[p], middleInitial[p],
patientAge[p]);
}
#include <stdio.h> #include <string.h> char lName[][15] = {"Brum","Carroll","Carter","Dodson","Garbus", "Greenwood", "Hilliard", "Lee", "Mann", "Notz", "Pastrana", "Rhon", "Rodriguez", "Wilson", "Zimmerman"}; char fName [][15] = {"Natalie","Cody","Sophia","Dominic","Chandler","Caleb","Sydnee","Peyton","Brianna","Zachery","Kevin","Luke","Juan","Kelci","Adam"}; char middleInitial[15]={'N','L','X','L','O','L','M','B','S','T','J','C','P','D','Z'}; char dob[][11]={"05/27/1935","11/27/1971","10/17/2003","12/08/1990","11/25/1991","10/30/1992","09/22/1993","08/04/1994","07/11/1995","06/18/1996","05/28/1997","04/07/1998","03/12/1999","02/23/2000","01/15/2001"}; int getYear(char *dob) { return ((dob[6] - '0') * 1000) + ((dob[7] - '0') *100) + ((dob[8] - '0') * 10) + ((dob[9] - '0') * 1); } void swapChars(char *x, char *y) { char a[100]; strcpy(a, x); strcpy(x, y); strcpy(y, a); } void swapIndices(int i, int j) { swapChars(lName[i], lName[j]); swapChars(fName[i], fName[j]); swapChars(dob[i], dob[j]); char c = middleInitial[i]; middleInitial[i] = middleInitial[j]; middleInitial[j] = c; } int main(void) { const int n = 15; int patientAge[n] = {0}; for(int i = 0; i < n; i++) { int min_idx = i; for(int j=i+1; j<n; j++) { if (getYear(dob[j]) < getYear(dob[min_idx])) { min_idx = j; } } swapIndices(i, min_idx); } for(int i = 0; i < 15; i++) { char name[100] = {'\0'}; char mid[2] = {middleInitial[i], '\0'}; strcat(name, fName[i]); strcat(name, " "); strcat(name, mid); strcat(name, " "); strcat(name, lName[i]); patientAge[i] = 2019 - getYear(dob[i]); printf("%-25sAge: %d\n",name, patientAge[i]); } return 0; }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.