Question

c++ I have to write the following functions that I will later use for testt but...

c++

I have to write the following functions that I will later use for testt but you just have to write the following functions again I have written the interface of it and please make sure it works. will have an array of randomly generated numbers - An array that is already sorted in ascending order - An array that is sorted in reverse (descending) order - An array that is sorted except for the last 10 numbers which are random - An array of few randomly generated numbers. These functions’ prototypes are:

/** @post Populates values with integers in ascending order @param values the array to populate @param size of the array to be populated */

void generateAscendingArray(int values[], size_t size); /

** @post Populates values with integers in descending order @param values the array to populate @param size of the array to be populated */

void generateDescendingArray(int values[], size_t size); /**

@post Populates values with integers in ascending order except for the last 10 that are randomly generated @param values the array to populate @param size of the array to be populated */

void generateLastTenRandomArray(int values[], size_t size);

/** @post Populates values with integers in randomly generated in range size/10 @param values the array to populate @param size of the array to be populated */

void generateFewRandomArray(int values[], size_t size);

Thank you very much. Please make sure your code works

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

//written all the required function along iwth helper function printArray which prints the array , which is required to print every time functions are called to print and see the result, you can find my changes with keyword CHEGGEA. I tested all the functions you listed. and output shows that my functions works fine . Please don't give -ve ratings as lot of effors from our end and tested the functions .

#include <iostream>

//for random number generation , below file used

#include<ctime>

#include<cstdlib>

using namespace std;

/** @post Populates values with integers in ascending order @param values the array to populate @param size of the array to be populated */

void generateAscendingArray(int values[], size_t size); /** @post Populates values with integers in descending order @param values the array to populate @param size of the array to be populated */

void generateDescendingArray(int values[], size_t size); /**

@post Populates values with integers in ascending order except for the last 10 that are randomly generated @param values the array to populate @param size of the array to be populated */

void generateLastTenRandomArray(int values[], size_t size);

/** @post Populates values with integers in randomly generated in range size/10 @param values the array to populate @param size of the array to be populated */

void generateFewRandomArray(int values[], size_t size);

//CHEGGEA, writter helper function to print array

void printArray(int a[],int n);

int main() {

//std::cout << "Hello World!\n";

//now randomly fill the array with some numbers

int size=100;

int arr[size];

cout<<"****Testing generateFewRandomArray****"<<endl;

generateFewRandomArray(arr,size);

cout<<"Original array is : "<<endl;

printArray(arr,size);

cout<<"****Testing generateAscendingArray****"<<endl;

generateAscendingArray(arr,size);

printArray(arr,size);

cout<<"****Testing generateDescendingArray****"<<endl;

generateDescendingArray(arr,size);

printArray(arr,size);

cout<<"****Testing generateLastTenRandomArray****"<<endl;

generateLastTenRandomArray(arr,size);

printArray(arr,size);

}

//written print array ,CHEGGEA

void printArray(int a[],int n)

{

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

{

if(i%10 == 0)

cout<<endl;

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

}

cout<<endl;

}

/** @post Populates values with integers in ascending order @param values the array to populate @param size of the array to be populated */

void generateAscendingArray(int values[], size_t size)

{

int tmp;

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

{

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

{

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

{

//swap two array elements

tmp=values[j+1];

values[j+1]=values[j];

values[j]=tmp;

}

}

}

}

/** @post Populates values with integers in descending order @param values the array to populate @param size of the array to be populated */

void generateDescendingArray(int values[], size_t size)

{

int tmp;

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

{

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

{

if(values[j] < values[j+1])

{

//swap two array elements

tmp=values[j+1];

values[j+1]=values[j];

values[j]=tmp;

}

}

}

}

/**

@post Populates values with integers in ascending order except for the last 10 that are randomly generated @param values the array to populate @param size of the array to be populated */

void generateLastTenRandomArray(int values[], size_t size)

{

int num;

//initializa srand

srand(time(NULL));

//call generateAscendingArray

generateAscendingArray(values,size);

//generate random number

for(int i = size-1; i>=size-10; i--)

{

//I am generating 1-100, you can change to whatever random numbers you want to generate in range

num = rand()%100+1;

values[i]=num;

}

}

/** @post Populates values with integers in randomly generated in range size/10 @param values the array to populate @param size of the array to be populated */

void generateFewRandomArray(int values[], size_t size)

{

int num;

//initializa srand

srand(time(NULL));

//generate random number

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

{

//I am generating 1-100, you can change to whatever random numbers you want to generate in range

num = rand()%100+1;

values[i]=num;

}

}



