"Number" is the integer the user chooses for the number of values in an array (min is 3, max is 100). The random number generator function generates numbers 1-100, so they key is 1-100.
I'm not sure I want to return the exact position value. For example, if the pos returned is 20, then it would add 20 to the percent value, there by raising the percent by 20% instead of 1%. That's why I was trying to return 1, if the value was found.
Here's the whole code, it generates 100 random numbers that are 1-100 that is stored in the array, then the user is prompt for how many numbers are used.
Then array is displayed, followed by the average of the array, then the amount of numbers in the array that are above the average,
Then 3 consecutive values that yield the highest average are displayed, followed by the percent of unique numbers 1-100 in the array (Note: if the numbers in the array was choose to be 10, then the max percent is 10%, if it was 92 it would be 92%.)
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;
const int size = 100;
void fillArray(int [size], int);
void printArray(int[size], int);
double average(int[size], int);
int countAboveAverage( int[size], int, double);
void printConsecutive( int [size], int);
double coverage( int [size], int);
int search ( int [size], int, int);
void main()
{
int list[size];
int number;
double avgNum;
int aboveAvg;
double percent;
cout <<"Please enter an integer that is between 3 and 100: "<< endl;
cin >> number;
fillArray( list, number);
printArray(list, number);
avgNum = average(list, number);
aboveAvg = countAboveAverage(list, number, avgNum);
cout << "The average of the numbers is: " << avgNum << endl;
cout << "The amount of numbers that are above the average is: "<< aboveAvg << endl;
printConsecutive( list, number);
percent = coverage(list, number);
cout << "The percent is: " << percent << endl;
}
void fillArray (int array[size], int number)
{
srand((unsigned)time(0));
for(int i=0; i<number; i++)
array[i] = (rand()%100)+1;
}
void printArray( int array[], int number)
{
for (int i=0; i<number; i++)
{
cout<< array[i];
if( (i % 10) == 9)
cout << endl;
else
cout << ", ";
}
}
double average ( int array[], int number)
{
double avg;
avg = 0;
for (int i = 0; i <number; i++)
avg = avg + array[i];
avg = avg/number;
return avg;
}
int countAboveAverage( int array[], int number, double average)
{
int total;
total = 0;
for (int i = 0; i <number; i++)
{
if (array[i] > average)
total++;
}
return total;
}
void printConsecutive(int array[], int number)
{
int largeAvg;
int index;
index = 0;
largeAvg = (array[0] + array[1] + array[2])/3;
for (int i = 1; i < number -2; i++)
{
if ( (array[i] + array[i + 1] + array[i + 2])/3 > largeAvg)
{
largeAvg = (array[i] + array[i+1] + array[1+2])/3;
index = i;
}
}
cout << "The three consecutive numbers with the largest average is: "
<< index << ", " << index+1 << ", " << index+2 << endl;
}
double coverage (int array[], int number)
{
int key;
int percent;
percent = 0;
for( key = 1; key<100; key++)
{
percent = percent + search(array, number, key);
}
return percent;
}
int search ( int array[], int number, int key )
{
int pos = 0;
while(pos < number && array[pos] != key)
pos++;
if( pos == number)
pos = 0;
else
pos = 1;
return 1;
}
Edit 1: I edit to post to update my print array function, it does what its supposed to now.