Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using C++ HELP: Code will not start Part 2. I have tried both parts separately but need them on the same main.cpp file since they

Using C++

HELP: Code will not start Part 2. I have tried both parts separately but need them on the same main.cpp file since they are both related to eachother. And when running the code, it will execute Part 2 without allowing me to input "Enter a string: ";

Below is my code listed. please help on how to fix this.

#include  #include  #include  using namespace std; int main() { // Part 1: Array of integers int n; //initialize n as an integer cout << "Enter the number of integers: "; cin >> n; //read in int n from user int arr[n]; //create n as an array cout << "Enter " << n << " integers:" << endl; for (int i = 0; i < n; i++) // loop through the array { cin >> arr[i]; } //n elements in the array and have boolean variable named didSwap bool didSwap = true; for (int i = 0; i < n - 1 && didSwap; i++) { didSwap = false; for (int j = n - 1; j > i; j--) { if (arr[j] < arr[j - 1]) { swap(arr[j], arr[j - 1]); didSwap = true; } } } cout << "Sorted array:" << endl; //print out sorted array of numbers for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; // Code will stop here and wont allow me to continue. How to fix this? // Part 2: Counting letters int letterArray[26] = {0}; //declare and array of 26 elements, initialize to 0 string inputString; //create a string variable cout << "Enter a string: "; getline(cin, inputString); //inputString will allow to input an entire string for (int i = 0; i < inputString.length(); i++) //for loop to loop thru every element { if (isalpha(inputString[i])) { letterArray[toupper(inputString[i]) - 'A']++; //increment the cout for letter } } cout << " A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" << endl; for (int i = 0; i < 26; i++) //once all characters have been counted, cout { cout << " " << letterArray[i]; } cout << endl; return 0; }

Assignment requirements are listed below:

For this homework you will be investigating the use of arrays to store information.

First create an array of integers. Create a loop to read in integers (first ask how many, then read them in). Then we will sort the elements of the array using a bubble sort (inefficient but effective).

If there are n elements in the array and you have a boolean variable named didSwap, the algorithm for Bubble sort is:

 for i = 0 to n-2 didSwap = false for j= n-1 downto i+1 if A[j] < A[j-1] swap A[j] and A[j-1] didSwap = true if didSwap == false break 

NOTE: We can add the check for didSwap == false to the for loop

 didSwap = true; for(i=0; i  

The initialization, stop condition, and increment parts of a for loop can contain multiple expressions.

Once we have sorted the integers, print out the sorted array of numbers.

For the second part we will use characters to index an array. First declare an array of 26 elements, initialize each element to zero.

Then ask for an input string. Use a "get line" type of input function to put an entire line into a string variable. Loop through the input string counting the number of each letter. NOTE: Make sure you convert all the characters to uppercase.

For example, suppose you have declared your array to be letterArray and the input string is called inputString. We can write a for loop to loop through every element of input string and count it if it is a letter.

Common functions/methods to use:

length() isalpha() toupper()

If we are on input character i we can increment the count for that letter using:

 if (isalpha(inputString[i]) letterArray[toupper(inputString[i]) - 'A']++;

The method used will vary depending on whether you are programming in Java or C++ (or C).

Once all the alphabetic characters have been counted print out your count, for example:

Please enter your string: Bob is a palindrome

 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 2 2 0 1 1 0 0 0 2 0 0 1 1 1 2 1 0 1 1 0 0 0 0 0 0 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions

Question

c. What were you expected to do when you grew up?

Answered: 1 week ago

Question

4. Describe how cultural values influence communication.

Answered: 1 week ago

Question

3. Identify and describe nine cultural value orientations.

Answered: 1 week ago