Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream> //for inputting a file
- using namespace std;
- int sumOfDataItems, numberOfDataItems, count, printRows, lastRow, rowCount, colCount;
- vector<int> v1;
- int numbersArray[1300];
- int readInput(int& totalNum)
- {
- ifstream inputFile; //File stream object
- int sum, data; //define variables
- inputFile.open("C:\\data\\TopicFin.txt"); //open the file
- if (!inputFile)//test for errors
- cout << "Cannot open TopicFin.txt. Program Ending\n";
- else
- {
- //initialize statistics
- totalNum = 0;
- sum = 0;
- //loop until the end of the file
- while (inputFile >> data)
- {
- v1.push_back(data);
- //compute statistics
- sum += data; //update sum
- totalNum++;
- }
- inputFile.close(); //close the input file
- return (sum);
- }
- }
- void sortArray(int array[], int size)
- {
- bool swap;
- int temp;
- do
- {
- swap = false;
- for (int count = 0; count < (size - 1); count++)
- {
- if (array[count] > array[count +1])
- {
- temp = array[count];
- array[count] = array[count +1];
- array[count + 1] = temp;
- swap = true;
- }
- }
- } while (swap);
- }
- int main()
- {
- sumOfDataItems = readInput(numberOfDataItems);
- if (sumOfDataItems == 0) //check for no data
- {
- cout << "Error - no input file data" << endl;
- }
- else
- {
- printRows = numberOfDataItems / 15;
- lastRow = numberOfDataItems % 15;
- cout << "The values read are:" << endl;
- for (rowCount = 0; rowCount < printRows; rowCount++)
- {
- for (colCount=0; colCount<15; colCount++)
- {
- cout << v1[count] << " ";
- count++;
- }
- cout << endl;
- }
- for (colCount=0; colCount<lastRow; colCount++)
- {
- cout << v1[count] << " ";
- count++;
- }
- cout << endl;
- for (count=0; count<numberOfDataItems; count++)
- numbersArray[count] = v1[count];
- sortArray( numbersArray, numberOfDataItems );
- cout << "\n\nThe average of values is " << (sumOfDataItems / numberOfDataItems) << endl;
- cout << numberOfDataItems << " Values were read" << endl << endl;
- cout << "The sorted result is\n";
- for (rowCount = 0; rowCount < printRows; rowCount++)
- {
- for (colCount=0; colCount<15; colCount++)
- {
- cout << v1[count] << " ";
- count++;
- }
- cout << endl;
- }
- for (colCount=0; colCount<lastRow; colCount++)
- {
- cout << numbersArray << " ";
- count++;
- }
- cout << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment