Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- using namespace std;
- int * extractNumberArrayFromStream(stringstream& stream, int len);
- void sortArray (int * numbers, int arrayLength);
- int main() {
- // This is used to store the content of the first line
- string firstLine;
- //This is used to store the content of the second line
- string secondLine;
- // This is used for writing into a file
- ofstream fileToWrite;
- //This is used to read the file containing the numbers.
- ifstream fileToRead ("acces.in"); //Here, as a an argument we have to give the path to the file that we want to read
- //Read the file content
- if (fileToRead.is_open()) {
- //Read the first line and store it into the firstLine variable
- getline(fileToRead, firstLine);
- //Read the second line and store it into the secondLine variable
- getline(fileToRead, secondLine);
- //CLose the file. THIS IS VERY IMPORTANT!~
- fileToRead.close();
- }
- // By using this streams variable we are able to parse the string and
- // split it by space and extract the numbers from it
- stringstream streamOfFirstLine(firstLine);
- stringstream streamOfSecondLine(secondLine);
- //If the streamOfFirstLine contains "12 23"
- string intermediate;
- // Now we extract the 12 and store it as an integer into the numberOfDigits variable.
- //We use the stoi to convert a string into an integer
- getline(streamOfFirstLine, intermediate, ' ');
- int numberOfDigits = stoi(intermediate);
- //Now we read the 23 and do the same as before
- //Remember that the first line contains the n and m numbers
- getline(streamOfFirstLine, intermediate, ' ');
- int numberOfDigitsToBeRemoved = stoi(intermediate);
- // We use this function to do the same logic of extracting numbers from a string
- // Only that now we are also storing them into an array as there could be more than 2 numbers as we had before
- int * arr = extractNumberArrayFromStream(streamOfSecondLine, numberOfDigits);
- // We call this function to store the number
- //The logic is that if we sort it in descending order, we will have the largest number that we could obtain
- sortArray(arr,numberOfDigits);
- fileToWrite.open("acces.out");
- //We write all the numbers, to the output file with the except of the last m numbers
- int numberOfDigitsToBewritten = numberOfDigits - numberOfDigitsToBeRemoved;
- for (int i = 0; i < numberOfDigitsToBewritten; i++) {
- fileToWrite << arr[i] << endl;
- }
- fileToWrite.close();
- return 0;
- }
- //This function extracts a number array from a stream.
- // When you are seeing int *, it is the same as int[]
- // stringstream& is just a type, by using the & symbol, it means that we have to pass the address of the variable also
- int * extractNumberArrayFromStream(stringstream& str, int len) {
- // C++ does not allow dynamic allocation of arrays like c
- // In C we could have done directly int numbers[len] but it is not working in some versions of C++
- int * numbers;
- numbers = new (nothrow) int[len];
- //Here we save intermediate results read by the function getline.
- string intermediate;
- int i = 0;
- while (getline(str, intermediate, ' ')) {
- //stoi(intermediate) is just a function which knows to convert from "3" to 3, for example.
- //stoi comes from string to integer
- numbers[i++] = stoi(intermediate);
- }
- return numbers;
- }
- // This function simply sorts an array using the bubble sort algorithm
- void sortArray (int * numbers, int arrayLength) {
- for (int i = 0 ; i < arrayLength -1 ; i++) {
- for (int j = 0; j < (arrayLength - i - 1) ; j++) {
- int temp;
- if ( numbers[j] < numbers[j+1]) {
- temp = numbers[j];
- numbers[j] = numbers[j+1];
- numbers[j+1] = temp;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment