Question

In: Computer Science

Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects •...

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;

}

Solutions

Expert Solution

#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:


Related Solutions

This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This lab maps to learning the following objectives: Write a working program that creates a Class, Instances of Objects from that Class, and Functions that use Objects as parameters. For this portion of the lab, you will create a new program for your Professor. Create a class named Student that holds the following data about a student: 1. Name 2. Student ID number 3. GPA...
Explain what classes and objects are in object - oriented programming. Give an example of each...
Explain what classes and objects are in object - oriented programming. Give an example of each and explain how they work together in a computer program.
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored...
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored in Memory? D. Why do you not need to declare when you are finished using an Object in Java? E. Can you edits the contents of a String? View keyboard shortcuts EditViewInsertFormatToolsTable 12pt Paragraph
This task is about classes and objects, and is solved in Python 3. We will look...
This task is about classes and objects, and is solved in Python 3. We will look at two different ways to represent a succession of monarchs, e.g. the series of Norwegian kings from Haakon VII to Harald V, and print it out like this: King Haakon VII of Norway, accession: 1905 King Olav V of Norway, accession: 1957 King Harald V of Norway, accession: 1991 >>> Make a class Monarch with three attributes: the name of the monarch, the nation...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Mathematical Modeling - Fractions · Operator Overloading – Binary Operators (Internal Overload) · Operator Overloading – Binary Operator (External...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Object-oriented Paradigm · Operator Overloading - Internal · Operator Overloading - External · Mathematical Modeling Assignment In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector. Use the following...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a Singapore Airlines Loyalty program. You are asked to write an inheritance hierarchy discount system that benefits KrisFlyer members program to calculate their profit. A brief about KrisFlyer is that it is useful for those who fly on Singapore Airlines (its partners like Virgin Australia and Air New Zealand) frequently. KrisFlyer miles can be earned through credit cards, flying and bonus miles promotions. The miles...
C++ PLEASE This will be a multi-part assignment. Later homework will expand upon what is developed...
C++ PLEASE This will be a multi-part assignment. Later homework will expand upon what is developed in this assignment. 1. Create a class called MyComplex to represent complex numbers. Each part (Real and Imaginary parts) should be stored as a double. Data members should be private and you need to supply appropaiate accessor and mutator methods. 2. Supply a default constructor (both parts zero), a constructor that allows the real part to be set and a third constructor that allows...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write a menu-driven program for Food Court. (You need to use functions!) Display the food menu to a user (Just show the 5 options' names and prices - No need to show the Combos or the details!) Ask the user what he/she wants and how many of it. (Check the user inputs) AND Use strip() function to strip your inputs. Keep asking the user until...
How algorithms address object-oriented classes and objects. What is the File object? How are File objects...
How algorithms address object-oriented classes and objects. What is the File object? How are File objects used in algorithms? 175 words minumum please :)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT