I need this program to prompt the user to enter a filename (a .txt file), find the specified file, read the contents of the file (any amount of numbers separated by new lines), create an array with the exact amount of memory to fit each of the numbers in the file, put every number from the file into it's own spot in the array, compare each of these numbers (which are each assigned to their own spot in the array) to each other to find and cout the highest number, lowest number, sum of all the numbers, and average of all the numbers.
In short:
1. get filename from user(.txt file)
2. program finds it and reads it (will contain a bunch of numbers)
3. makes an array with as many spots of memory for each number in the file
4. assign each number to a spot in the newly created array
5. compare all of the numbers in the array with each other to find and cout the total amount, lowest, highest, sum, and average of the numbers.
I have looked around to see if someone had asked the same question as I am, but I could only find
http://www.cplusplus.com/forum/general/14574/ which did not help me.
Everything contained in the following code is pretty much the extent of what I know, so please refrain from modifying the code to where I wont understand it. I think I am simply missing a conceptual aspect to functions in regards to how data is passed into them or pulled/called out of them from main.
Please help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <iostream>
#include <string>
#include <fstream>
#include <climits>
using namespace std;
int getSize(string fileName)
{
int cnt = 0;
int throwAway;
ifstream inputFile;
inputFile.open(fileName);
while (inputFile >> throwAway)
{
cnt++;
}
inputFile.close();
return cnt;
}
void readNumbers(string fileName, int nums[], const int size)
{
ifstream inputFile;
inputFile.open(fileName);
int cnt = 0;
while ((cnt < size) && (inputFile >> nums[cnt]))
{
cnt++;
}
inputFile.close();
}
int getLowestNum(int nums[], int size)
{
int lowest = INT_MIN;
for (int i = 0; i > size; i++)
{
if (i < lowest)
{
lowest = i;
}
}
return lowest;
}
int getHighestNum(int nums[], int size)
{
int highest = INT_MAX;
for (int i = 0; i < size; i++)
{
if (i > highest)
{
highest = i;
}
}
return highest;
}
int calcSum(const int nums[], const int size)
{
int sum = 0;
for (int cnt = 0; cnt < size; cnt++)
{
sum += nums[cnt];
}
return sum;
}
int getAverageNum()
{
}
int main()
{
string fileName;
int totalSize = getSize(fileName);
int numbers[totalSize];
int lowestNum = getLowestNum(size);
int highestNum = getHighestNum();
int totalNum = calcSum();
double averageNum = getAverageNum();
cout << "Please enter the name of the file: ";
cin >> fileName;
cout << "\nThere are " << totalSize << " numbers in this file." << endl;
cout << "The lowest number is this file is: " << lowestNum << endl;
cout << "The highest number in this file is: " << highestNum << endl;
cout << "The sum of the numbers in the file is: " << averageNum << endl;
cout << "The average of these numbers is: " << totalNum << endl;
char c;
cout << "\nPress any key and ENTER to exit: ";
cin >> c;
return 0;
}
|