Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Language C++ : For this homework you will be investigating the use of arrays to store information. First create an array of integers. Create

Using Language C++ :

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']++; 

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

Students also viewed these Databases questions