Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation.
The Functions:
You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness.
-
findLast: takes an array of integers, its size and a target integer value. Returns an index of the last occurrence of the target element in the array. If the target value doesn't exist, returns -1. For this function only: Do not use square brackets anywhere in the function, not even the parameter list (use pointer notation instead).
-
nextFibonacci. Modify the following function to use pointers as parameters (instead of reference parameters). The Fibonacci sequence is a series of numbers where each number is the addition of the previous two numbers, starting with 0, and 1 (specifically: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…). This function updates the given i-th and (i+1)-st Fibonacci numbers to (i+1)-st and (i+2)-nd. For example, if fibCurr is 13 and fibNext is 21, it puts 21 in fibCurr and 34 in fibNext. Don't modify the function except to make it use pointers instead of reference parameters.
void nextFibonacci(int& fibCurr, int& fibNext){ int fibPrev = fibNext - fibCurr; fibNext += fibCurr; fibCurr += fibPrev; }
-
repeat: takes an array of integers, its size and a number N. Creates a new array that is N times bigger than an input array and fills the new array with values of an input array cyclically repeated N times (see Sample Execution). Returns the new array.
-
swapEvenOdd: takes an array of integers and its size. Creates and returns a new array that is the same as an original array, but with each element with an even index swapped with a subsequent element with an odd index (if it exists).
-
subArray: takes an int array, a start index and a length as arguments. It creates a new array that is a copy of the elements from the original array starting at the start index, and has length equal to the length argument. For example, subArray(aa,5,4) would return a new array containing only the elements aa[5], aa[6], aa[7], and aa[8]. You must define subArray as follows: Add the code for the subArray function given below to your program. Fill in the blanks with expressions so that the function subArray behaves as described above. Note that the duplicateArray function (from the Unit 3 lecture slides) has already been added to the provided template main.cpp file. DO NOT alter duplicateArray, DO NOT alter subArray as defined below.
int *subArray (int *array, int start, int length) { int *result = duplicateArray(__________, ___________); return result; }
Main function
Test these five functions using the main function as a driver. The driver should pass (constant) test data as arguments to the functions. Select appropriate test data for each function and then call that function using the test data. For each function, you should output four lines: a label indicating which function is being tested, the test data, the expected results, and the actual results. For the test data and Expected result, you should hard code the output values (use string literals containing the numeric values), for the Actual results, use the actual values returned/altered by the function.
Rules
- DO NOT change the names of the functions or the order of parameters!
- DO NOT do any output from the functions (only from main)!
- DO NOT do any input at all!
Notes
- Use the template in the provided main.cpp file and sample output below as guides to test your functions. Remember, your output should match exactly, so you may need to play with spacing.
- Your program should release any dynamically allocated memory when it is finished using it.
Output
Testing findLast: test data: 1 2 3 2 1 2 searching for value 1 result: 4 expected: 4 Testing nextFibonacci: test data: 55 89 result: 89 144 expected: 89 144 Testing repeat: test data: 4 5 1 N = 4 result: 4 5 1 4 5 1 4 5 1 4 5 1 expected: 4 5 1 4 5 1 4 5 1 4 5 1 Testing swapEvenOdd: test data: 1 2 7 8 23 20 3 result: 2 1 8 7 20 23 3 expected: 2 1 8 7 20 23 3 Testing subArray: test data: 18 19 15 16 12 11 13 14 start and length: 3 4 result: 16 12 11 13 expected: 16 12 11 13
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images
- Write the main program to create an array of size 10. Create two integer variables called max and min. Write a function to accept this array and its size. This function must populate the array with random numbers in the range -400 to 300. Write a different function to accept this array, its size and the address of the two variables max and min. The function must find the max and min of this array and write it to the min and max using pointer notation. Print the array and the values of max and min in the main program. in C++ visual studioarrow_forwardWrite a program in C write a two-dimension array in the main function that contains different names and grades, then write a function that will ask the user if he wants : Press 1 for print passing students names and grades Press 0 for print failing students names and grades Press 2 for printing all the student's names and grades after that, the function will print what the user wants hint : >= 60 passedarrow_forwardPlease write the following in very simple C++ code: Write a function that calculates the average of an array, where the smallest element in the array is dropped. If there are multiple copies of the smallest value, just ignore one copy. You can assume the array has at least two items in it. The function header is as follows. int getAverage (int a[], int size)arrow_forward
- Create a function using Java: Number of Rows = 3-6 Number of Columns = 3-6 Function Name: winInDiagonalFSParameters: board: 2D integer array, piece: integerReturn: booleanAssume board is valid 2D int array, piece is X==1/O==2.Look at all forward slash / diagonals in the board. If any forward slash / diagonals has at least 3consecutive entries with given type of piece (X/O) (3 in a row XXX, OOO), then return true,otherwise falsearrow_forwardHelp me structure a program in C++ that opens a file for read. Then reads the data in from the file and calculates the sum of the row values. The data is all integers with 7 values per line, and 10 lines of data in the file. The program must use two functions, one to open the file, and another to output and calculate the sum of the row values. All data must be read into a two dimensional array first, then the data is output and the sum calculated. The data should be printed out by row, along with the sum of the row values. Prototypes to be used: void rowSumCalculator( int myArray[][10], int rowCount, int colCount );Function rowSumCalculator( ) writes out the data in the array and the sum of the values in the row. void openFile( ifstream& infile, string prompt );Function openFile should print the prompt out to the user, read in the filename and open the file. If the file does not open, an error is written to the display, and the user prompted again. The function doesnot return until…arrow_forwardWrite a c++ function that accepts an array of doubles and the array’s size as its only arguments. The function should use dynamic memory allocation to create a new array that is double the size of the argument array. The function should copy the contents of the argument array to the second half of the new array and initialize the first half of the new 0s. The function should return a pointer to the new array. So, if the original array contained 1 2 3 4, the new array should contain 0 0 0 0 1 2 3 4. So, if the original array contained 1 2 3 4, the new array should contain 0 0 0 0 1 2 3 4.arrow_forward
- Help with writing a program a C program implements a bubble sort algorith on an array of integers, and use command line paramameters to populate an array with data. The program should follow below guidelines if possible: If there are no command-line arguments at all when the program is run, the program should print out instructions on its use (a "usage message"). There should be one common usage message (consider a method/function for printing the usage message) for any type of usage error. The program will accept an A or D as the second command line argument (after the program name). This letter will tell you whether the bubble sort should sort in ascending or descending fashion. Anything other than A or D in that position should display the usage message and terminate the program. The program will be able to accept up to 32 numbers (integers) on the command line. If there are more than 32 numbers on the command line, or no numbers at all, the program should print out the usage…arrow_forwardWrite a C++ program : Question:Ask user to give you a list of the numbers and then Sort them, by calling two functions: Asc(Sort in Ascending order), and Desc(Sort in Descending order): Do the above question by using array. First ask user how many numbers are there in the list and then get the numbers and sort them.arrow_forwardIn C++ please and thank you!arrow_forward
- Using either pseudocode of C++ code, write a function that takes three parameters and performs a sequential search. The first parameter is an array of integers. The second parameter is an integer representing the size of the array. The third parameter takes the value to be search form. The function should return the subscripts at which the value is found or -1 is the array does not contain the search term. Please dont use vectors and explain each steparrow_forwardDefine a function named “getExamListInfo” that accepts an array of Exam objects and its size. It will return the following information to the caller: - The number of exams with perfect score - The number of exams with “Pass” status - The index of the Exam object in the array that has the largest score. For example, if you get the exam info for this array of Exam object Exam examList[] = { {"Midterm1 Exam", 90}, {"Midterm2 Exam", 80}, {"Final Exam", 50}, {"Extra Credit", 100}, {"Initial Test", 0}, {"Homework1", 69} } ; You will get Perfect Count: 1 Pass Count: 3 Index of the largest: 3 C++arrow_forwardRevise the following Course class implementation in the following c++ code When adding a new student to the course, if the array capacity is exceeded, increase the array size by creating a new larger array and copying the contents of the current array to it. Implement the dropStudent function. Add a new function named clear() that removes all students from the course. Implement the destructor and copy constructor to perform a deep copy in the class. Write a test program that creates a course, adds three students, removes one, and displays the students in the course.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education