Question

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.

Output:

• Display the array after the numbers have been placed in it. It will be unsorted at this point.

• Display the array after it has been sorted.

NOTE: There is no input from the User in this program.

Please post the original program fixed(with srand(time(NULL)) removed first), then post the fixed program with the new added program.



#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

void fillArray(int randNos[], int SIZE);
int getRandom(int begin, int end);
void displayArray(int randNos[], int SIZE);
int searchNumber(int randNos[], int SIZE, int s);

int main() {
    const int SIZE = 20;
    srand(time(NULL)); <---Remove this 
    int randNos[SIZE];
    char ch = 'y';
    while (ch == 'y') {
        fillArray(randNos, SIZE);
        displayArray(randNos, SIZE);
        int s = getRandom(1, 10);
        cout << "Search Number :" << s << endl;
        int cnt = searchNumber(randNos, SIZE, s);
        if (cnt == 0) {
            cout << s << " is not found in the array" << endl;
        } else {
            cout << "No of times " << s << " appears in the array is :" << cnt << endl;
        }
        cout << "\nDo you Want to continue(Y/N):";
        cin >> ch;

        if (ch == 'y' || ch == 'Y') {

            ch = 'y';
        } else {
            ch = 'n';
        }
    }

    return 0;
}

void fillArray(int randNos[], int SIZE) {
    for (int i = 0; i < SIZE; i++) {
        randNos[i] = getRandom(1, 10);
    }
}

int getRandom(int begin, int end) {
    return begin + (rand() % (end - begin + 1));
}

void displayArray(int randNos[], int SIZE) {
    cout << "Displaying Array Elements :" << endl;
    for (int i = 0; i < SIZE; i++) {
        cout << randNos[i] << " ";
    }
    cout << endl;
}

int searchNumber(int randNos[], int SIZE, int s) {
    int cnt = 0;
    for (int i = 0; i < SIZE; i++) {
        if (randNos[i] == s) {
            cnt++;
        }
    }
    return cnt;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

void fillArray(int randNos[], int SIZE);
int getRandom(int begin, int end);
void displayArray(int randNos[], int SIZE);
int searchNumber(int randNos[], int SIZE, int s);
void bubbleSort(int randNos[], int SIZE);
int main() {
const int SIZE = 20;

int randNos[SIZE];
char ch = 'y';
while (ch == 'y') {
fillArray(randNos, SIZE);
displayArray(randNos, SIZE);
int s = getRandom(1, 10);
cout << "Search Number :" << s << endl;
int cnt = searchNumber(randNos, SIZE, s);
if (cnt == 0) {
cout << s << " is not found in the array" << endl;
} else {
cout << "No of times " << s << " appears in the array is :" << cnt << endl;
}
  
bubbleSort(randNos,SIZE);
cout<<"\n:: After Sorting in ascending order ::"<<endl;
displayArray(randNos,SIZE);
  
cout << "\nDo you Want to continue(Y/N):";
cin >> ch;

if (ch == 'y' || ch == 'Y') {

ch = 'y';
} else {
ch = 'n';
}
}

return 0;
}

void fillArray(int randNos[], int SIZE) {
for (int i = 0; i < SIZE; i++) {
randNos[i] = getRandom(1, 10);
}
}

int getRandom(int begin, int end) {
return begin + (rand() % (end - begin + 1));
}

void displayArray(int randNos[], int SIZE) {
cout << "Displaying Array Elements :" << endl;
for (int i = 0; i < SIZE; i++) {
cout << randNos[i] << " ";
}
cout << endl;
}

int searchNumber(int randNos[], int SIZE, int s) {
int cnt = 0;
for (int i = 0; i < SIZE; i++) {
if (randNos[i] == s) {
cnt++;
}
}
return cnt;
}
void bubbleSort(int randNos[], int SIZE)
{
       //This Logic will Sort the Array of elements in Decending order
   int temp1;
   for (int i = 0; i < SIZE; i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (randNos[i] > randNos[j])
{
temp1 = randNos[i];
randNos[i] = randNos[j];
randNos[j] = temp1;
}
}
}
}

__________________________

Output:


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...
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
  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • read in numbers into array , print out, sort - my program reads in and prints...

    read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++ #include <iostream> using namespace std; void sortarray(int *tarray, int ); void displayarray(int *[], int); void averageshowarray(int *[], int); int main() {     int Size;     int count;     cout << "enter the size: " << endl;     cin >> Size;     int...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • a. Write a function named findmax() that finds and displays the maximum values in a two...

    a. Write a function named findmax() that finds and displays the maximum values in a two dimensional array of integers. The array should be declared as a 10 row by 20 column array of integers in main() and populated with random numbers between 0 and 100. b. Modify the function written above so that it also displays the row and column numbers of the element with the maximum value. I have this so far, and but it's only finding the...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

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