Question

In: Computer Science

You must use C Language. End Goal: HATFIELD, HEIDI KAISER, RUSSELL LIPSHUTZ, HOWARD PENKERT, DAWN WRIGHT,...

You must use C Language.

End Goal:

HATFIELD, HEIDI
KAISER, RUSSELL
LIPSHUTZ, HOWARD
PENKERT, DAWN
WRIGHT, ELIZABETH

The user inputs the students first name and last names separately but within one loop. The loop should end when the user presses enter on the first name without entering any text. Upon completing entry of data, the output pictured above should display on the output.

Using the code given, follow the steps:

1. You should be able to enter up to 20 student first names. Also, change the input array to an appropriate size of 18 for the length of the first name. Use a meaningful name for the storage of first names array. Change prompts as needed. The loop should exit when the user presses enter when inputing the first name without adding any text. Compile and make sure it works from main(). At this point, you should be able to enter and alphabetize a list of up to 20 first names! Alphabetizing the first name is just a test!!! In the end, you will alphabetize the whole name string.

2. Add another array and get input for last name INSIDE the loop for your first names. This last name array will also be an array of 20 elements but with room for up to 25 characters. Again, do not use another loop! Just add code to input the last name to the first loop. The program should now ask the user to input the student's first name and then last name in that order for each individual. Then the program will loop to continue adding student names until the user presses enter on the student's first name. Make sure the last name is converted to all caps. You do not need to alphabetize this array, but you may want to print it out to make sure everything is working just as a test.

3. Make changes to convert the first name to all upper case using a function. (Example: User enters bob on the first name, then on the last name enters jenkins, it will look like Bob Jenkins instead of bob jenkins)

Last step: Combine last and first into an third array. This code is most easily added to the first loop. You just had the user enter first and last names. So the current value of the subscript used for these arrays can be used to combine content and store in the third array.  Alphabetize THIS array (instead of the first name array) which means you need to send a different pointer to the stsrt() function. Print out the end result. Test that everything is working on this program.

Given Code:

void rollsheet(void) {
int ct = 0;
char *ptstr[LIMIT];
char input[LIMIT][SIZE];
int k;

printf("Enter up to %d student names, and I will sort them!\n", LIMIT);
printf("To stop, press the Enter key at a line's start.\n");
while (ct < LIMIT && s_gets(input[ct], SIZE) != NULL
&& input[ct][0] != '\0')
{
ptstr[ct] = input[ct]; /* set ptrs to strings */
ct++;
}
stsrt(ptstr,ct);
puts("\nHere's the sorted list:\n");
for (k = 0; k < ct; k++)
puts(ptstr[k]) ; /* sorted pointers */
}

void stsrt(char *strings[], int num)
{
char *temp;
int top, seek;

for (top = 0; top < num-1; top++)
for (seek = top + 1; seek < num; seek++)
if (strcmp(strings[top],strings[seek]) > 0)
{
temp = strings[top];
strings[top] = strings[seek];
strings[seek] = temp;
}
}


char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}

Solutions

Expert Solution

Solution:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LIMIT 20             //Macro contains maximum limit of the names...
#define FIRST_NAME_SIZE 20 //Macro contains maximum limit of the names...
#define LAST_NAME_SIZE 25    //Macro contains maximum size of the names...

char* toUpperCase(char *name);
void rollsheet(void);
char * s_gets(char * st, int n);       /*Declaring prototypes..*/
void stsrt(char *strings[], int num);

/**
*   @Func : toUpperCase
* @brief: To convert the names to upper case..
*/
char* toUpperCase(char *name)
{
   int i;

   for(i=0; *(name + i)!='\0'; i++)
   {
       /* Checks each character is lowercase alphabet */
       if(*(name+i) >= 'a' && *(name+i) <= 'z')
               name[i] = name[i]-32; /*Convert char to Upper case*/
   }
}//end..

