#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void createList(int list[], int &size); //create list of array
int getInput(int min, int max, string title); //create input function
void loadArray(int *list, int size, int min, int max); //create construction of array
void display(int list[], int size, string title); //display function
void sort(int *list, int size);
void swap(int *x, int *y);
int main()
{
//QCoreApplication a(argc, argv);
srand(time(NULL));
int size, *list;
createList(list,size);
display(list,size, "Your data!");
return 0;
}
void display(int list[], int size, string title)
{
cout<<title<<endl;
for(int i = 0; i < size; i++)
{
if(i % 10 == 0) //used to create 10 rows, move onto next row
cout<<endl;
cout<<setw(5)<<*(list + i);//set width of 5 spaces
}
if(size%10 == 0)
cout<<endl;
cout<<endl;
}
void createList(int list[], int &size)
{
size = getInput(0,1000000, "What is the size of the array you want? ");
list = new int[size];
int max =(unsigned(~0) >> 1); //unsign sin bit, shift right 1
int min;
min = -max;
min = getInput(min, max, "What is the smallest value you wish to see in the array? ");
max = getInput(min, max, "What is the maximim value you wish to see in the array? ");
loadArray(list, size, min, max);
}
int getInput(int min, int max, string title)
{
int value;
bool again = true;
do
{
cout<<title;
cin>>value;
if(value<min || value > max)
cout<<"Illegal input. Your entry must be between "<<min<<" and "<<max<<endl
<<"Please re-enter"<<endl;
else
again = false;
}while(again);
return value;
}
void loadArray(int *list, int size, int min, int max)
{
int num;
for(int i = 0; i < size; i++)
{
while((num = (int)pow(-1.,rand()%2)*rand()) < min || num > max);
*(list+i) = num;
}
}
void sort(int *list, int size)
{
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++)
if(*(list+i) < *(list+j))
swap(list+i, list+j);
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
} |