------------------------------------------------------------------------

//output

****Testing generateFewRandomArray****
Original array is : 

57 4 100 17 50 67 82 75 93 55 
54 89 59 84 62 26 23 62 62 95 
34 85 47 9 52 8 47 30 68 73 
25 24 28 76 93 77 42 26 51 34 
81 5 74 91 40 35 16 62 49 30 
8 34 66 55 94 69 14 40 99 82 
13 75 57 92 50 49 69 91 27 19 
76 7 75 50 49 14 36 65 76 36 
46 35 69 11 41 14 79 55 54 29 
36 18 3 92 9 4 93 29 94 19 
****Testing generateAscendingArray****

3 4 4 5 7 8 8 9 9 11 
13 14 14 14 16 17 18 19 19 23 
24 25 26 26 27 28 29 29 30 30 
34 34 34 35 35 36 36 36 40 40 
41 42 46 47 47 49 49 49 50 50 
50 51 52 54 54 55 55 55 57 57 
59 62 62 62 62 65 66 67 68 69 
69 69 73 74 75 75 75 76 76 76 
77 79 81 82 82 84 85 89 91 91 
92 92 93 93 93 94 94 95 99 100 
****Testing generateDescendingArray****

100 99 95 94 94 93 93 93 92 92 
91 91 89 85 84 82 82 81 79 77 
76 76 76 75 75 75 74 73 69 69 
69 68 67 66 65 62 62 62 62 59 
57 57 55 55 55 54 54 52 51 50 
50 50 49 49 49 47 47 46 42 41 
40 40 36 36 36 35 35 34 34 34 
30 30 29 29 28 27 26 26 25 24 
23 19 19 18 17 16 14 14 14 13 
11 9 9 8 8 7 5 4 4 3 
****Testing generateLastTenRandomArray****

3 4 4 5 7 8 8 9 9 11 
13 14 14 14 16 17 18 19 19 23 
24 25 26 26 27 28 29 29 30 30 
34 34 34 35 35 36 36 36 40 40 
41 42 46 47 47 49 49 49 50 50 
50 51 52 54 54 55 55 55 57 57 
59 62 62 62 62 65 66 67 68 69 
69 69 73 74 75 75 75 76 76 76 
77 79 81 82 82 84 85 89 91 91 
55 93 75 82 67 50 17 100 4 57 
Add a comment
Know the answer?
Add Answer to:
c++ I have to write the following functions that I will later use for testt but...
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
  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • C++ Write a program to compute the mean, median, mode, and sample standard deviation. Output results...

    C++ Write a program to compute the mean, median, mode, and sample standard deviation. Output results to output file and screen. Steps: 1. Populate an array of 99 integers (1-9 inclusively) which are generated randomly. 2. Compute mean, median, mode(with histogram) and sample standard deviation. Use similar functions: void populater (int [], int); double mean (const int [], int, ofstream&); void mode (int [], const int [], int, ofstream&); void median (int [], int, ofstream&); void sort (int [], int);...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • write the interface of the following functions in the SortedArray.cpp, by the given functions in the SortedArray.h : #if...

    write the interface of the following functions in the SortedArray.cpp, by the given functions in the SortedArray.h : #ifndef SortedArray_H #define SortedArray_H //INCLUDES #include <iostream> using namespace std; //---------------------------------- // SortedArray<Object> //---------------------------------- template <typename Object> class SortedArray { public: /** insert param: obj Description: insert new elements into the correct position in the sorted array, sliding elements over the position to right, as needed    */    void insert(const Object &obj);    /**    Description: remove the element in position 0, and slide over...

  • Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this...

    Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this question, however I found a lot of mistakes on it, where the guy declared a public class, which is not a thing in C language. Furthermore it is important to divide the question into two C files, one for part a and one for part b. hw2 Merge Sort algorithm using 2 processes a.) Define an integer array of 100...

  • Using Java solve recursively without the use of loops or modifying the method signatures. /** *...

    Using Java solve recursively without the use of loops or modifying the method signatures. /** * Given a sorted input array a, return a sorted array of size a.length + 1, * consisting of all elements of array a and integer i. * * @param a an array that is sorted in a non-descending order * @param i an integer * @return a sorted array of size a.length + 1, consisting of all elements of * array a and integer...

  • Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a...

    Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a variable passed by reference- the GCD of all integer numbers in an array void GCD(int A[], int size, int &g) and another function that allows you to divide all numbers of an array by a given integer void Div(int A[], int size, int gcd) (assume the functions are in a library, therefore you don’t need to implement them, but just use them). 1. Write...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

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
ADVERTISEMENT