In: Computer Science
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;
}
#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; }