Computer Science

COP 3014: Fall 2018 Homework 4

Total Points: 200 Due: Friday, 11/16/2018

The objective for this assignment is to make sure

  • You understand and can work with C++ arrays.
  • You are comfortable with writing functions in C++.
  • You are familiar with repetitive structures (loops) and selection statements (if/else or switch) in any combination and can use them in functions and to manipulate array data.
  • You can approach a complex problem, break it down into various parts, and put together a solution.
    From this assignment, please make sure you conform to Output requirements. You should now be familiar enough with cout statements and output formatting that you should be able to EXACTLY match the sample output (other than certain exceptions you will be informed about beforehand).
    This assignment requires you to several files on Canvas. Please do so in a SINGLE submission. Canvas allows you to turn in multiple flies in one submission. Once you have uploaded your first file, click “attach another file” to upload your second file and so on.
    Turn in your 3 files functions.cpp, arrays.cpp and matrix.cpp, (and the extra credit programevaluate.cpp, if you are attempting it) to Canvas.

2 Program 1 – Writing Functions – 75 points

Squilliam Fancyson has heard of the fancy calculator that Squidward (supposedly) made for the Krusty Krab’s Cash Register. Determined to prove that he is the best programmer in town, he has challenged Squidward to a series of programming challenges. If Squilliam wins, Squidward has to hand over his clarinet and never paint again. If Squidward wins, Squilliam will work Squidward’s shift at the Krusty Krab’s cash register for a week. Squidward is determined to win, except he is not really a programmer. He has left no stone (pioneer transport or otherwise) unturned. He has even tried asking Karen, who was unable to help. He realizes that instead of working off Mr. Krabs’ old computer (the one Patrick smashed while painting the house), he could just pay you to do the challenges for him. Your task today is to help Squidward retain his sanity and dignity (whatever is left of it).

The first challenge is to write a few mathematical functions. For this program, we define a new term called Special Difference. The Special Difference of a number is the difference between the number and the one you obtain by reversing its digits. For example, the Special Difference of 1234 is 3087 (| 1234 – 4321 |).

1

We also find the value of ex using the Taylor’s series expansion. The Taylor’s Series expansion of ex is defined as

e =
Please make sure your program conforms to the following requirements.

number. (20 points)

  1. Write a function called difference that accepts a number as a parameter, calculates the special difference of the number and returns it. (15 points)
  2. Write a function called factorial that returns the factorial of a number. You can use the code from the class examples.
  3. Write a function called sum that takes a number ’x’ and a number ’n’ as parameters and calculates ex accurate to the ’nth’ term. You cannot just raise e to x. You have to use the expansion to calculate the value. (20 points)
  4. You can use the pow function to calculate xn.
  5. In the main function, accept a series of numbers from the user. Stop if the number entered is negative. Use the difference method to find the sum of the special differences and print it. (10 points)
  6. Next, still in the main function, accept the values of ’x’ and ’n’ from the user and use the sumfunction to calculate the required value and print it. (5 points)
  7. Make sure you add comments to explain your logic. (5 points)

2.1 Sample Run

Calculating the Special Differences:
Enter the numbers:
1234
57
982
603
-2
The Sum is 4095
Calculating e^x through Taylor’s Series Expansion:
Enter the value of x: 5
Enter the number of terms: 20
e^x is 148.413

3 Program 2 – Array Operations – 75 Points

Squilliam has been soundly beaten in the first challenge. He still refuses to give in. It is now a best of 3 challenge. The next item on the agenda is array operations. For this program, we’re going to perform a range of standard array operations to show Squilliam once and for all, that Squidward is familiar with arrays and how they work. You’re required to write functions to insert and delete

inf
x ????xn x0 x1 x2 x3

n!=0!+1!+2!+3!…
1. Write a function called reverse that takes a number as a parameter and returns the reversed

page2image3672928page2image3678128page2image3671888page2image3673344page2image3673552

n=0

2

elements from an array, and sort the array. We’re also going to write a command line menu to do these tasks repeatedly. For this problem, you can assume that the array size will never drop below 1 and will never rise above 99.

Please make sure you conform to the following requirements:

  1. Create a global constant integer called CAPACITY and set it to 100 (5 points)
  2. Use the class examples and exercises to write functions to initialize the array by reading values from the user, and to print the array. These functions take the array and its current size as parameters. You can use the code in the examples. (5 points)
  3. Write a function called insert that takes the array, a number, a position and the current size of the array as parameters. Insert the number at the given position. (15 points)
  4. Write a function called remove that takes the array, a number and the current size of the array, and removes the first instance of the number from the array. (15 points)
  5. Write a function called sort that takes the array and its current size as parameters and sorts the array in ascending order. You need to use insertion sort, which is given below. (15 points)
  6. In the main function, have the user initialize the array. Then, write a command line menu with the following options:
    • Print
    • Insert
    • Remove• Sort
    • Exit
    Call the appropriate functions when the user chooses a particular option. Print and error message and continue if the user enters a wrong option. (15 points)
  7. Please make sure your code is appropriately commented. (5 points)

Algorithm for insertion sort

loop i from 1 to Length(A)
        x = A[i]

j=i-1
loop as long as j >= 0 and A[j] > x

A[j+1] = A[j]

j=j-1 end loop

       A[j+1] = x
end loop

3.1 Sample Runs

Enter the number of elements you want to enter (Max 100): 5
Enter 5 numbers
2.5 6 -7 10.7 0.8
1. Insert an element
2. Remove an element
3. Print the array

3

4. Sort the array
5. Exit
Enter your option: 1
Enter the number: 3.1
Enter the position: 2
Element Inserted.
1. Insert an element
2. Remove an element
3. Print the array
4. Sort the array
5. Exit
Enter your option: 3
The array is:
2.5 6 3.1 -7 10.7 0.8
1. Insert an element
2. Remove an element
3. Print the array
4. Sort the array
5. Exit
Enter your option: 4
1. Insert an element
2. Remove an element
3. Print the array
4. Sort the array
5. Exit
Enter your option: 3
The array is:
-7 0.8 2.5 3.1 6 10.7
1. Insert an element
2. Remove an element
3. Print the array
4. Sort the array
5. Exit
Enter your option: 2
Enter the element: 0.8
Element deleted.
1. Insert an element
2. Remove an element
3. Print the array
4. Sort the array
5. Exit
Enter your option: 3
The array is:
-7 2.5 3.1 6 10.7

4

1. Insert an element
2. Remove an element
3. Print the array
4. Sort the array
5. Exit
Enter your option: 5

Goodbye!

4 Program 3 – Matrix Norms – 50 points

The competition is heating up. The special judging panel, consisting of Karen, Old Man Jenkins and Sandy, do not want to release the results of the previous challenge until this one is complete as well. For this challenge, you have to compute the Max Norm of a matirx. The Max norm of a ma- trix is defined as the largest elements of a matrix. Write a C++ program to compute the Max norm.

Please make sure you conform to the following requirements:

  1. Declare global constant integers for ROWCAP and COLCAP. Set both to 100 (5 points)
  2. Use the class examples and exercises to write functions to initialize the matrix by reading values from the user, and to print the matrix. These functions take the matrix and its current number of rows and columns as parameters. You can use the code in the examples. (5 points)
  3. Write a function called findnorm that accepts the matrix, its current number of rows and columns and return its max norm. (20 points)
  4. In the main function, initialize the matrix, read in its values and compute and print its norm. (15 points)
  5. Please make sure your code is commented (5 points).

4.1 Sample Run

Enter the number of rows: 3 Enter the number of columns: 3 Enter the matrix:
-3 5 7
2 16 4
028

The max Norm is: 16

5 Extra Credit Problem – Simple Calculator – 50 points

In the distant and slightly dystopian future, the Grand Poobah has banned the use of hand held calculators. Some say he was hit on the head with one. Others believe it is due to his fear of accountants. Either way, hand calculators are a thing of the past. However, you find yourself in sudden need of a calculator that can perform simple calculations. No worries. You are a C++ programmer and you can write a console application to do this.

This program does not serve any practical purpose and is, in fact, a very inefficient way to de- sign a calculator. This exercise is just to give you some practice in writing functions.

5

Specifications

  • This program makes use of functions. The main function should only contain one effective line (that is not the usual program stub), which is a call to the evaluate function. (5 points).
  • Write 5 functions to perform the 5 basic arithmetic operations: +, -, *, / and %. Give them some sensible name (for example, the function that performs addition can be called add). Each of the functions will take 2 integer arguments and return an integer value. (25 points)
  • Write a function called evaluate, that does not take any arguments and does not return a value. This function will read in an arbitrary number of values from the user: an integer followed by a character that denotes an operation. Once you have the number and the operation, call the appropriate function from the five that you just wrote to keep a running evaluated total. The user input ends when the character entered is a ‘=’. When the user is done with input, print the result. (15 points)
  • You may assume that all inputs are valid. You need not test for wrong inputs.
  • Here, you need to ignore the order of operations and calculate the values as they come.
  • Please include comments wherever appropriate. (5 points)

5.1 Sample Run

Enter the expression:
1
+
26
*
2
%
19
/
5
=
The result is 3

5.2 Sample Run 2

Enter the expression:
12
*
9
-
37
+
-8
=
The result is 63

6 Generic Guidelines

1. Include the header comment with your name and other information on the top of your files.

6

  1. Please make sure you’re only using the concepts already discussed in class. That is, please try and restrict yourself to input/output statements, variables, selection statements, loops, functions and arrays. Using any other concept (like pointers) will result in loss of points.
  2. You cannot use arrays for Problem 1 and Problem 4. For those, you are restricted to functions, loops and selection statements.
  3. The first 2 programs are worth 75 points each. the third program is worth 50 points.
  4. Please make sure that you’re conforming to specifications (program name, print statements, expected inputs and outputs etc.).
  5. No global variables (variables outside of main() ), excpet for constants.
  6. All input and output must be done with streams, using the library iostream
  7. You may only use the iostream, iomanip and the cmath libraries (you do not need any others for these tasks)
  8. NO C style printing is permitted. (Aka, dont use printf). Use cout if you need to print to the screen.
  9. When you write source code, it should be readable and well-documented (comments).
  10. Make sure you either develop with or test with Jetbrains CLion (to be sure it reports no compile errors or warnings!) before you submit the program.
  11. Program submissions should be done through the Canvas class page, under the assignments tab (if its not there yet Ill create it soon.) Do not send program submissions through e-mail e-mail attachments will not be accepted as valid submissions.
  12. TheONLYfilesyouwillsubmitviaCanvasarefunctions.cpp, arrays.cppandmatrix.cpp, (and the extra credit program evaluate.cpp, if you are attempting it).
  13. Please make sure you’ve compiled and run your program before you turn it in. Compilation errors can be quite costly (4 points per error,until the 10th error, after which you will be assigned a grade of 0).
  14. General Advice – always keep an untouched copy of your finished homework files in your email. These files will have a time-stamp which will show when they were last worked on and will serve as a backup in case you ever have legitimate problems with submitting files through Canvas. Do this for ALL programs.
  • 2

Order now and get 10% discount on all orders above $50 now!!The professional are ready and willing handle your assignment.

ORDER NOW »»