Starting from:

$35

CSE030-Lab 4 Functions Solved

(Exercise) Create – combineString.cpp 
In this part of the lab, you will create a function (combineStr) that concatenates a string by a number of times.  There should be two functions in this program: main and combineStr. The descriptions of the functions are as follows.

The main function will

1.   Prompt the user to “Enter a string: ”.

2.   Prompt the user to “Enter a number of times: ”.

3.   Call the function combineStr.

4.   Output “The resulting string is: ”  and the resulting string.

5.   Start over again.  User can terminate the program by entering 0 (zero) for the number of times.

 

The combineStr function will

1.   Takes in a string and an integer as arguments.

2.   Concatenates the string argument by a number of times according to the integer argument.

3.   Return the resulting string.

 

Before starting to write your program, use a piece of paper or a text editor to write the pseudocode of BOTH functions.  You will need to submit the pseudocode in order to receive full credit.  Again, there is no unique way to write pseudocode. It will be good enough as long as you can understand it and translate it to C++ code.  Ask your TA if you are not sure about the structure of the pseudocode.  

 

Example runs (input is in italic and bold): 

Enter a string: 0 

Enter a number of times: 7 

The resulting string is:  0000000 

Enter a string: bla 

Enter a number of times: 3 

The resulting string is: blablabla 

Enter a string: cse30 

Enter a number of times: 0 

  

(Exercise) Create – sortArray1.cpp
In this part of the lab you will write a function (sortArr) that sorts an array of integers in either ascending or descending order.  This function:

•       Is a procedure with no return value.

•       Takes in a variable of your choice (bool or int) as an argument to indicate ascending or descending order of the sorting array.

•       Takes in an array of integers as an argument.

•       Takes in a number as argument indicating the size of the array.

•       Sorts the array in ascending/descending order.  

Note:  

•       A 1-dimensional array a is passed *by reference* with  a[]  in the argument list.

•       Reuse the selection sort (both ascending and descending versions) code that you developed in Lab 3 in this function.  

 

The main function can reuse the codes from Lab 3 to define and read the array.  

Specifically, you should ask the user to enter the size of the array, by outputting:

“Enter the size of the array: ” to the console.  If the user enters an incorrect size, you should output the error message

“ERROR: you entered an incorrect value for the array size!”

and exit the program.   If the input is a valid size for the array, ask the user to enter the data, by outputting

“Enter the numbers in the array, separated by a space, and press enter: ”

Let the user enter the numbers of the array and store them into a C++ array.  Up to now, this should be exactly the same code as Lab 2 and 3.  Then, you need to ask the user if the array should be sorted in ascending or descending order.  You may choose your criteria to input a selection:

“Sort in ascending (0) or descending (1) order? ”

Now, call sortArr to sort the array.  After the function finishes its operations, print to the console if the output is sorted in ascending or descending order (depending on what the user had input):

“This is the sorted array in <ascending or descending> order: ” and then output the array in a newline.   

Before starting to write your program, use a piece of paper or a text editor to write the pseudocode of the sortArr function only.  Remember to write the pseudocode for both ascending and descending versions.  You will need to submit the pseudocode in order to receive full credit.  Again, there is no unique way to write pseudocode. It will be good enough as long as you can understand it and translate it to C++ code.  Ask your TA if you are not sure about the structure of the pseudocode.  

Example runs (input is in italic and bold): 

Enter the size of the array: 5 

Enter the numbers in the array, separated by a space, and press enter: 4 6 8 2 5 

Sort in ascending (0) or descending (1) order? 1 

This is the sorted array in descending order: 8 6 5 4 2 

 

 

 

 

(Exercise) Create – sortArray2.cpp 
In this part of the lab, your program will run similarly to sortArray1.cpp. However, the sortArr function will use insertion sort algorithm instead of selection sort.  Here is the pseudocode of an insertion sort in ascending order, but you need to do both ascending and descending versions.

  

Before starting to write your program, use a piece of paper or a text editor to write the pseudocode of the sortArr function only.  Remember to write the pseudocode for both ascending and descending versions.  You will need to submit the pseudocode in order to receive full credit.  Again, there is no unique way to write pseudocode. It will be good enough as long as you can understand it and translate it to C++ code.  Ask your TA if you are not sure about the structure of the pseudocode.  

Example runs are the same as that of sortArray1.cpp 

More products