Hello Guys!! Everything about my code is working perfectly fine the only thing Im trying to figure out is why when I try to print 10 integers per line I am unable to do so! Under displayData I am unable to make it so only 10 integers per line is the output and it is organized neatly any help is appreciated! Thank you!
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
usingnamespace std;
//Function Prototypes
void arraySort(int[], int);
void displayData(int[], int);
int main()
{
//Declared Variables
srand(NULL);
int random[100];
int randomNumber;
int sum = 0;
double medianVal;
double average;
// Generates 100 random numbers from
//500-1000
for (int x = 0; x < 100; x++)
{
randomNumber = (rand() % 500) + 500;
random[x] = randomNumber;
}
// Calls arraySort function to sort in ascending order
arraySort(random, 100);
// Displays 100 random numbers sorted
cout << "Here are the sorted 100 numbers!\n" << endl;
displayData(random, 100);
// Prints smallest number from 100 random numbers
cout << "\nThe smallest number is: " << random[0] << endl;
// Prints largest number from 100 random numbers
cout << "\nThe largest number is: " << random[99] << endl;
// Median value calculation and then displays
// the median value
medianVal = (random[49]+random[50])/2;
cout << "\nThe median of all the values is: " << medianVal << endl;
// The sum of all 100 numbers
for (int i = 0;i<100; i++)
{
sum+=random[i];
}
cout << "\nThe sum of all the numbers is: " << sum << endl;
//average of all 100 numbers
average = sum/100;
cout << "\nThe average is: " << average << endl;
return 0;
}
// Function used to strt array by using a temp variable
// This swaps all numbers to put them in ascending order
void arraySort(int array[], int num)
{
int temp;
bool swap;
do
{
swap = false;
for (int x = 0; x < (num - 1); x++)
{
if (array[x] > array[x + 1])
{
temp = array[x];
array[x] = array[x + 1];
array[x + 1] = temp;
swap = true;
}
}
} while (swap);
}
// prints 100 random numbers from smallest to largest
void displayData(int array[], int num)
{
for (int c = 0; c < num; ++c)
cout << array[c] << " ";
if ((c+1) % 10 == 0){
cout << endl;
}
}
it worked for me, although I had to add in a couple brackets...
1 2 3 4 5 6 7 8 9 10
// prints 100 random numbers from smallest to largest
void displayData(int array[], int num)
{
for (int c = 0; c < num; ++c){//added bracket
cout << array[c] << " ";
if ((c+1) % 10 == 0){
cout << endl;
}
}//added bracket
}