+1 (315) 557-6473 

Process string in 2D array using C++ Assignment Solution


An array of Strings assignment

1. Write a C++ assignment that asks a user for a number between 1 and 3 to determine how many strings they will provide.

  • A value of 0 exits the program.
  • Validate the user’s input. Using a loop, continue to ask for input until a valid number is received.
  • Create an array of strings with a fixed size of 3 to store these strings.
  • The program then reads the specified number of strings and stores them into the array.
  • The program then loops through the array to print the strings in the following manner:
    a) First string: every second letter is printed after the first character

    b) Second string: every third letter is printed after the first character

    c) Third string: every fourth letter is printed after the first character

  • In addition, the capitalization must be flipped in each string.
  • Use functions to break down the solution into manageable subprograms!
  • Hint: use isupper() and islower() functions to check for capitalization.

Example 1:

How many strings will you want (one to three)? 5

Invalid entry.

How many strings will you want (one to three)? asdf

Invalid entry.

How many strings will you want (one to three)? 0

Goodbye!

Example 2:

How many strings will you want (one to three)? 3

String 1: Memorization

String 2: incomprehensibility

String 3: DISESTABLISHMENT

String 1: mMRZTO

String 2: IOREILY

String 3: dslm

2. Write a program that creates a 2D array of integers with a size of 10 x 10.

  • The program must then ask the user for a number between 3 and 10, inclusive, which will determine the size of an X to draw with 1s within the array.
  • An entry of 0 quits the program.
  • Validate the input the user provides; bad input should not be accepted. If the user provides bad data, a loop must be used to ask for the input again until it is correctly provided.

Examples of program executions are below. Red is user input. Blue is to help you visualize.

Example 1:

Strings 1

Example 2:

Strings 2

Example 3:

Strings 3

Example 4:

Strings 4

Solution:

Question 1:

#include < iostream> #include < string> #include < cstring> using namespace std; int getInput(char* message, int lb, int ub) { int option; string line; while (1) { cout << message; getline(cin, line); try { option = stoi(line); if (option >= lb && option <= ub) return option; else cout << "Invalid entry." << endl; } catch(std::invalid_argument e) { cout << "Invalid entry." << endl; } } } int main() { // array to store strings string strings[3]; // ask option char message[] = "How many strings will you want (one to three)? "; int n_strings = getInput(message, 0, 3); if (n_strings == 0) { cout << "Goodbye!" << endl; exit(0); } else { for (int i = 0; i < n_strings; i++) { cout << "String " << i + 1 << ": "; getline(cin, strings[i]); } } cout << endl; // now, process strings // first, for each one, switch capitalization for (int i = 0; i < 3; i++) { for (int j = 0; j < strings[i].length(); j++) { if (isupper(strings[i][j])) strings[i][j] = tolower(strings[i][j]); else strings[i][j] = toupper(strings[i][j]); } } char str[100]; int counter; for (int i = 0; i < n_strings; i++) { cout << "String " << (i + 1) << ": "; for (int j = 0; j < strings[i].length(); j++) { if (i == 0) // for first string, print only even indexes { if (j== 0 || j % 2 == 0) { cout << strings[i][j]; } } else if (i == 1) // second string { if (j == 0 || j % 3 == 0) { cout << strings[i][j]; } } else if (i == 2) // third { if (j == 0 || j % 4 == 0) { cout << strings[i][j]; } } } cout << endl; } }

Question 2:

#include < iostream> #include < string> using namespace std; int getInput(char* message, int lb, int ub) { int option; string line; while (1) { cout << message; getline(cin, line); try { option = stoi(line); if (option >= lb && option <= ub) return option; else cout << "Invalid entry." << endl; } catch (std::invalid_argument e) { cout << "Invalid entry." << endl; } } } void printMatrix(int array[10][10]) { // function to print the matrix for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { cout << "[" << array[i][j] << "]"; } cout << endl; } } int main() { // create array of fixed size 10x10 int array[10][10]; // initialize with zeros for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) array[i][j] = 0; } // ask size char message[] = "Please provide a size (3 to 10): "; int size = getInput(message, 0, 10); if (size == 0) { cout << "Goodbye." << endl; exit(0); } else { // fill the X with ones // fill diagonals int row = 0; int col = size - 1; for (int i = 0; i < size; i++) { array[i][i] = 1; array[row][col] = 1; row++; col--; } // now print printMatrix(array); } }