/**
*   @Func : rollsheet
* @brief: To manage the get and sort name functions..
* @return: void type(NULL)
*/
void rollsheet(void) {

   int ct = 0;
   char *ptstr[LIMIT];
   char firstName[LIMIT][FIRST_NAME_SIZE];       /*Array to store firstNames..*/
   char lastName[LIMIT][LAST_NAME_SIZE];       /*Array to store firstNames..*/
   int k;

   while (ct < LIMIT)           /*Loop to prompt user to enter names..*/
   {
       printf("\nEnter the first name : ");   /*To get values from user..*/
       if (s_gets(firstName[ct], FIRST_NAME_SIZE) != NULL && firstName[ct][0] != '\0')
       {
           printf("Enter the Last name   : "); /*To get values from user..*/
           if (s_gets(lastName[ct], LAST_NAME_SIZE) != NULL && lastName[ct][0] != '\0')
           {  
               toUpperCase(firstName[ct]);       /*Func call to convert to upper case..*/
               toUpperCase(lastName[ct]);       /*Func call to convert to upper case..*/
               strcat(firstName[ct], ", ");   /*To seperate first and last name..*/
               strcat(firstName[ct], lastName[ct]); /*Combine first and last name..*/
               ptstr[ct] = firstName[ct]; /* Assign ptr to names array */
           }
           else
               break;           /*End of input..*/
       }
       else
           break;           /*End of input..*/

       ct++;               /*Increment the pointer index*/

   }//end_of_while..

   stsrt(ptstr,ct);           /*Function call to sort the names..*/
   printf("\n<---------------------------------------------------->\n\n");
   puts("\nHere's the sorted list:\n");
   for (k = 0; k < ct; k++)
       puts(ptstr[k]) ;        /* Printing sorted array elements(Output..)*/
}

/**
*   @Func : stsrt
* @brief: Sorts the array in the alphabetic order..
* @return: void type(NULL)
*/
void stsrt(char *strings[], int num)
{
   char *temp;
   int top, seek;           /*Index variables..*/

   for (top = 0; top < num-1; top++)       /*Loop to traverse array..*/
       for (seek = top + 1; seek < num; seek++)
           if (strcmp(strings[top],strings[seek]) > 0)
           {
               temp = strings[top];          
               strings[top] = strings[seek];   /*Sorting by swaping*/
               strings[seek] = temp;
           }
}//end..

/**
*   @Func : s_gets
* @brief: To get/read the names from user..
* @return: char* type(array)
*/
char * s_gets(char * st, int n)
{
   char * ret_val;                   /*Stores the returning value..*/
   int i = 0;                       /*Index variable..*/

   ret_val = fgets(st, n, stdin);   /*Get the names from user..*/
   if (ret_val)
   {                               /*Loop to traverse string..*/
       while (st[i] != '\n' && st[i] != '\0')
           i++;
       if (st[i] == '\n') {   /*Check if end of line..*/
           st[i] = '\0';
       } else {            /* should have word[i] == '\0'*/
           while (getchar() != '\n')
               continue;
       }
   }
   return ret_val;       /* Returns the array value..*/
}

/**
*   Main function
*/
int main()
{
   printf("\n<---------------------------------------------------->\n\n");
   printf("Enter up to limit(%d) student names, and it will sorted.!!\n", LIMIT);
   printf("To quit, press 'Enter' key at the fist line of prompt..\n\n");
   rollsheet();           /*Function call to get names...*/
   printf("\n<---------------------------------------------------->\n\n");
   return 0;
}


-----------------------------------------------------------------------

Code screenshots:

-----------------------------------------------------------------------

Output:

-----------------------------------------------------------------------

Kindly up-vote if you were satisfied with this solution.


Related Solutions

c++ language for this assignment use real world example. Your application must be a unique project...
c++ language for this assignment use real world example. Your application must be a unique project and must incorporate the use of an STL container and/or iterator and at least 7 STL algorithms with an option to exit. Your application must contain at least 8 menu options. You will decide how to implement these menu selections.
Goals In this assignment you must use Java language to program the small application for a...
Goals In this assignment you must use Java language to program the small application for a grocery buyer. You will create meaningful utility that implements simple math model for real problem; design of the application logic; basic user interface. The implementation plus coding style and efficiency will be graded. Description The supermarket uses three pricing scenarios for the local products – Monday, Wednesday, and Friday, the product A has fixed regular price of X dollars. Tuesday and Thursday, the product...
Done in C language using mobaxterm if you can but use basic C This program is...
Done in C language using mobaxterm if you can but use basic C This program is broken up into three different functions of insert, delete, and main. This program implements a queue of individual characters in a circular list using only a single pointer to the circular list; separate pointers to the front and rear elements of the queue are not used. The linked list must be circular.      The insert and remove operations must both be O(1)    You...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Translate the following C code into M4K assembly language. You do not have to use the...
Translate the following C code into M4K assembly language. You do not have to use the frame pointer, just use $sp if you need to use the stack. You do not have to show the stack initialization nor stack cleanup. If you need a specific value for an address, just make an assumption. int A; main() { int B = 5; B = A+B }; // main //Disassembly starts here !main() { //stack and frame pointer init // you do...
C# language. Answer in a discussion format. What is a loop? When do you use a...
C# language. Answer in a discussion format. What is a loop? When do you use a loop versus a selection statement? What are some examples when an infinite loop is appropriate? Describe one of the following three types of loops supported in C#: while loop for loop do loop (or do-while loop) Describe the steps necessary to make a while loop end correctly: Explain the difference between incrementing and decrementing loop control variables. Explain the benefits of using both pretest...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT