Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <ctime>
- #include <cstdlib>
- #include <string>
- using namespace std;
- // Global constant for the array size
- const int ARRAY_SIZE = 1200;
- // Function prototypes
- void writeNumbers(ofstream &, int[], const int);
- int getLowest(int[], int);
- int getHighest(int[], int);
- int getSum(int[], int);
- double getAverage(int[], int);
- int getRand(int, int);
- void fillArray(int[]);
- int main()
- {
- int highest;
- ofstream outFile;
- string randomnumbers;
- cout << "Enter the filename \n";
- cin >> randomnumbers;
- srand(time(NULL));
- int iarray[ARRAY_SIZE];
- fillArray(iarray);
- writeNumbers(outFile, iarray, ARRAY_SIZE);
- int sum = getSum(iarray, ARRAY_SIZE);
- cout << "This is the sum of the numbers " << sum << "\n";
- highest = getHighest(iarray, ARRAY_SIZE);
- cout << "This is the highest number " << highest << "\n";
- return 0;
- }
- void writeNumbers(ofstream &, int iarray[], const int ARRAY_SIZE)
- {
- ofstream outFile;
- string randomnumbers;
- outFile.open("randomnumbers.txt");
- for (int n=0;n<ARRAY_SIZE-1;n++)
- outFile << iarray[n] << "\n";
- }
- int getLowest(int[], int)
- {
- int lowest = 0;
- return lowest;
- }
- int getHighest(int iarray[], int ARRAY_SIZE)
- {
- int highest = 0;
- int first = 0,last = ARRAY_SIZE - 1, middle, position = -1;
- bool found = false; // Flag
- while (!found && first <= last)
- {
- middle = (first + last) / 2; // Calculate mid point
- if (iarray[middle] == highest) // If value is found at mid
- {
- found = true;
- highest = middle;
- return highest;
- }
- else if (iarray[middle] > highest) // If value is in lower half
- last = middle - 1;
- else
- first = middle + 1;
- // If value is in upper half
- }
- return highest;
- }
- int getSum(int iarray[], int)
- {
- int sum, total=0;
- for (int n = 0; n < ARRAY_SIZE - 1; n++)
- {
- int total=0, sum=0;
- int val = iarray[n];
- total = val + total;
- }
- return total;
- }
- double getAverage(int[], int)
- {
- double average = 1230.21;
- return average;
- }
- int getRand(int lowestNum, int highestNum)
- {
- int j;
- j = (rand() % (highestNum+1));
- while (j < lowestNum)
- {
- j = (rand() % (highestNum+1));
- }
- cout << j << "\n";
- return j;
- }
- void fillArray(int iarray[])
- {
- iarray[ARRAY_SIZE];
- int highestNum, lowestNum;
- cout << "What is the Lowest number you want in the list?\n";
- cin >> highestNum;
- cout << "\n " << "What is the Highest number you want in the list?. \n";
- cin >> lowestNum;
- for (int i = 0; i < ARRAY_SIZE - 1; i++)
- {
- iarray[i] = getRand(highestNum, lowestNum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement