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.
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...
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
4.4 Lab: Arrays of Pointers to Structures (Sort) C++ programming This assignment is based on Homework...
4.4 Lab: Arrays of Pointers to Structures (Sort) C++ programming This assignment is based on Homework 3. The program will create an array of Airport structures. On the first line in the input file airports.txt there is an integer n, representing the number of lines in the input file. On each of the following n lines there is an airport code (a unique identifier) followed by the airport’s number of enplanements (commercial passenger boardings), and city served. You may assume...
Classes and Objects are the central of Object Oriented Programming (O.O.P.) This is a style of...
Classes and Objects are the central of Object Oriented Programming (O.O.P.) This is a style of programming that focuses on using objects to design and build many great applications. What is the difference between a class an an instance of the class? Explain what is constructor? What do you call a constructor that accepts no arguments? Explain the "has-a" relationship can exist between classes. Explain what is the "this" keyword? Do the following: Answer the 4 points with only one...
Java language This is your first homework on objects and classes, and the basics of object-oriented...
Java language This is your first homework on objects and classes, and the basics of object-oriented programming. For each exercise, your task is to first write the class that defines the given object. Compile it and check it for syntax errors. Then write the “demo class” with the main program that creates and uses the object. a)You are planning to purchase a new motor boat for cool cruises on Porter’s Lake this summer, but you want to simulate it before...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you familiar with some of the most used JAVA syntax, as well as constructors objects, objects calling other objects, class and instance attributes and methods. You will create a small program consisting of Musician and Song classes, then you will have Musicians perform the Songs. Included is a file “Assignment1Tester.java”. Once you have done the assignment and followed all instructions, you should be able to...
Using*************** C++ **************** explain what Objects and Classes are. Please describe in detail.
Using*************** C++ **************** explain what Objects and Classes are. Please describe in detail.
c++ Programming Assignment 1: Game of Life The objective of this programming assignment is to design...
c++ Programming Assignment 1: Game of Life The objective of this programming assignment is to design and implement what is known as the “Game of Life”, conceptualized by the British mathematician John Horton Conway in 1970 to simulate the evolution patterns in a population of living organisms.   The game board is seeded with an initial population pattern, and then evolves based on the set of rules defining when a cell dies or is born into life. A cell’s life cycle...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT