Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ PROGRAMMING QUESTION: PLEASE TIE THESE THREE FUNCTIONS TOGETHER INTO ONE COM

ID: 3887799 • Letter: C

Question

C++ PROGRAMMING

QUESTION: PLEASE TIE THESE THREE FUNCTIONS TOGETHER INTO ONE COMPLETE COMPILE PROGRAM.

1ST:

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

//Initialize_array should fill the array with random numbers in the range of 1-100.

void initialize_array(int array[], int count)

{

    for(int i = 0; i < count; i++)

        array[i] = rand() % 100 + 1;

}

//Prints the array.

void print_array(int array[], int count)

{

    for(int i = 0; i < count; i++)

    {

       cout << array[i] << " ";

       if((i+1) % 10 == 0)

           cout << endl;

    }

    cout << endl;

}

//Sorts the array, and stores the result in a new array.

int * sort_array(int array[], int count)

{

    int *newArray = new int[count];

    for(int i = 0; i < count; i++)

        *(newArray + i) = *(array + i);

    for(int i = 0; i < count-1; i++)

        for(int j = 0; j < count - i - 1; j++)

            if(newArray[j] > newArray[j+1])

            {

               int temp = newArray[j];

               newArray[j] = newArray[j+1];

               newArray[j+1] = temp;

            }

    return newArray;        

}

int main()

{

    int count;

    //Start by prompting the user for the amount of numbers you want to work with.

    cout << "Enter the count of numbers you want to work with: ";

    cin >> count;

    //Dynamically allocate an array of that size.

    int array[count];

    //Fill the array by calling the function initialize_array.

    srand(time(NULL));

    initialize_array(array, count);

    //Display the array

    print_array(array, count);

    //Call a function that creates a NEW array by sorting the current one.

    int* newArray = sort_array(array, count);

    //Display the contents of both arrays after the function call, with appropriate labeling.

    cout << "The initial array is:" << endl;

    print_array(array, count);

    cout << endl << "The newly sorted array is:" << endl;

    print_array(newArray, count);

}

2ND:

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

void split_up_array(int a[], int n, int * &b, int * &c, int &sizeB, int &sizeC) {

int index = (rand()%(n-2))+1; // For n = 10, it produces index from 1 to 8

sizeB = index;

sizeC = n - index - 1;

b = new int[sizeB];

c = new int[sizeC];

for(int i=0; i<index; i++) {

b[i] = a[i];

}

for(int i=0; i<(n-index-1); i++) {

c[i] = a[index+1+i];

}

}

void printArray(int a[], int len) {

for(int i=0; i<len; i++) {

cout << a[i] << " ";

}

cout << endl;

}

int main()

{

srand((unsigned)time(NULL));

int sizeA = 10;

int *a = new int[sizeA];

for(int i=0; i<sizeA; i++) {

a[i] = rand() % 100;

}

int *b, *c;

int sizeB, sizeC;

cout << "Before split:" << endl;

printArray(a, sizeA);

split_up_array(a, sizeA, b, c, sizeB, sizeC);

cout << "After split:" << endl;

cout << "B:" << endl;

printArray(b, sizeB);

cout << "C:" << endl;

printArray(c, sizeC);

return 0;

}

3RD:

#include<iostream>

using namespace std;

int calc_sum(int* a,int m,int* b,int n,int* c,int k)/*calc_sum() function definition*/

{

int l,num1,num2; /*variable declaration*/

for(l=0;l<k;l++) /*loop repeats for k times (k is the resultant array size)*/

{

if(l<m) /*if the index of the first array is less than m */

num1=a[l]; /* lth index value of first array*/

else /* if the index of the first array is greater than m*/

num1=0; /*element is zero*/

if(l<n) /*if the index of the second array is less than n */

num2=b[l]; /* lth index value of second array*/

else /* if the index of the second array is greater than n*/

num2=0;/*element is zero*/

c[l]=num1+num2; /*lth index value of new array c*/

}

return 1; /*return 1 to the main() function*/

}

void print_array(int a[],int size)/*print_array() definition*/

{

for(int i=0;i<size;i++) /*prints the array elements on the screen*/

cout<<a[i]<<" ";

}

int main() /*main() function definition*/

{

int a[100],b[100],c[100],m,n,i,large,val; /*variable declaration*/

cout<<"Enter the number of elements of the first array:"; /*prompts the message*/

cin>>m; /*size of first array*/

cout<<"Enter the number of elements of the second array:";/*prompts the message*/

cin>>n;/*size of second array*/

cout<<"Enter the elements of the first array:";/*prompts the message*/

for(i=0;i<m;i++)/*takes the elements into first array*/

cin>>a[i];

cout<<"Enter the elements of the second array:";/*prompts the message*/

for(i=0;i<n;i++)/*takes the elements into second array*/

cin>>b[i];

cout<<"Before function call, The elements of the first array are:";/*prompts the message*/

print_array(a,m);/*function call to print_array()*/

cout<<" Before function call, The elements of the second array are:";/*prompts the message*/

print_array(b,n);/*function call to print_array()*/

large=m>n?m:n;/*identifies the largest size array between first and second arrays*/

val=calc_sum(a,m,b,n,c,large); /*function call to calc_sum()*/

if(val==1)

{

cout<<" After function call, The elements of the first array are:";/*prompts the message*/

print_array(a,m);/*function call to print_array()*/

cout<<" After function call, The elements of the second array are:";/*prompts the message*/

print_array(b,n);/*function call to print_array()*/

cout<<" The new array is:";/*prompts the message*/

print_array(c,large);/*function call to print_array()*/

}

return 0;

}/*end of main()*/

Explanation / Answer

//there are multiple definition of print_array function

//so i comment out one of them

//i put all the function definition outside of main()

//and merge all main to one main

//you can now manipulate main() function according to your wish

//and can get the result by coding the main()

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

//Initialize_array should fill the array with random numbers in the range of 1-100.

void initialize_array(int array[], int count)

{

for (int i = 0; i < count; i++)

array[i] = rand() % 100 + 1;

}

////Prints the array.

//void print_array(int array[], int count)

//{

// for (int i = 0; i < count; i++)

// {

// cout << array[i] << " ";

// if ((i + 1) % 10 == 0)

// cout << endl;

// }

// cout << endl;

//}

//Sorts the array, and stores the result in a new array.

int * sort_array(int array[], int count)

{

int *newArray = new int[count];

for (int i = 0; i < count; i++)

*(newArray + i) = *(array + i);

for (int i = 0; i < count - 1; i++)

for (int j = 0; j < count - i - 1; j++)

if (newArray[j] > newArray[j + 1])

{

int temp = newArray[j];

newArray[j] = newArray[j + 1];

newArray[j + 1] = temp;

}

return newArray;

}

void split_up_array(int a[], int n, int * &b, int * &c, int &sizeB, int &sizeC) {

int index = (rand() % (n - 2)) + 1; // For n = 10, it produces index from 1 to 8

sizeB = index;

sizeC = n - index - 1;

b = new int[sizeB];

c = new int[sizeC];

for (int i = 0; i<index; i++) {

b[i] = a[i];

}

for (int i = 0; i<(n - index - 1); i++) {

c[i] = a[index + 1 + i];

}

}

void printArray(int a[], int len) {

for (int i = 0; i<len; i++) {

cout << a[i] << " ";

}

cout << endl;

}

int calc_sum(int* a, int m, int* b, int n, int* c, int k)/*calc_sum() function definition*/

{

int l, num1, num2; /*variable declaration*/

for (l = 0; l<k; l++) /*loop repeats for k times (k is the resultant array size)*/

{

if (l<m) /*if the index of the first array is less than m */

num1 = a[l]; /* lth index value of first array*/

else /* if the index of the first array is greater than m*/

num1 = 0; /*element is zero*/

if (l<n) /*if the index of the second array is less than n */

num2 = b[l]; /* lth index value of second array*/

else /* if the index of the second array is greater than n*/

num2 = 0;/*element is zero*/

c[l] = num1 + num2; /*lth index value of new array c*/

}

return 1; /*return 1 to the main() function*/

}

void print_array(int a[], int size)/*print_array() definition*/

{

for (int i = 0; i<size; i++) /*prints the array elements on the screen*/

cout << a[i] << " ";

}

int main()

{

int count;

//Start by prompting the user for the amount of numbers you want to work with.

cout << "Enter the count of numbers you want to work with: ";

cin >> count;

//Dynamically allocate an array of that size.

int *array = new int[count];

//Fill the array by calling the function initialize_array.

srand(time(NULL));

initialize_array(array, count);

//Display the array

print_array(array, count);

//Call a function that creates a NEW array by sorting the current one.

int* newArray = sort_array(array, count);

//Display the contents of both arrays after the function call, with appropriate labeling.

cout << "The initial array is:" << endl;

print_array(array, count);

cout << endl << "The newly sorted array is:" << endl;

print_array(newArray, count);

//srand((unsigned)time(NULL));

int sizeA = 10;

int *A = new int[sizeA];

for (int i = 0; i<sizeA; i++) {

A[i] = rand() % 100;

}

int *B, *C;

int sizeB, sizeC;

cout << "Before split:" << endl;

printArray(A, sizeA);

split_up_array(A, sizeA, B, C, sizeB, sizeC);

cout << "After split:" << endl;

cout << "B:" << endl;

printArray(B, sizeB);

cout << "C:" << endl;

printArray(C, sizeC);

//return 0;

int a[100], b[100], c[100], m, n, i, large, val; /*variable declaration*/

cout << "Enter the number of elements of the first array:"; /*prompts the message*/

cin >> m; /*size of first array*/

cout << "Enter the number of elements of the second array:";/*prompts the message*/

cin >> n;/*size of second array*/

cout << "Enter the elements of the first array:";/*prompts the message*/

for (i = 0; i<m; i++)/*takes the elements into first array*/

cin >> a[i];

cout << "Enter the elements of the second array:";/*prompts the message*/

for (i = 0; i<n; i++)/*takes the elements into second array*/

cin >> b[i];

cout << "Before function call, The elements of the first array are:";/*prompts the message*/

print_array(a, m);/*function call to print_array()*/

cout << " Before function call, The elements of the second array are:";/*prompts the message*/

print_array(b, n);/*function call to print_array()*/

large = m>n ? m : n;/*identifies the largest size array between first and second arrays*/

val = calc_sum(a, m, b, n, c, large); /*function call to calc_sum()*/

if (val == 1)

{

cout << " After function call, The elements of the first array are:";/*prompts the message*/

print_array(a, m);/*function call to print_array()*/

cout << " After function call, The elements of the second array are:";/*prompts the message*/

print_array(b, n);/*function call to print_array()*/

cout << " The new array is:";/*prompts the message*/

print_array(c, large);/*function call to print_array()*/

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote