Question

In: Computer Science

****C language**** char lName[][15] = {"Brum","Carroll","Carter","Dodson","Garbus", "Greenwood", "Hilliard", "Lee", "Mann", "Notz", "Pastrana", "Rhon", "Rodriguez", "Wilson", "Zimmerman"};...

****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]);
}

Solutions

Expert Solution

#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.


Related Solutions

Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1;...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1; char ch2; int main() { cin.get(ch1); cin.get(ch2);    cout << ch1 << chConst << ch2;    return 0; }
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
(In C language) Given a two-dimensional char array, how do I encode it into a one...
(In C language) Given a two-dimensional char array, how do I encode it into a one dimensional integer array? For example: char arr [8][8] = {{1,1,1,1,1,1,1,1}, {1,0,0,0,1,0,0,1}, {1,0,1,0,1,1,0,1}, {1,0,1,0,0,0,0,1}, {1,0,1,1,1,1,0,1}, {1,0,0,0,0,0,0,1}, {1,0,1,0,1,0,1,1}, {1,1,1,1,1,1,1,1}} into int arr2 [8] I know this problem requires bit-shifting but I am unsure how. It also needs to be as efficient as possible. Thanks!
Below is for C language typedef struct _node { Node *next; char *str; } Node; You...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You are given a function that takes in two Node* pointers to two different linked lists. Each linked list node has a string represented by a character array that is properly null terminated. You're function is supposed to return true if the concatenation of strings in a linked list match each other, else return false. EXAMPLES List 1: "h" -> "el" -> "l" -> "o"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT