In: Computer Science
Homework 3 – Programming with C++
What This Assignment Is About: • Classes and Objects • Methods • Arrays of Primitive Values • Arrays of Objects • Recursion • for and if Statements • Insertion Sort
2. Use the following Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects) • Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent. • Use white space to make your program more readable. For each file in your assignment, provide a heading (in comments) which includes: • The assignment number. • Its author (your name). • A description of what this program is doing.
3. Part 1. Primitive Types, Searching, Recursion (35 points). a) In the file homework_part_1.cpp (available on Canvas), add header comments and function comments b) Implement the function initializeArray that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters.
c) Implement the function printArray that receives as parameters an array of integers and the array size. Use a for statements to print all the elements in the array. You must use pointers to work with the array. Hint: review pointers as parameters.
d) Implement the function arrayInsertionSort that receives as parameters an array of integers and the array size, and order the array’s elements in descending order. Implement Insertion Sort algorithm. It should be Insertion Sort, not Selection Sort, not Quick Sort, etc.
e) Implement the recursive function that calculates and returns the factorial of a number. The function receives the number (integer number) as parameter
f) Compile and run the code for homework_part_1.cpp Any changes made to the main method of the file will be penalized unless otherwise instructed
using namespace std;
void initializeArray(int* array, int length)
{
}
void printArray(int* array, int length)
{
}
void insertionSort(int* array, int length)
{
}
int factorial(int num)
{
return 0;
}
int main()
{
int a[] = {2, 5, 7, 9, 12, 13, 15, 17, 19, 20};
int b[] = {18, 16, 19, -5, 3, 14, 6, 0};
int c[] = {4, 2, 5, 3, 1};
/* testing initialize_array */
printArray(a, sizeof(a)/sizeof(a[0])); /* print: 2, 5, 7, 9, 12, 13, 15, 17,
19, 20 */
initializeArray(a, sizeof(a)/sizeof(a[0]));
printArray(a, sizeof(a)/sizeof(a[0])); /* print: 5, 0, 5, 0, 5, 0, 5, 0, 5, 0
*/
/* testing insertionSort */
printArray(b, sizeof(b)/sizeof(b[0])); /* print: 18, 16, 19, -5, 3, 14, 6, 0 */
insertionSort (b, sizeof(b)/sizeof(b[0]));
printArray(b, sizeof(b)/sizeof(b[0])); /* print: 19, 18, 16, 14, 6, 3, 0, -5 */
/* testing factorial */
cout << factorial(5) << endl; /* print: 120 */
c[0] = factorial (c[0]);
c[1] = factorial (c[2]);
printArray(c, sizeof(c)/sizeof(c[0])); /* print: 24, 120, 5, 3, 1 */
return 0;
}
#include <iostream>
using namespace std;
// function to initialize the array with 0s in the odd positions and 5s in the even positions.
void initializeArray(int* array, int length)
{
for(int i=0;i<length;i++)
{
if(i%2 == 0) // if i is even
*(array+i) = 5;
else
*(array+i) = 0;
}
}
// function to print the elements of the array
void printArray(int* array, int length)
{
for(int i=0;i<length-1;i++)
cout<<*(array+i)<<", ";
cout<<*(array+length-1)<<endl;
}
// function to sort the elements of the array using insertion sort in descending order
void insertionSort(int* array, int length)
{
int i,j;
int key;
for(i=1;i<length;i++)
{
j = i-1;
key = *(array+i);
while((j >=0) && (*(array+j) < key))
{
*(array+j+1) = *(array+j);
j--;
}
*(array+j+1) = key;
}
}
// function to calculate the factorial of num using recursion
int factorial(int num)
{
if(num < 2)
return 1;
else
return num*factorial(num-1);
}
int main() {
int a[] = {2, 5, 7, 9, 12, 13, 15, 17, 19, 20};
int b[] = {18, 16, 19, -5, 3, 14, 6, 0};
int c[] = {4, 2, 5, 3, 1};
/* testing initialize_array */
printArray(a, sizeof(a)/sizeof(a[0])); /* print: 2, 5, 7, 9, 12, 13, 15, 17,
19, 20 */
initializeArray(a, sizeof(a)/sizeof(a[0]));
printArray(a, sizeof(a)/sizeof(a[0])); /* print: 5, 0, 5, 0, 5, 0, 5, 0, 5, 0
*/
/* testing insertionSort */
printArray(b, sizeof(b)/sizeof(b[0])); /* print: 18, 16, 19, -5, 3, 14, 6, 0 */
insertionSort (b, sizeof(b)/sizeof(b[0]));
printArray(b, sizeof(b)/sizeof(b[0])); /* print: 19, 18, 16, 14, 6, 3, 0, -5 */
/* testing factorial */
cout << factorial(5) << endl; /* print: 120 */
c[0] = factorial (c[0]);
c[1] = factorial (c[2]);
printArray(c, sizeof(c)/sizeof(c[0])); /* print: 24, 120, 5, 3, 1 */
return 0;
}
//end of program
Output: