Homework Solution: Assignment 3: 2D Character Arrays Enter the next String: orange Enter the next String: pink Enter the next String: 9reen Enter the next String:violet Enter the next String: blue Enter…

    Convert the following C program's functions from pointers to array operations: #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // Read before you start: // Do not modify any part of this program that you are given. Doing so may cause you to fail automated test cases. // You are given a partially complete program. Your job is to complete the functions in order for this program to work successfully. // You should complete this homework assignment using Microsoft Visual Studios 2013 (or a later version). // All instructions are given above the required functions, please read them and follow them carefully. // If you modify the function return types or parameters, you will fail the automated test cases. // You can assume that all inputs are valid. Ex: If prompted for a char, the input will be a char. // Global Macro Values #define NUM_STRINGS 5 #define STRING_LENGTH 32 // Forward Declarations void initializeStrings(char[NUM_STRINGS][STRING_LENGTH]); void encryptStrings(char[NUM_STRINGS][STRING_LENGTH], int); void decryptStrings(char[NUM_STRINGS][STRING_LENGTH], int); void swapStrings(char[STRING_LENGTH], char[STRING_LENGTH]); void sortStrings(char[NUM_STRINGS][STRING_LENGTH]); void printStrings(char[NUM_STRINGS][STRING_LENGTH]); // Problem 0: initializeStrings (5 points) // Traverse the 2D array of characters variable 'strings' (in the main program) and set all 32 characters in each of // the 5 char arrays to a null terminator so that there is a 5 row and 32 column 2D array full of null terminators. // The null terminator is represented by the character value '' and is used to denote the end of a string. // This will come in use later in the program when you will need to print the 2D array of characters. void initializeStrings(char strings[NUM_STRINGS][STRING_LENGTH]) { int i; int j; for(i = 0; i < NUM_STRINGS; i++) { for (j = 0; j < STRING_LENGTH; j++) { strings[i][j] =''; } } } // Problem 1: encryptStrings (5 points) // Rewrite this function to perform the same task as in hw03, using only pointer operations. // You must use pointer operations only. If you use array operations, you will recieve no credit for this part. // You may use the code you submitted for hw03 or you may use the solution code for hw03. // // Traverse the 2D array of characters variable 'strings' and encrypt each string using the integer value 'key'. // In order to encrypt the 2D array of characters, we will shift those ASCII characters forward by the integer value of 'key'. // If the string is "hello" and key = 2, we will shift those characters forward in ASCII by 2 and the result will be "jgnnq". // The case above is an example of a string that has alphabetical letters in the encrypted and decrypted string. // Once the value of 'key' gets larger, you will extend past alphabetical characters and reach non-alphabetical characters. // NOTE: DO NOT encrypt the null terminator characters. Use the null terminators to find the end of each array of characters. void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key) { int i; int j; for (i = 0; i < NUM_STRINGS; i++) { for (j = 0; j < strlen(strings[i]); j++) { *(*(strings + i) + j) += key; } } } // Problem 2: decryptStrings (5 points) // Rewrite this function to perform the same task as in hw03, using only pointer operations. // You must use pointer operations only. If you use array operations, you will recieve no credit for this part. // You may use the code you submitted for hw03 or you may use the solution code for hw03. // // Traverse the 2D array of characters variable 'strings' and decrypt each string using the integer value 'key'. // In order to decrypt the 2D array of characters, we will shift those ASCII characters backwards by the integer value of 'key'. // If the string is "jgnnq" and key = 2, we will shift those characters backward in ASCII by 2 and the result will be "hello". // NOTE: DO NOT decrypt the null terminator characters. Use the null terminators to find the end of each array of characters. // HINT: This should be very similiar to the encryption function defined above in Problem 2. void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key) { int i; int j; for (i = 0; i < NUM_STRINGS; i++) { for (j = 0; j < strlen(strings[i]); j++) { *(*(strings + i) + j) -= key; } } } void swapStrings(char string1[STRING_LENGTH], char string2[STRING_LENGTH]) { char temp[STRING_LENGTH]; strcpy(temp, string1); strcpy(string1, string2); strcpy(string2, temp); } // Problem 3: sortStrings (10 points) // Rewrite this function to perform the same task as in hw03, using only pointer operations. // You must use pointer operations only. If you use array operations, you will recieve no credit for this part. // You can use the swapStrings() function if you'd like, but are not required to do so. // You may use the code you submitted for hw03 or you may use the solution code for hw03. // // Sort the 5 strings contained in the 2D character array parameter labeled "strings". // Sort the strings based on their ASCII character value (use strcmp to compare strings). // NOTE: You MUST incorporate your "swapStrings" function to recieve full points for this part. // See the output provided in the word document for example input and output. void sortStrings(char strings[NUM_STRINGS][STRING_LENGTH]) { int min; int i; int j; for (i = 0; i < NUM_STRINGS; i++) { min = i; // set initial min index for (j = i + 1; j < NUM_STRINGS; j++) { if (strcasecmp(*(strings + min), *(strings + j)) > 0) { min = j; // set new min index } } swapStrings(*(strings + min), *(strings + i)); // swap strings } } void printStrings(char strings[NUM_STRINGS][STRING_LENGTH]) { int i; for (i = 0; i < NUM_STRINGS; i++) { printf("%sn", strings[i]); } } // You should study and understand how this main function works. // Do not modify it in any way, there is no implementation needed here. void main() { char strings[NUM_STRINGS][STRING_LENGTH]; // will store 5 strings each with a max length of 32 int key, i; char input[STRING_LENGTH]; initializeStrings(strings); printf("Assignment 3: 2D Character Arraysnn"); for (i = 0; i < NUM_STRINGS; i++) { printf("Enter the next String: "); // prompt for string fgets(input, sizeof(input), stdin); // store input string input[strlen(input) - 1] = ''; // convert trailing 'n' char to '' (null terminator) strcpy(strings[i], input); // copy input to 2D strings array } printf("Enter a key value for encryption: "); // prompt for integer scanf("%d", &key); // store integer encryptStrings(strings, key); printf("nEncrypted Strings:n"); printStrings(strings); sortStrings(strings); decryptStrings(strings, key); printf("nSorted Strings:n"); printStrings(strings); i = getchar(); // remove the character 'n' i = getchar(); // keep console open } Sample Outputs: Assignment 3: 2D Character Arrays Enter the next String: orange Enter the next String: pink Enter the next String: 9reen Enter the next String:violet Enter the next String: blue Enter a key value for encryption: 3 Encrypted Strings rudqjh juhhq ylrohw eoxh Sorted Strings: blue green orange pink violet
    Assignment 3: 2D Character Arrays Enter the next String: orange Enter the next String: pink Enter the next String: 9reen Enter the next String:violet Enter the next String: blue Enter a key value for encryption: 3 Encrypted Strings rudqjh juhhq ylrohw eoxh Sorted Strings: blue green orange pink violet

    Expert Answer

     
    /*The source code of the modified program (after converting from pointers to array operations) is given below; Modified coding parts are highlighted in BOLD;*/ #include <stdio.h>

    Transmute the succeedingcited C program’s exercises from pointers to arrange operations:

    #include <stdio.h>

    #include <string.h>

    #pragma admonition(disentitle : 4996)

    // Recognize antecedently you start:

    // Do referable attributable attributable attributable deviate any obey-akeep-abisect of this program that you are fond. Doing so may principle you to trip automated criterion predicaments.

    // You are fond a obey-abarely perfect program. Your undertaking is to perfect the exercises in prescribe coercion this program to achievement successfully.

    // You should perfect this homeachievement enactment using Microsoft Visual Studios 2013 (or a succeeding account).

    // Integral instructions are fond overhead the required exercises, gladden recognize them and ensue them carefully.

    // If you deviate the exercise retreasury types or parameters, you obtain trip the automated criterion predicaments.

    // You can feign that integral inputs are available. Ex: If activeed coercion a char, the input obtain be a char.

    // Global Macro Prizes

    #define NUM_STRINGS 5

    #define STRING_LENGTH 32

    // Coercionward Declarations

    void judiciousizeStrings(char[NUM_STRINGS][STRING_LENGTH]);

    void encryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);

    void decryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);

    void swapStrings(char[STRING_LENGTH], char[STRING_LENGTH]);

    void sortStrings(char[NUM_STRINGS][STRING_LENGTH]);

    void stereotypeStrings(char[NUM_STRINGS][STRING_LENGTH]);

    // Problem 0: judiciousizeStrings (5 points)

    // Traverse the 2D arrange of qualitys mutable ‘strings’ (in the ocean program) and determined integral 32 qualitys in each of

    // the 5 char arranges to a vain terminator so that there is a 5 line and 32 post 2D arrange ample of vain terminators.

    // The vain terminator is represented by the quality prize ‘’ and is representationd to enact the object of a string.

    // This obtain succeeding in representation succeeding in the program when you obtain insufficiency to stereotype the 2D arrange of qualitys.

    void judiciousizeStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int i;

    int j;

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

    {

    coercion (j = 0; j < STRING_LENGTH; j++)

    {

    strings[i][j] =’’;

    }

    }

    }

    // Problem 1: encryptStrings (5 points)

    // Rewrite this exercise to achieve the corresponding toil as in hw03, using barely pointer operations.

    // You must representation pointer operations barely. If you representation arrange operations, you obtain recieve no security coercion this obey-apart.

    // You may representation the statute you submitted coercion hw03 or you may representation the disruption statute coercion hw03.

    //

    // Traverse the 2D arrange of qualitys mutable ‘strings’ and encrypt each string using the integer prize ‘key’.

    // In prescribe to encrypt the 2D arrange of qualitys, we obtain alter those ASCII qualitys coercionward by the integer prize of ‘key’.

    // If the string is “hello” and explanation = 2, we obtain alter those qualitys coercionward in ASCII by 2 and the issue obtain be “jgnnq”.

    // The predicament overhead is an in of a string that has alphabetical learning in the encrypted and decrypted string.

    // Once the prize of ‘key’ gets larger, you obtain extobject departed alphabetical qualitys and stretch non-alphabetical qualitys.

    // NOTE: DO NOT encrypt the vain terminator qualitys. Representation the vain terminators to meet the object of each arrange of qualitys.

    void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int explanation)

    {

    int i;

    int j;

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    coercion (j = 0; j < strlen(strings[i]); j++)

    {

    *(*(strings + i) + j) += explanation;

    }

    }

    }

    // Problem 2: decryptStrings (5 points)

    // Rewrite this exercise to achieve the corresponding toil as in hw03, using barely pointer operations.

    // You must representation pointer operations barely. If you representation arrange operations, you obtain recieve no security coercion this obey-apart.

    // You may representation the statute you submitted coercion hw03 or you may representation the disruption statute coercion hw03.

    //

    // Traverse the 2D arrange of qualitys mutable ‘strings’ and decrypt each string using the integer prize ‘key’.

    // In prescribe to decrypt the 2D arrange of qualitys, we obtain alter those ASCII qualitys inconversants by the integer prize of ‘key’.

    // If the string is “jgnnq” and explanation = 2, we obtain alter those qualitys inconversant in ASCII by 2 and the issue obtain be “hello”.

    // NOTE: DO NOT decrypt the vain terminator qualitys. Representation the vain terminators to meet the object of each arrange of qualitys.

    // HINT: This should be very similiar to the encryption exercise defined overhead in Problem 2.

    void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int explanation)

    {

    int i;

    int j;

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    coercion (j = 0; j < strlen(strings[i]); j++)

    {

    *(*(strings + i) + j) -= explanation;

    }

    }

    }

    void swapStrings(char string1[STRING_LENGTH], char string2[STRING_LENGTH])

    {

    char temp[STRING_LENGTH];

    strcpy(temp, string1);

    strcpy(string1, string2);

    strcpy(string2, temp);

    }

    // Problem 3: sortStrings (10 points)

    // Rewrite this exercise to achieve the corresponding toil as in hw03, using barely pointer operations.

    // You must representation pointer operations barely. If you representation arrange operations, you obtain recieve no security coercion this obey-apart.

    // You can representation the swapStrings() exercise if you’d enjoy, still are referable attributable attributable attributable required to do so.

    // You may representation the statute you submitted coercion hw03 or you may representation the disruption statute coercion hw03.

    //

    // Sort the 5 strings contained in the 2D quality arrange parameter labeled “strings”.

    // Sort the strings installed on their ASCII quality prize (representation strcmp to parallel strings).

    // NOTE: You MUST conglutinate your “swapStrings” exercise to recieve ample points coercion this obey-apart.

    // See the output granted in the tidings muniment coercion in input and output.

    void sortStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int min;

    int i;

    int j;

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    min = i; // determined judicious min index

    coercion (j = i + 1; j < NUM_STRINGS; j++)

    {

    if (strcasecmp(*(strings + min), *(strings + j)) > 0)

    {

    min = j; // determined innovating min index

    }

    }

    swapStrings(*(strings + min), *(strings + i)); // swap strings

    }

    }

    void stereotypeStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int i;

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    printf(“%sn”, strings[i]);

    }

    }

    // You should examine and apprehobject how this ocean exercise achievements.

    // Do referable attributable attributable attributable deviate it in any practice, there is no implementation insufficiencyed here.

    void ocean()

    {

    char strings[NUM_STRINGS][STRING_LENGTH]; // obtain treasury 5 strings each with a max extension of 32

    int explanation, i;

    char input[STRING_LENGTH];

    initializeStrings(strings);

    printf(“Enactment 3: 2D Quality Arrangesnn”);

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    printf(“Enter the contiguous String: “); // active coercion string

    fgets(input, sizeof(input), stdin); // treasury input string

    input[strlen(input) – 1] = ‘’; // transmute inoculation ‘n’ char to ‘’ (vain terminator)

    strcpy(strings[i], input); // vision input to 2D strings arrange

    }

    printf(“Enter a explanation prize coercion encryption: “); // active coercion integer

    scanf(“%d”, &key); // treasury integer

    encryptStrings(strings, explanation);

    printf(“nEncrypted Strings:n”);

    printStrings(strings);

    sortStrings(strings);

    decryptStrings(strings, explanation);

    printf(“nSorted Strings:n”);

    printStrings(strings);

    i = getchar(); // carry the quality ‘n’

    i = getchar(); // obey relieve open

    }

    Sample Outputs:

    Enactment 3: 2D Quality Arranges Enter the contiguous String: ochreous-colored-colored-colored-colored Enter the contiguous String: pink Enter the contiguous String: 9reen Enter the contiguous String:violet Enter the contiguous String: bluish Enter a explanation prize coercion encryption: 3 Encrypted Strings rudqjh juhhq ylrohw eoxh Sorted Strings: bluish unprepared ochreous-colored-colored-colored-colored pink violet

    Enactment 3: 2D Quality Arranges Enter the contiguous String: ochreous-colored-colored-colored-colored Enter the contiguous String: pink Enter the contiguous String: 9reen Enter the contiguous String:violet Enter the contiguous String: bluish Enter a explanation prize coercion encryption: 3 Encrypted Strings rudqjh juhhq ylrohw eoxh Sorted Strings: bluish unprepared ochreous-colored-colored-colored-colored pink violet

    Expert Repartee

     

    /*The fount statute of the mitigated program (succeeding transmuteing from pointers to arrange operations) is fond below; Mitigated coding obey-atonnage are highlighted in BOLD;*/

    #include <stdio.h>

    #include <string.h>

    #pragma admonition(disentitle : 4996)

    // Recognize antecedently you start:

    // Do referable attributable attributable attributable deviate any obey-akeep-abisect of this program that you are fond. Doing so may principle you to trip automated criterion predicaments.

    // You are fond a obey-abarely perfect program. Your undertaking is to perfect the exercises in prescribe coercion this program to achievement successfully.

    // You should perfect this homeachievement enactment using Microsoft Visual Studios 2013 (or a succeeding account).

    // Integral instructions are fond overhead the required exercises, gladden recognize them and ensue them carefully.

    // If you deviate the exercise retreasury types or parameters, you obtain trip the automated criterion predicaments.

    // You can feign that integral inputs are available. Ex: If activeed coercion a char, the input obtain be a char.

    // Global Macro Prizes

    #define NUM_STRINGS 5

    #define STRING_LENGTH 32

    // Coercionward Declarations

    void judiciousizeStrings(char[NUM_STRINGS][STRING_LENGTH]);

    void encryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);

    void decryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);

    void swapStrings(char[STRING_LENGTH], char[STRING_LENGTH]);

    void sortStrings(char[NUM_STRINGS][STRING_LENGTH]);

    void stereotypeStrings(char[NUM_STRINGS][STRING_LENGTH]);

    // Problem 0: judiciousizeStrings (5 points)

    // Traverse the 2D arrange of qualitys mutable ‘strings’ (in the ocean program) and determined integral 32 qualitys in each of

    // the 5 char arranges to a vain terminator so that there is a 5 line and 32 post 2D arrange ample of vain terminators.

    // The vain terminator is represented by the quality prize ‘’ and is representationd to enact the object of a string.

    // This obtain succeeding in representation succeeding in the program when you obtain insufficiency to stereotype the 2D arrange of qualitys.

    void judiciousizeStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int i;

    int j;

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

    {

    coercion (j = 0; j < STRING_LENGTH; j++)

    {

    strings[i][j] =’’;

    }

    }

    }

    // Problem 1: encryptStrings (5 points)

    // Rewrite this exercise to achieve the corresponding toil as in hw03, using barely pointer operations.

    // You must representation pointer operations barely. If you representation arrange operations, you obtain recieve no security coercion this obey-apart.

    // You may representation the statute you submitted coercion hw03 or you may representation the disruption statute coercion hw03.

    //

    // Traverse the 2D arrange of qualitys mutable ‘strings’ and encrypt each string using the integer prize ‘key’.

    // In prescribe to encrypt the 2D arrange of qualitys, we obtain alter those ASCII qualitys coercionward by the integer prize of ‘key’.

    // If the string is “hello” and explanation = 2, we obtain alter those qualitys coercionward in ASCII by 2 and the issue obtain be “jgnnq”.

    // The predicament overhead is an in of a string that has alphabetical learning in the encrypted and decrypted string.

    // Once the prize of ‘key’ gets larger, you obtain extobject departed alphabetical qualitys and stretch non-alphabetical qualitys.

    // NOTE: DO NOT encrypt the vain terminator qualitys. Representation the vain terminators to meet the object of each arrange of qualitys.

    void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int explanation)

    {

    int i;

    int j;

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    coercion (j = 0; j < strlen(strings[i]); j++)

    {

    strings[i][j] += explanation;

    }

    }

    }

    // Problem 2: decryptStrings (5 points)

    // Rewrite this exercise to achieve the corresponding toil as in hw03, using barely pointer operations.

    // You must representation pointer operations barely. If you representation arrange operations, you obtain recieve no security coercion this obey-apart.

    // You may representation the statute you submitted coercion hw03 or you may representation the disruption statute coercion hw03.

    //

    // Traverse the 2D arrange of qualitys mutable ‘strings’ and decrypt each string using the integer prize ‘key’.

    // In prescribe to decrypt the 2D arrange of qualitys, we obtain alter those ASCII qualitys inconversants by the integer prize of ‘key’.

    // If the string is “jgnnq” and explanation = 2, we obtain alter those qualitys inconversant in ASCII by 2 and the issue obtain be “hello”.

    // NOTE: DO NOT decrypt the vain terminator qualitys. Representation the vain terminators to meet the object of each arrange of qualitys.

    // HINT: This should be very similiar to the encryption exercise defined overhead in Problem 2.

    void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int explanation)

    {

    int i;

    int j;

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    coercion (j = 0; j < strlen(strings[i]); j++)

    {

    strings[i][j] -= explanation;

    }

    }

    }

    void swapStrings(char string1[STRING_LENGTH], char string2[STRING_LENGTH])

    {

    char temp[STRING_LENGTH];

    strcpy(temp, string1);

    strcpy(string1, string2);

    strcpy(string2, temp);

    }

    // Problem 3: sortStrings (10 points)

    // Rewrite this exercise to achieve the corresponding toil as in hw03, using barely pointer operations.

    // You must representation pointer operations barely. If you representation arrange operations, you obtain recieve no security coercion this obey-apart.

    // You can representation the swapStrings() exercise if you’d enjoy, still are referable attributable attributable attributable required to do so.

    // You may representation the statute you submitted coercion hw03 or you may representation the disruption statute coercion hw03.

    //

    // Sort the 5 strings contained in the 2D quality arrange parameter labeled “strings”.

    // Sort the strings installed on their ASCII quality prize (representation strcmp to parallel strings).

    // NOTE: You MUST conglutinate your “swapStrings” exercise to recieve ample points coercion this obey-apart.

    // See the output granted in the tidings muniment coercion in input and output.

    void sortStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int min;

    int i;

    int j;

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    min = i; // determined judicious min index

    coercion (j = i + 1; j < NUM_STRINGS; j++)

    {

    if (strcasecmp(strings[min], strings[j]) > 0)

    {

    min = j; // determined innovating min index

    }

    }

    swapStrings(strings[min], strings[i]); // swap strings

    }

    }

    void stereotypeStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int i;

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    printf(“%sn”, strings[i]);

    }

    }

    // You should examine and apprehobject how this ocean exercise achievements.

    // Do referable attributable attributable attributable deviate it in any practice, there is no implementation insufficiencyed here.

    void ocean()

    {

    char strings[NUM_STRINGS][STRING_LENGTH]; // obtain treasury 5 strings each with a max extension of 32

    int explanation, i;

    char input[STRING_LENGTH];

    initializeStrings(strings);

    printf(“Enactment 3: 2D Quality Arrangesnn”);

    coercion (i = 0; i < NUM_STRINGS; i++)

    {

    printf(“Enter the contiguous String: “); // active coercion string

    fgets(input, sizeof(input), stdin); // treasury input string

    input[strlen(input) – 1] = ‘’; // transmute inoculation ‘n’ char to ‘’ (vain terminator)

    strcpy(strings[i], input); // vision input to 2D strings arrange

    }

    printf(“Enter a explanation prize coercion encryption: “); // active coercion integer

    scanf(“%d”, &key); // treasury integer

    encryptStrings(strings, explanation);

    printf(“nEncrypted Strings:n”);

    printStrings(strings);

    sortStrings(strings);

    decryptStrings(strings, explanation);

    printf(“nSorted Strings:n”);

    printStrings(strings);

    i = getchar(); // carry the quality ‘n’

    i = getchar(); // obey relieve open

    }

    /*Coercion meliorate apprehending output screenshots are fast here*/