Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Does same as java solution but in c++
- Test file for you
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 35
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 86 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- 85 85 85 85 85 85 85 85 85 85
- */
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <sstream>
- class FileToArray {
- private:
- std::vector<std::vector<int>> array;
- public:
- FileToArray(const std::string& filePath) {
- std::ifstream inputFile(filePath);
- if (!inputFile.is_open()) {
- std::cerr << "Error: Could not open file" << std::endl;
- return;
- }
- array.resize(20, std::vector<int>(10));
- std::string line;
- std::getline(inputFile, line);
- std::istringstream lineStream(line);
- int value;
- for (int i = 0; i < 20; i++) {
- for (int j = 0; j < 10; j++) {
- if (lineStream >> value) {
- array[i][j] = value;
- } else {
- std::cerr << "Error: Not enough data in the file." << std::endl;
- inputFile.close();
- return;
- }
- }
- }
- inputFile.close();
- }
- void compareAndPrintMismatches(int selectedNumber) {
- for (int row = 0; row < 20; row++) {
- for (int col = 0; col < 10; col++) {
- int number = array[row][col];
- if (number != selectedNumber) {
- std::cout << "Mismatch: Row " << (row + 1) << ", Column " << (col + 1) << " - Value: " << number << std::endl;
- }
- }
- }
- }
- int countElements() {
- return 20 * 10;
- }
- void exportToFile(const std::string& outputPath, const std::string& delimiter) {
- std::ofstream outputFile(outputPath);
- if (!outputFile.is_open()) {
- std::cerr << "Error: Could not open output file" << std::endl;
- return;
- }
- for (int row = 0; row < 20; row++) {
- for (int col = 0; col < 10; col++) {
- outputFile << array[row][col];
- if (col < 9) {
- outputFile << delimiter;
- }
- }
- outputFile << "\n";
- }
- outputFile.close();
- }
- };
- int main() {
- std::string inputFilePath = "numbers.txt"; // Change this to the path of your input file
- FileToArray fileToArray(inputFilePath);
- int selectedNumber = 85; // Change this to the number you want to compare
- fileToArray.compareAndPrintMismatches(selectedNumber);
- int elementCount = fileToArray.countElements();
- std::cout << "Total number of elements in the array: " << elementCount << std::endl;
- std::string outputFilePath = "output.txt"; // Change this to the desired output file path
- std::string delimiter = "\t"; // Change this to the desired delimiter
- fileToArray.exportToFile(outputFilePath, delimiter);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement