Question

Please complete the following programming in C++ with clear explanations. Thanks! Primitive Types, Searching and Recursion...

Please complete the following programming in C++ with clear explanations. Thanks!

Primitive Types, Searching and Recursion

a) Create a class Homework (in a file homework.h and homework.cpp).

b) Create a function initialize_array that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 1s in the odd positions of the array and 0s in the even positions. (Use pointers to pass an array of integers as parameter)

c) Create a function print_array that receives as parameters an array of integers and the array size. Use a for statements to print all the elements in the array. (Use pointers to pass an array of integers as parameter).

d) Create a function selection_sort that receives as parameters an array of integers and the array size and order the array element in descending order. Implement Selection Sort algorithm. It should be Selection Sort, not Bubble Sort, not Quick Sort, etc.

e) Create a RECURSIVE function that calculate and returns the factorial of a number. The function receives the number (integer number) as parameter

f) Create a file main_part1.cpp. Copy the following main function in your class,

int main() {

int a [10] = {3, 5, 6, 8, 12, 13, 16, 17, 18, 20};

int b [6]= {18, 16, 19, 3 ,14, 6};

int c [5]= {5, 2, 4, 3, 1};

Homework h;

// testing initialize_array

h.print_array(a, 10); // print: 3, 5, 6, 8, 12, 13, 16, 17, 18, 20

h.initialize_array(a, 10);

h.print_array(a, 10); // print: 0, 1, 0, 1, 0, 1, 0, 1, 0, 1

// testing initialize_array

h.print_array(b, 6); // print: 18, 16, 19, 3 ,14, 6

h.selection_sort (b, 6);

h.print_array(b, 6); // print: 19, 18, 16, 14, 6, 3

// testing factorial

cout <<"Factorial of 5 = “ << h.factorial (5) <<endl; //print: 120

c[0] = h.factorial (c[0]);

c[1] = h.factorial (c[2]);

h.print_array(c, 5); // print: 120, 24, 4, 3, 1
  
return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

#include <iostream>
using namespace std;

class Homework
{
private:
int size;
int* arr;

public:
Homework()
{
}

void printArray(int nos[], int size)
{
for (int i = 0; i < size; i++)
{
cout << nos[i];
if (i != size - 1)
{
cout << ", ";
}
}
cout << endl;
}
void initializeArray(int a[], int s)
{

for (int i = 0; i < s; i++)
{
if ((i + 1) % 2 == 0)
{
a[i] = 1;
}
else
{
a[i] = 0;
}
}
this->size = s;
this->arr = a;
}

void arraySelectionSort(int nos[], int size)
{
int small;
for (int i = 0; i < size; i++)
{
int m = i;
for (int j = i + 1; j < size; j++)
{
if (nos[j] < nos[m])
m = j;
}
small = nos[m];
nos[m] = nos[i];
nos[i] = small;
}
}
int factorial(int num)
{
if (num >= 1)
return num * factorial(num - 1);
else
return 1;
}
};

int main()
{

int a[10] = { 3, 5, 6, 8, 12, 13, 16, 17, 18, 20 };

int b[6] = { 18, 16, 19, 3, 14, 6 };

int c[5] = { 5, 2, 4, 3, 1 };

Homework h;

// testing initializeArray

h.printArray(a, 10); // print: 3, 5, 6, 8, 12, 13, 16, 17, 18, 20

h.initializeArray(a, 10);
h.printArray(a, 10); // print: 0, 1, 0, 1, 0, 1, 0, 1, 0, 1

// testing initializeArray

h.printArray(b, 6); // print: 18, 16, 19, 3 ,14, 6

h.arraySelectionSort(b, 6);

h.printArray(b, 6); // print: 3, 6, 14, 16, 18, 19

// testing factorial

cout << "Factorial of 5 = " << h.factorial(5) << endl; // print: 120
c[0] = h.factorial(c[0]);
c[1] = h.factorial(c[2]);
h.printArray(c, 5); // print: 120, 24, 4, 3, 1
return 0;
}


Output:-

Add a comment
Know the answer?
Add Answer to:
Please complete the following programming in C++ with clear explanations. Thanks! Primitive Types, Searching and Recursion...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives...

    Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives as a parameter an array of integers. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. b) Implement the static method printArray that receives as a parameter an array of integers. Use a for statements to print all the elements in the array. c) Implement the static method insertionSort...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of...

    create a file homework_part_1.c a) Implement the function initialize_array 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. b) Implement the function print_array that receives as parameters an array of integers and the array size. Use a for statements...

  • I am having problems getting the insertion sort function to output in descending order. Also, the...

    I am having problems getting the insertion sort function to output in descending order. Also, the initialize array function is not being called when I test it in the main function. I am wondering why it prints out the original array instead of initializing. Thank you. #include <stdio.h> void print_array(int *array, int length){ int i; for(i=0; i<length;i++) printf("%d"",",array[i]); printf("\n"); } void initialize_array(int *array, int length){ int i; for(i=0, i<length; i++;){ if (i%2 ==0) array[i]= 5; else array[i]= 0; } }...

  • 1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching...

    1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...

  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • Write a c program that finds the uncommon elements from two array elements using pointers only...

    Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • C++ program Correctly complete the following assignment. Follow all directions. The main purpose is to show...

    C++ program Correctly complete the following assignment. Follow all directions. The main purpose is to show super and sub class relationships with an array of super media pointers to sub class objects and dynamic binding. The < operator will be overloaded in the super class so all subclasses can use it. The selection sort method will be in the main .cpp file because it will sort the array created in main. The final .cpp file, the three .h header class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT