I'm new to C++ and wanted some help please. Can anyone tell me where and how to put a table that organizes the output of the array in 5 columns. Thank you
int sum(int a[], int n)
{
int total=0;
for (int i=0; i<n; i++)
total += a[i];
return total;
}
int sumOfSquare(int a[], int n)// return the sum of the square of each element from integer array a of size n
{
int total = 0;
for (int i=0; i<n; i++)
total += a[i] * a[i];
return total;
}
int max(int a[] , int n) // return the largest element of the array a of size n
{
int maximum = a[0];
for (int i=0; i<n; i++)
{
if(a[i] > maximum)
{
maximum = a[i];
}
}
return maximum;
}
int min(int a[], int n) // return the smallest element of the array a of size n
{
int minimum = a[0];
for(int i = 0; i < n; i++)
{
if(a[i] < minimum)
{
minimum = a[i];
}
}
return minimum;
}
void maxmin(int a[], int n , int &maxima, int & minima) // find the max and min from the given array a of size n
{
maxima = max(a,n);
minima = min(a,n);
}
double avg(int a[], int n) // compute the average of n data storing in the array a
double stdev(int a[], int n) // compute the standard deviation of n data storing in the array a
{
double standdev = 0;
standdev = sqrt(var(a, n));
return standdev;
}
void swap(int a[], int index1, int index2)
{
int temp = a[index1];
a[index1] = a[index2];
a[index2] = temp;
}
void selectionSort(int a[], int n) // sort the array a of size n using selectionSort algorithm
{
int pos_min = 0;
for (int unsortedEnd=0; unsortedEnd<n-1; unsortedEnd++)
{
pos_min = unsortedEnd;
for(int i = unsortedEnd+1; i<n; i++)
{
if (a[i] < a[pos_min])
pos_min = i;
}
if (pos_min != unsortedEnd)
{
int temp = a[pos_min];
a[pos_min] = a[unsortedEnd];
a[unsortedEnd] = temp;
}
}
}
bool isSorted(int a[], int n) // check to see if the integer array a of size n is orted in ascending order or not
{
for(int i = 1; i < n; i++)
{
if(a[i-1] > a[i])
{
return false;
}
}
return true;
}
bool gen(int a[], int n, int low = 0 , int up= 100) // randomly generates n integer between low and up (inclusively) and store in the array a
{
int range = up - low + 1;
for( int i = 0; i < n; i++ )
{
a[i] = low+rand()%range;
}
return true;
}
void print(int a[], int n, int k = 1) // display the content of the integer array a of size n , k elements per
{
int rowsNum = (int)ceil( (double)n/(double)k );
int displayedCount = 0;
for(int row = 0; row < rowsNum; row++)
{
for(int col = 0; col<k; col++)
{
if (displayedCount >= n)
break;
cout << a[row*k+col] << "\t";
displayedCount++;
}
cout << endl;
}
}
string dup(string s, int n ) // return a string which is a n-copy of s, e.g. dup("hello ", 3) returns "hello hello hello "
{
string result = "";
for (int i=0; i<n; i++)
result += s;
return result;
}
string dup(char c, int n ) // return a string which is a n-copy of c, e.g. dup('A',5) returns "AAAAA"
{
string result = "";
for (int i=0; i<n; i++)
result += c;
return result;
}
void stat(int a[], int n) // display the statistics of the integer array a of size n
{
cout << "Data size: " << n << endl;
cout << "Minimum: " << min(a, n) << endl;
cout << "Maximum: " << max(a, n) << endl;
cout << "Average: " << avg(a, n) << endl;
cout << "Variance: " << var(a, n) << endl;
cout << "Standard Deviation: " << stdev(a, n) << endl;
}
void histogram(int a[], int n) // display the histogram of the integer data array a of size n
{
selectionSort(a, n);
for(int i=0; i<n;)
{
int value = a[i];
cout << a[i] << ":";
do {
cout << "*";
i++;
if(i>=n)
break;
} while(a[i] == value);
cout << endl;
}
}
void displayArray(int a[], int sizeOfArray)
{
for(int i = 0; i < sizeOfArray; i++)
cout << a[i] << endl;
}
int main ()
{
srand ((unsigned) time(NULL));
int num [300];
int N;
char answer[10];
cout <<" Please enter an interger between 1 and 300 (inclusively): " ;
cin >>N;
gen (num,N);
cout << "\nDo you want to see the content of the data set being generated? (Y/N) " ;
cin >> answer;
if (answer [0] == 'Y')
cout<< "\nDo you want to see the content of the data generated in sorted order(Y/N): ";
cin >> answer;
if (answer [0] == 'Y')
selectionSort (num,N);
isSorted (num, N);
print (num, N);
cout << "\n Do you want to see the histogram of the data set being generated? ( Y/N) :";
cin >> answer;
if (answer [0] == 'Y')
histogram (num, N) ;
cout <<"\nDo you want to see the statistics of the data set being generated? ( Y/N) :";
cin >> answer;
if (answer [0] == 'Y')
stat (num,N);
cout << " Bye, have a nice day!!" <<endl;
if (answer [0] == 'N')
cout << " Bye, have a nice day!!" <<endl;