1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
//Alexandros Panayi
//CS 003A Random Number Program
//Done with Visual Studio 2013
//QT creator not downloaded yet
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void createList(int *&list, int &size); //create pointer variable int refering to address list
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, string title);
void swap(int *x, int *y);
void getMin(int list[], int size); //obtain min value
void getMax(int list[], int size); //obtain max value
int getMean(int list[], int size); //obtain mean value
double getSD(int list[], int size, int average); //obtain mean and standard deviation
int main()
{
//QCoreApplication a(argc, argv);
srand(time(NULL));
int size, *list;
double standard;
int average;
createList(list, size);
average = getMean(list, size);
cout << "The average is " << average << endl;
standard = getSD(list, size, average);
cout << "The Standard Deviation is " << standard << endl;
delete[] list; //deallocates memory pointed by list, allowing storage space previously used to be called by new operator
system("PAUSE"); //visual pause operator
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]; //allocating new memory 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);
getMean(list, size);
display(list, size, "your data!");
sort(list, size, "Sorting!");
getMin(list, size);
getMax(list, size);
}
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, string title)
{
cout << title << endl;
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;
}
void getMin(int list[], int size)
{
int minValue = list[0];
for (int i = 0; i < size; i++)
{
if (list[i] < minValue)
{
minValue = list[i];
}
}
cout << "The lowest value is " << minValue << endl;
}
void getMax(int list[], int size)
{
int maxValue = list[0];
for (int i = 0; i <size; i++)
{
if (list[i] > maxValue)
{
maxValue = list[i];
}
}
cout << "The max value is " << maxValue << endl;
}
double getSD(int list[], int size, int average)
{
double sd;
double sdfactor;
sdfactor = 0;
for (int j = 0; j < size; j++)
{
sdfactor = sdfactor + list[j] - average; //subtract mean from each element in array
sd = sqrt(pow(sdfactor, 2) / size); //square difference of mean from each element divided by total size of array over squareroot
}
return sd;
}
int getMean(int list[], int size)
{
int mean = list[0];
for (int i = 0; i < size; i++)
{
mean = mean + list[i];
}
mean = mean / size;
return mean;
}
|