#include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // Read b
ID: 3665472 • Letter: #
Question
#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. // 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 an integer, an integer will be input. // Forward Declarations void encryption(char[], int); void decryption(char[], int); void sortStrings(char[5][32]); void matrix(int[50][50]); void printMultiplicationTable(int[50][50], int); // Problem 1: (5 points) // In order to encrypt the input array of characters, we will shift those characters forward alphabetically by the integer value 'e' // If the input string is "hello" and e = 2, we will shift those characters forward alphabecially by 2 and the result will be "jgnnq" // Hint: Use strlen() to find the amount of characters in the array void encryption(char input[], int e) { } // Problem 2: (5 points) // In order to decrypt the input array of characters, we will shift those characters backwards alphabetically by the integer value 'd' // If the input string is "jgnnq" and d = 2, we will shift those characters back alphabecially by 2 and the result will be "hello" // Hint: this should be very similar to your encryption function void decryption(char input[], int d) { } // Problem 3: (5 points) // Swap the strings that are passed as parameters, this function will be used in Problem 4. // If string1 is "hello" and string2 is "goodbye", after calling this function, string1 will be "goodbye" and string2 will be "hello". void swapStrings(char string1[], char string2[]) { } // Problem 4: (20 points) // Use the selection sort algorithm to alphabetically sort the strings in the 2D character array "strings". // If you are unfamiliar with selection sort, here is a visual example: https://en.wikipedia.org/wiki/Selection_sort // NOTE: 'A' and 'a' should be considered alphabetically equivalent. // NOTE: You MUST use the selection sort algorithm for your program to produce the correct output. // NOTE: You MUST incorporate your "swapStrings" function to recieve full points for this part. // See output provided in word document for example input and output. void sortStrings(char strings[5][32]) { } // Problem 5: (10 points) // Using the 2D integer array "input", you are to fill the array with a multiplication table for the integer "num" // For example: if num = 2, your output 2D array should contain: // input[0][0] = 1 // input[0][1] = 2 // input[1][0] = 2 // input[1][1] = 4 // See output provided in word document for further clarification void multiplicationTable(int input[50][50], int num) { } // Problem 6: (5 points) // Print the 2D array in the following format: // 1 2 // 2 4 // Use ' ' to print a tab after each index and ' ' to print a newline after each line // Hint: this should be very similar to your multiplicationTable function // See ouptut provided in word document for further clarification void printMultiplicationTable(int input[50][50], int num) { } // 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() { int selection; // used for program selection char input[32]; // used for encryption int encrypt; // used for encryption char strings[5][32]; // used for sorting int num; // used for multiplication table int mt[50][50]; // used for multiplication table printf("Select one of the following: "); // prompt for program selection integer printf("1: Encryption "); printf("2. Sorting "); printf("3. Multiplication Table "); scanf("%d", &selection); // store program selection integer getchar(); // consume ' ' char; NOTE: If you are using GCC, you may need to comment out this line printf(" "); // newline switch (selection) { case 1: printf("Enter a string up to 20 characters long: "); // prompt for string fgets(input, sizeof(input), stdin); // store string input[strlen(input) - 1] = ''; // discard ' ' char; NOTE: If you are using GCC, you may need to comment out this line printf("Enter an Integer value for Encryption: "); // prompt for integer scanf("%d", &encrypt); // store integer encryption(input, encrypt); // encrypt string printf(" Encrypted String: %s ", input); // print encrypted string decryption(input, encrypt); // encrypt string printf("Decrypted String: %s ", input); // print decrypted string break; case 2: printf("Enter the first String: "); // prompt for string fgets(strings[0], sizeof(strings[0]), stdin); // store string printf("Enter the second String: "); fgets(strings[1], sizeof(strings[1]), stdin); printf("Enter the third String: "); fgets(strings[2], sizeof(strings[2]), stdin); printf("Enter the fourth String: "); fgets(strings[3], sizeof(strings[3]), stdin); printf("Enter the fifth String: "); fgets(strings[4], sizeof(strings[4]), stdin); sortStrings(strings); // call sorting function // print strings in sorted order printf(" Sorted Strings: %s%s%s%s%s", strings[0], strings[1], strings[2], strings[3], strings[4]); break; case 3: printf("Enter an integer 1-50 for a multiplication table: "); // prompt for integer scanf("%d", &num); // store integer multiplicationTable(mt, num); // create multiplication table printf(" "); // newline printMultiplicationTable(mt, num); // print multiplication table } }
Explanation / Answer
Answer:
While printing the multiplication table, the tab (" ") is placed after printing a number because, the space in the console is not showing the output in a formatted manner. So, if required the tab can be placed before printing the number.
Program code to copy:
#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.
// 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 an integer, an integer will be input.
// Forward Declarations
void encryption(char[], int);
void decryption(char[], int);
void sortStrings(char[5][32]);
void matrix(int[50][50]);
void printMultiplicationTable(int[50][50], int);
// Problem 1: (5 points)
// In order to encrypt the input array of characters, we will shift those characters forward alphabetically by the integer value 'e'
// If the input string is "hello" and e = 2, we will shift those characters forward alphabecially by 2 and the result will be "jgnnq"
// Hint: Use strlen() to find the amount of characters in the array
void encryption(char input[], int e)
{
int i = 0;
char val;
for(i=0;i<strlen(input);i++)
{
val = input[i]+e;
if(input[i]=='y')
val = val - 'y'- 2 + 'a';
else if(input[i]=='z')
val = val - 'z'- 1 + 'a';
input[i] = val;
}
}
// Problem 2: (5 points)
// In order to decrypt the input array of characters, we will shift those characters backwards alphabetically by the integer value 'd'
// If the input string is "jgnnq" and d = 2, we will shift those characters back alphabecially by 2 and the result will be "hello"
// Hint: this should be very similar to your encryption function
void decryption(char input[], int d)
{
int i = 0;
for(i=0;i<strlen(input);i++)
{
char val = input[i] - d;
if(input[i]<='b')
val = val - 'a' + 'z' + 1;
input[i] = val;
}
}
// Problem 3: (5 points)
// Swap the strings that are passed as parameters, this function will be used in Problem 4.
// If string1 is "hello" and string2 is "goodbye", after calling this function, string1 will be "goodbye" and string2 will be "hello".
void swapStrings(char string1[], char string2[])
{
char temp[20];
strcpy(temp, string1);
strcpy(string1, string2);
strcpy(string2, temp);
}
// Problem 4: (20 points)
// Use the selection sort algorithm to alphabetically sort the strings in the 2D character array "strings".
// If you are unfamiliar with selection sort, here is a visual example: https://en.wikipedia.org/wiki/Selection_sort
// NOTE: 'A' and 'a' should be considered alphabetically equivalent.
// NOTE: You MUST use the selection sort algorithm for your program to produce the correct output.
// NOTE: You MUST incorporate your "swapStrings" function to recieve full points for this part.
// See output provided in word document for example input and output.
void sortStrings(char strings[5][32])
{
int i = 0, j = 0,k=0, l=0;
int index = 0;
char newString[32], temp[32], temp1[32];
for (i = 0; i <4; ++i)
{
index = i;
strcpy(newString, strings[i]);
k=0;
while(newString[k] )
{
newString[k] = tolower(newString[k]);
k++;
}
for (j = i + 1; j < 5; ++j)
{
strcpy(temp1, strings[j]);
k=0;
while(temp1[k] )
{
temp1[k] = tolower(temp1[k]);
k++;
}
if (strcmp(newString, temp1) > 0)
{
strcpy(temp, strings[j]);
index = j;
}
}
swapStrings(strings[i], strings[index]);
}
}
// Problem 5: (10 points)
// Using the 2D integer array "input", you are to fill the array with a multiplication table for the integer "num"
// For example: if num = 2, your output 2D array should contain:
// input[0][0] = 1
// input[0][1] = 2
// input[1][0] = 2
// input[1][1] = 4
// See output provided in word document for further clarification
void multiplicationTable(int input[50][50], int num)
{
int i =0, j=0;
for(i =0;i<num; i++)
{
for(j = 0; j < num; j++)
{
input[i][j] = (i+1)*(j+1);
}
}
}
// Problem 6: (5 points)
// Print the 2D array in the following format:
// 1 2
// 2 4
// Use ' ' to print a tab after each index and ' ' to print a newline after each line
// Hint: this should be very similar to your multiplicationTable function
// See ouptut provided in word document for further clarification
void printMultiplicationTable(int input[50][50], int num)
{
int i =0, j=0;
for(i =0;i<num; i++)
{
for(j = 0; j < num; j++)
{
printf("%d ", input[i][j]);
}
printf(" ");
}
}
// 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()
{
int selection; // used for program selection
char input[32]; // used for encryption
int encrypt; // used for encryption
char strings[5][32]; // used for sorting
int num; // used for multiplication table
int mt[50][50]; // used for multiplication table
char s1[20] = "Hello", s2[20]="Goodbye";
printf("Select one of the following: "); // prompt for program selection integer
printf("1: Encryption ");
printf("2. Sorting ");
printf("3. Multiplication Table ");
scanf("%d", &selection); // store program selection integer
getchar(); // consume ' ' char; NOTE: If you are using GCC, you may need to comment out this line
printf(" "); // newline
switch (selection)
{
case 1:
printf("Enter a string up to 20 characters long: "); // prompt for string
fgets(input, sizeof(input), stdin); // store string
input[strlen(input) - 1] = ''; // discard ' ' char; NOTE: If you are using GCC, you may need to comment out this line
printf("Enter an Integer value for Encryption: "); // prompt for integer
scanf("%d", &encrypt); // store integer
encryption(input, encrypt); // encrypt string
printf(" Encrypted String: %s ", input); // print encrypted string
decryption(input, encrypt); // encrypt string
printf("Decrypted String: %s ", input); // print decrypted string
break;
case 2:
printf("Enter the first String: "); // prompt for string
fgets(strings[0], sizeof(strings[0]), stdin); // store string
printf("Enter the second String: ");
fgets(strings[1], sizeof(strings[1]), stdin);
printf("Enter the third String: ");
fgets(strings[2], sizeof(strings[2]), stdin);
printf("Enter the fourth String: ");
fgets(strings[3], sizeof(strings[3]), stdin);
printf("Enter the fifth String: ");
fgets(strings[4], sizeof(strings[4]), stdin);
sortStrings(strings); // call sorting function
// print strings in sorted order
printf(" Sorted Strings: %s%s%s%s%s", strings[0], strings[1], strings[2], strings[3], strings[4]);
break;
case 3:
printf("Enter an integer 1-50 for a multiplication table: "); // prompt for integer
scanf("%d", &num); // store integer
multiplicationTable(mt, num); // create multiplication table
printf(" "); // newline
printMultiplicationTable(mt, num); // print multiplication table
}
}
Sample output:
Sample run-1:
Select one of the following:
1: Encryption
2. Sorting
3. Multiplication Table
1
Enter a string up to 20 characters long: hello
Enter an Integer value for Encryption: 2
Encrypted String: jgnnq
Decrypted String: hello
*** Process returned 24 ***
Press any key to continue...
Sample Run – 2:
Select one of the following:
1: Encryption
2. Sorting
3. Multiplication Table
2
Enter the first String: Hello
Enter the second String: apple
Enter the third String: hello
Enter the fourth String: jack
Enter the fifth String: ben
Sorted Strings:
ben
apple
hello
Hello
jack
*** Process returned 44 ***
Press any key to continue...
Sample run – 3:
Select one of the following:
1: Encryption
2. Sorting
3. Multiplication Table
3
Enter an integer 1-50 for a multiplication table: 10
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
*** Process returned 1 ***
Press any key to continue...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.