Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <ctime>
  5. #include <cstdlib>
  6. #include <string>
  7. using namespace std;
  8.  
  9. // Global constant for the array size
  10. const int ARRAY_SIZE = 1200;
  11.  
  12. // Function prototypes
  13. void writeNumbers(ofstream &, int[], const int);
  14. int getLowest(int[], int);
  15. int getHighest(int[], int);
  16. int getSum(int[], int);
  17. double getAverage(int[], int);
  18. int getRand(int, int);
  19. void fillArray(int[]);
  20.  
  21. int main()
  22. {
  23.     int highest;
  24.     ofstream outFile;
  25.     string randomnumbers;
  26.     cout << "Enter the filename \n";
  27.     cin >> randomnumbers;
  28.     srand(time(NULL));
  29.     int iarray[ARRAY_SIZE];
  30.     fillArray(iarray);
  31.     writeNumbers(outFile, iarray, ARRAY_SIZE);
  32.     int sum = getSum(iarray, ARRAY_SIZE);
  33.     cout << "This is the sum of the numbers " << sum << "\n";
  34.     highest = getHighest(iarray, ARRAY_SIZE);
  35.     cout << "This is the highest number " << highest << "\n";
  36.     return 0;
  37. }
  38. void writeNumbers(ofstream &, int iarray[], const int ARRAY_SIZE)
  39. {
  40.     ofstream outFile;
  41.     string randomnumbers;
  42.  
  43.     outFile.open("randomnumbers.txt");
  44.  
  45.     for (int n=0;n<ARRAY_SIZE-1;n++)
  46.     outFile << iarray[n] << "\n";
  47.    
  48. }
  49. int getLowest(int[], int)
  50. {
  51.     int lowest = 0;
  52.     return lowest;
  53. }
  54. int getHighest(int iarray[], int ARRAY_SIZE)
  55. {
  56.         int highest = 0;
  57.         int first = 0,last = ARRAY_SIZE - 1, middle, position = -1;        
  58.         bool found = false;        // Flag  
  59.         while (!found && first <= last)  
  60.         {      
  61.             middle = (first + last) / 2;     // Calculate mid point
  62.             if (iarray[middle] == highest)      // If value is found at mid      
  63.             {        
  64.                 found = true;        
  65.                 highest = middle;
  66.                 return highest;
  67.             }    
  68.             else if (iarray[middle] > highest)  // If value is in lower half      
  69.                 last = middle - 1;  
  70.             else        
  71.                 first = middle + 1;      
  72.             // If value is in upper half  
  73.         }  
  74.         return highest;
  75. }
  76.  
  77.  
  78. int getSum(int iarray[], int)
  79. {
  80.     int sum, total=0;
  81.     for (int n = 0; n < ARRAY_SIZE - 1; n++)
  82.     {
  83.         int total=0, sum=0;
  84.         int val = iarray[n];
  85.         total = val + total;
  86.     }
  87.     return total;
  88. }
  89. double getAverage(int[], int)
  90. {
  91.     double average = 1230.21;
  92.     return average;
  93. }
  94. int getRand(int lowestNum, int highestNum)
  95. {
  96.     int j;
  97.     j = (rand() % (highestNum+1));
  98.     while (j < lowestNum)
  99.     {
  100.         j = (rand() % (highestNum+1));
  101.     }
  102.     cout << j << "\n";
  103.     return j;
  104. }
  105. void fillArray(int iarray[])
  106. {
  107.     iarray[ARRAY_SIZE];
  108.     int highestNum, lowestNum;
  109.     cout << "What is the Lowest number you want in the list?\n";
  110.     cin >> highestNum;
  111.     cout << "\n " << "What is the Highest number you want in the list?. \n";
  112.     cin >> lowestNum;
  113.     for (int i = 0; i < ARRAY_SIZE - 1; i++)
  114.     {
  115.         iarray[i] = getRand(highestNum, lowestNum);
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement