Question

In: Computer Science

The program will prompt the user to enter a number between [10 .. 20] (inclusive). After...

The program will prompt the user to enter a number between [10 .. 20] (inclusive). After verifying that the number is within the proper range, you will generate that many random numbers and place them into a dynamically allocated array. Your program will then display the contents of the array (with 4 numbers per line). Next, you will sort the values in descending order (from largest to smallest). Lastly, you will display the sorted numbers (again, with 4 per line).

For my code have the bugs, when i compile that.please help me to check that

#include<iostream>
#include<cstring>
#include<ctime>
#include<cstdlib>
using namespace std;


void introduction(){

cout << " Sorting Random Integers Programmed by Jingwei Geng" << endl;
cout << " This program generates random numbers in the range [10 .. 20]," << endl;
cout << "displays the original list, and sorts the list in" << endl;
cout << "in descending order. Lastly, the sorted list is displayed to the user."<< endl;

}


void get_num(int *num_ptr )

{

cout<< " How many numbers should be generated? [10..20]," <<endl;

cin>> *num_ptr;

do{

if((*num_ptr < 10) ||( *num_ptr > 20))
cout<< "Invalid input"<<endl;

}while(*num_ptr >= 10 && *num_ptr <= 20);

}


void fill_array(int array_size, int *array)
{

for(int i=0; i< array_size; i++)
{
*(array + i) =rand()%(900)+100;// the number range is from [100, 999]
}
}
/*
*
* */
void sort_list(int array_size,int *array)
{


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

for(int k=i+1; k<array_size; k++){

if( *array[k]>array[i]){

exchange((array[i+1]),(array[i]));
}//if

}//for

}//for

}//void


void exchange(int *a,int *b)
{
int temp;

   temp = *a;
   *a = *b;
   *b = temp;

}

void display_list(int array_size, int *array)
{
for(int i=0; i< array_size,i++;){

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

}
cout <<endl;

}

int main(){

introduction();

   int *num_ptr;
   int *array;
   get_num(num_ptr);

   array = new int[*num_ptr];
   fill_array(*num_ptr,array);

       srand(time(NULL));

       cout<< "The unsorted random numbers:" <<endl;
       display_list(*num_ptr, array);
       cout<< "The sorted list:" <<endl;
       display_list(*num_ptr, array);
       cout<< " Thanks for using my program!" <<endl;

   delete[] array;
      
  


}

Solutions

Expert Solution

#include<iostream>
#include<cstring>
#include<ctime>
#include<cstdlib>

using namespace std;

void introduction() {
    cout << " Sorting Random Integers Programmed by Jingwei Geng" << endl;
    cout << " This program generates random numbers in the range [10 .. 20]," << endl;
    cout << "displays the original list, and sorts the list in" << endl;
    cout << "in descending order. Lastly, the sorted list is displayed to the user." << endl;
}

void get_num(int *num_ptr) {
    cout << " How many numbers should be generated? [10..20]," << endl;
    cin >> *num_ptr;
    do {
        if ((*num_ptr < 10) || (*num_ptr > 20))
            cout << "Invalid input" << endl;
    } while (*num_ptr >= 10 && *num_ptr <= 20);
}

void fill_array(int array_size, int *array) {
    for (int i = 0; i < array_size; i++) {
        *(array + i) = rand() % (900) + 100;// the number range is from [100, 999]
    }
}

void exchange(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void sort_list(int array_size, int *array) {
    for (int i = 0; i < array_size; i++)
        for (int j = 0; j < array_size - 1; j++)
            if (array[j] < array[j + 1])
                exchange(array + j, array + j + 1);
}

void display_list(int array_size, int *array) {
    for (int i = 0; i < array_size; i++) {
        cout << " " << array[i];
    }
    cout << endl;
}

int main() {
    introduction();
    int num_ptr;
    int *array;
    get_num(&num_ptr);

    array = new int[num_ptr];
    fill_array(num_ptr, array);

    srand(time(NULL));

    cout << "The unsorted random numbers:" << endl;
    display_list(num_ptr, array);
    sort_list(num_ptr, array);
    cout << "The sorted list:" << endl;
    display_list(num_ptr, array);
    cout << " Thanks for using my program!" << endl;
    delete[] array;
}

Related Solutions

Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Program IN C Prompt the user to enter month number and year. Retain an appropriate response...
Program IN C Prompt the user to enter month number and year. Retain an appropriate response for invalid month numbers, but don’t worry about validity of year. 2.Determine the number of days in the month entered (ignore leap years). 3.Use the answer to step 2 in one FOR-LOOP to ask the user to enter a FAHRENHEIT temperature (integer or floating) for each day of the month utilizing a prompt that includes the DATE. a. Prompt the user to enter the...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0. using for-loop
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT