Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We are going to combine structs, strings, and command line arguments to create a

ID: 663472 • Letter: W

Question

We are going to combine structs, strings, and command line arguments to create a name/age sorting program.

You will not be using scanf in this program. You will be having your users input names and ages via command line arguments. Here is an example (assuming your program is called hw5 and is in the current directory on UNIX):

./hw5 Tom 56 Billy 26 Megan 25 Jose 15 Zapp 45 Tim 12

Using this information, you will populate an array of structs that hold both the first name (as a string of no more than 80 characters) and the age as an integer. Each name should come with an age. To check for this, count the arguments and produce an error message if they are incorrect (remember, the program name is in the argv array).

I want you to use a typedef for the struct. I suggest you put this typedef outside of main and other functions.

No age should be less than or equal to zero. You will need to convert the ages from strings to ints. You can use a function called atoi in stdlib for this. The inherit advantage is that if the age is NOT a number (like if someone entered "fish" for age), it will just produce zero and you can catch that the same way you would just look for zero as a possible entry.

You will also write a function that will be passed the array of people and produce and print the each person name and age (see sample output).

You are allowed to use qsort from stdlib. You will be using this to sort by age and name SEPARATELY. This is two distinct sorts. (HINT: qsort's final argument is a function. You may have to look up how it is used, but do not copy code directly.).

Sample I/O

./hw5 Tom 56 Billy 26 Megan 25 Jose 15 Zapp 45 Tim 12
You have entered 6 person(s) into the program.
Unsorted Names:
Name: Tom | Age: 56
Name: Billy | Age: 26
Name: Megan | Age: 25
Name: Jose | Age: 15
Name: Zapp | Age: 45
Name: Tim | Age: 12

Sorted by First Name:
Name: Billy | Age: 26
Name: Jose | Age: 15
Name: Megan | Age: 25
Name: Tim | Age: 12
Name: Tom | Age: 56
Name: Zapp | Age: 45

Sorted by Age:
Name: Tim | Age: 12
Name: Jose | Age: 15
Name: Megan | Age: 25
Name: Billy | Age: 26
Name: Zapp | Age: 45
Name: Tom | Age: 56

./hw5 Tom 56 Billy 26 Megan 0
You have entered 3 person(s) into the program.
Invalid age <= 0. Try again.

./hw5 Tom 56 Billy 26 Megan fish
You have entered 3 person(s) into the program.
Invalid age <= 0. Try again.

./hw5 Tom 56 Billy
Invalid Arguments.
Usage: ./hw5 name1 age1 name2 age2 ...

./hw5
Invalid Arguments.
Usage: ./hw5 name1 age1 name2 age2 ...

./hw5 Tom 56 Billy 26 Megan 25 Jose 15 Zapp 45 Tim
Invalid Arguments.
Usage: ./hw5 name1 age1 name2 age2 ...

./hw5 Jim 20 Jam 30 Jon 15
You have entered 3 person(s) into the program.
Unsorted Names:
Name: Jim | Age: 20
Name: Jam | Age: 30
Name: Jon | Age: 15

Sorted by First Name:
Name: Jam | Age: 30
Name: Jim | Age: 20
Name: Jon | Age: 15

Sorted by Age:
Name: Jon | Age: 15
Name: Jim | Age: 20
Name: Jam | Age: 30

Explanation / Answer

Program:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

//struct to hold name and age

struct information

{

char name[80];

int age;

};

//main method

int main(int argc,char* argv[])

{

int i;

char name1[80];

int age1;

int n=argc/2;

struct information info[100];

clrscr();

if(argc==0 || argc%2!=1)

printf("invalid arguments");

else

printf(" You have entered %d(s) into the program",n);

for(i=0,j=0;i<n;i++,j+2)

{

sscanf(argv[j],"%s",name1);

//info[i].name=argv[i];

//info[i].age=atoi(argv[i+1]);

sscanf(argv[j+1],"%i",&age1);

strcpy(info[i].name,name1);

info[i].age=age1;

}

printf(" Unsorted names:");

for(i=0;i<n;i+2)

{

printf(" Name:%s | Age: %d",info[i].name,info[i].age);

}

//sort by names

for(j=0; j < argc - 1; j++)     

    {

        for(k = argc - 1; j < k ; k--)        {

            if(strcmp(info[k-1].name, info[k].name) > 0)

            {  

                strcpy(name1,info[k-1].name);

                strcpy(info[k-1].name,info[k].name);

                strcpy(info[k].name,name1);

                age1=info[k-1].age1;

               info[k-1].age=info[k].age;

               info[k].age=age1;

               

            }

        }

    }

//print the sorted list

printf(" Sorted by names:");

for(i=0;i<argc;i++)

{

printf(" Name:%s | Age: %d",info[i].name,info[i].age);

}

//sort by age

for(j=0; j < argc - 1; j++)     

    {

        for(k = argc - 1; j < k ; k--)

        {

          if(info[k-1].age>info[k].age)

          {

               age1=info[k-1].age;

               info[k-1].age=info[k].age;

               info[k].age=age1;

               strcpy(name1,info[k-1].name);

                strcpy(info[k-1].name,info[k].name);

                strcpy(info[k].name,name1);

          }

     }

}

//printed the sorted list by age

printf(" Sorted by age:");

for(i=0;i<argc;i++)

{

printf(" Name:%s | Age: %d",info[i].name,info[i].age);

}

return 0;

}//end main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote