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

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...
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...
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT