online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <fstream> #include <string> #include <string.h> #include <cstring> using namespace std; int const MAX = 10000; void findAndSort(int[], int[], int, int); void findAndSortStrings(string arr1[], string arr2[], int n1, int n2); //function for sorting strings void heapStrings(string arr[], int n, int i) { int largest = i; int lft = 2 * i + 1; int rgt = 2 * i + 2; if (lft < n && arr[lft] > arr[largest]) largest = lft; if (rgt < n && arr[rgt] > arr[largest]) largest = rgt; if (largest != i) { swap(arr[i], arr[largest]); heapStrings(arr, n, largest); } } //heap sorting algorithm for sorting strings void heapSortStrings(string arr[], int n) { for (int i = n / 2 - 1; i >= 0; i--) heapStrings(arr, n, i); for (int i = n - 1; i >= 0; i--) { swap(arr[0], arr[i]); heapStrings(arr, i, 0); } } //print the string array void printStringArray(string arr[], int n) { for (int i = 0; i < n; i++){ while(i<n-1 && arr[i]==arr[i+1]) i++; //for(int j=i+1; j<i;j++) //if(arr[i] == arr[j]) // break; cout << arr[i] <<endl;} cout<<endl; } //print the integer array void printArray(int arr[], int n) { for (int i = 0; i < n; i++){ while(i<n-1 && arr[i]==arr[i+1]) i++; //for(int j=i+1; j<i;j++) //if(arr[i] == arr[j]) // break; cout << arr[i] <<endl;} cout<<endl; } //sort the integer array void heapNum(int arr[], int n, int i) { int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < n && arr[l] > arr[largest]) largest = l; if (r < n && arr[r] > arr[largest]) largest = r; if (largest != i) { swap(arr[i], arr[largest]); heapNum(arr, n, largest); } } //Heap sort algorithm for integer array void heapSortNum(int arr[], int n) { for (int i = n / 2 - 1; i >= 0; i--) heapNum(arr, n, i); for (int i = n - 1; i >= 0; i--) { swap(arr[0], arr[i]); heapNum(arr, i, 0); } } int main(int argc, char* argv[]) { int arrSize1 = 0; int arr1[MAX]; int arrSize2 = 0; int arr2[MAX]; string strarr1[MAX]; string strarr2[MAX]; if (argc < 4) { cout << "Error: Incorrect usage. Try again..\n"; return -1; } string type = argv[1]; //check for integer array if (type == "i") { ifstream myfile(argv[2]); if (myfile.is_open()) { while (true) { int x; myfile >> x; if (myfile.eof()) break; arr1[arrSize1++] = x; } cout << "File " << argv[2] << " contains:" << endl; printArray(arr1, arrSize1); } else { cout << "Can't open file"; } ifstream myfile1(argv[3]); if (myfile1.is_open()) { while (true) { int x; myfile1 >> x; if (myfile1.eof()) break; arr2[arrSize2++] = x; } cout << "File " << argv[3] << " contains:" << endl; printArray(arr2, arrSize2); } else { cout << "Can't open file"; } findAndSort(arr1, arr2, arrSize1, arrSize2); //check for string array } else if (type == "s") { fstream file; string word, t, q, filename; filename = argv[2]; file.open(filename.c_str()); while (file >> word) { strarr1[arrSize1++] = word; } cout << "File " << argv[2] << " contains:" << endl; for (int i = 0; i < arrSize1; ++i) cout << strarr1[i] << " "<<endl; cout << endl; fstream file1; string filename1, word1; filename1 = argv[3]; file1.open(filename1.c_str()); while (file1 >> word1) { strarr2[arrSize2++] = word1; } cout << "File " << argv[3] << " contains:" << endl; for (int i = 0; i < arrSize2; ++i) cout << strarr2[i] << " "<<endl; cout << endl; findAndSortStrings(strarr1, strarr2, arrSize1, arrSize2); } else { //prints error message when the input is not i or s cout << "Error: Incorrect usage. Please enter i OR s" << endl; } return 0; } //finding duplicates and sorting void findAndSort(int arr1[], int arr2[], int n1, int n2) { int a[MAX]; int i = 0, j = 0, k = 0; for (i = 0; i < n1; i++) { for (j = 0; j < n2; j++) { if (arr1[i] == arr2[j]) { a[k] = arr2[j]; k++; } } } heapSortNum(a, k); cout << "Array after sorting is: " << endl; printArray(a, k); } //finding duplicates and sorting strings void findAndSortStrings(string arr1[], string arr2[], int n1, int n2) { string a[MAX]; int i = 0, j = 0, k = 0; for (i = 0; i < n1; i++) { for (j = 0; j < n2; j++) { if (arr1[i] == arr2[j]) { a[k] = arr2[j]; k++; } } } heapSortStrings(a, k); cout << "Array after sorting is: " << endl; printStringArray(a, k); cout << endl; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue