老实说,我不知道如何从动态数组编号生成器创建直方图,并且我无法对数组进行排序,我对数组进行了排序但输出了整数的地址而不是输出随机数生成数据.......
提前感谢您的帮助。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void histogram(int a[], int n, int min, int max);
void draw(char c, int n);
int *gen(int n, int low, int high);
void display(int a[], int size);
void sort(int a[], int number);
void swap(int& v1, int& v2);
int indexofsmallest(const int a[], int start, int number);
int main()
{
int n;
int low;
int high;
char resp[20];
int *data;
while(true)
{
cout << "\nPlease enter a positive integer : ";
cin >> n;
cout << "\nPlease enter a lower bound : ";
cin >> low;
cout << "\nPlese enter an upper bound : ";
cin >> high;
cout << endl;
data = gen(n, low, high);
cout << " Do you want to see the random data?\n " ;
cin >> resp;
cout << "\n";
if (resp[0] == 'y' || resp[0] == 'Y')
{
display(data, n);
cout << "\n";
system("pause");
cout << "\n";
}
cout << "\nDo you want to see the data organized? " << endl;
cin >> resp;
if (resp[0] == 'y' || resp[0] == 'Y')
{
sort(data, n);
cout << " In sorted order the data is " << endl;
for (int index = 0; index < n; index++)
cout <<
system("pause");
cout << "\n";
}
cout << "\nDo you want to see the histogram of the data? " << endl;
cin >> resp;
if (resp[0] == 'y' || resp[0] == 'Y')
{
histogram(data, n, low, high);
system("pause");
cout << "\n";
}
delete [] data;
cout << "\nDo you want to try another value? " ;
cin >> resp;
if (resp[0] == 'y' || resp[0] == 'Y') continue;
else break;
}
return 0;
}
void draw(char c, int n) //displays char c, n times to the screen
{
if (n == 0) return;
cout << c;
draw(c, n-1);
}
void histogram(int a[], int n, int min, int max)
{
}
int *gen(int n, int low, int high)
{
srand(time(NULL));
int *temp = new int[n];
for (int i = 0; i < n; i++)
{
temp[i] = (rand()%(high - low) + low);
}
return temp;
}
void display(int a[], int n)
{
for(int i = 0; i < n; i++)
{
cout << a[i] << " ";
if ((i+1) % 5 == 0) cout << endl;
}
}
void sort(int a[], int number)
{
int indexofnextsmallest;
for (int index = 0; index < number - 1; index++)
{
indexofnextsmallest = indexofsmallest(a, index, number);
swap(a[index], a[indexofnextsmallest]);
}
}
void swap(int&v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int indexofsmallest(const int a[], int start, int number)
{
int min = a[start], indexofmin = start;
for (int index = start+1; index < number; index++)
if (a[index] < min)
{
min = a[index];
indexofmin = index;
}
return indexofmin;
}