c++ - Can these two functions be combined into one? -
i've finished working on mini task that's described in comment block in code below, i've been trying make code better combining getraredigits
, displayraredigits
1 function. no matter do, logic ends breaking. can explain me if it's possible these 2 functions combined? ^_^
/* written by: stephanie yumiko * program ask user series of integers, defined user. * program display user number of rare digits, digits occur once in single integer, not rest. * program sort integers based on number of occurrences of rare digits contains, greatest least. */ #include <iostream> using namespace std; bool num_contains(int, int); void showraredigits(int*, int); void sortraredigits(int*, int* , int); bool num_contains(int digit, int n) { while (n) { if (digit == n % 10) return true; n /= 10; } return false; } void getraredigits(int *arr, int *ordered, int len) { (int index = 0; index < len; ++index) { int n = arr[index]; if (n < 0) n *= -1; int d = 0; while (n) { d = n % 10; int i; // keep track of loop counter outside loop int stop = 0; // break out loop (i = 0; < len; ++i) { if (i != index && num_contains(d, arr[i])) stop = 1; } // increment array if loop exited before // completing (implying goto have happened) if (!stop) { ++ordered[index]; } // execute n /= 10; } } (int = 0; i<len; i++) { (int j = 0; j<len - - 1; j++) { if (ordered[j]<ordered[j + 1]) { int temp = ordered[j]; ordered[j] = ordered[j + 1]; ordered[j + 1] = temp; int temp2 = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp2; } } } cout << "\narray after sort:\n"; (int = 0; < len; i++) { cout << arr[i] << endl; } } void showraredigits(int* iary, int size) { const int size2 = 10; int* tmpary = new int[size]; int totalcount[size2] = { 0 }; int currentcount[size2] = { 0 }; int totaluncommon = 0; int i, j; int* ordered; ordered = new int[size]; (i = 0; < size; i++) { ordered[i] = 0; tmpary[i] = iary[i]; if (tmpary[i] < 0) tmpary[i] *= -1; (j = 0; j < size2; j++) currentcount[j] = 0; if (tmpary[i] == 0) { currentcount[0] = 1; } while (tmpary[i] / 10 != 0 || tmpary[i] % 10 != 0){ currentcount[tmpary[i] % 10] = 1; tmpary[i] /= 10; } (j = 0; j < size2; j++) { totalcount[j] += currentcount[j]; } } (i = 0; < size2; i++) { if (totalcount[i] == 1) { totaluncommon++; } } cout << "\ntotal rare digits: " << totaluncommon << endl << "\nthe rare digits:\n"; if (totaluncommon == 0) { cout << "\nno rare digits found."; } else { (i = 0; < size2; i++) { if (totalcount[i] == 1) { cout << << endl; } } } getraredigits(iary, ordered, size); delete[] tmpary; delete[] ordered; return; } int main() { int size; int* arr; cout << "enter # of integers: "; cin >> size; arr = new int[size]; (int = 0; < size; i++) { cout << "enter value #" << << " : "; cin >> arr[i]; } cout << "array before sorting:\n"; (int = 0; < size; i++) { cout << arr[i] << endl; } showraredigits(arr, size); delete[] arr; return 0; }
your 2 functions big , clunky is. that's difficult avoid, combining them 1 not idea.
instead try figure out logic common them , put individual functions can use get… , display… functions.
also should have @ continue , break break out of loops. despite popular belief goto viable option break out of multiple loop levels , can used simplify code , make shorter , easier comprehend.
Comments
Post a Comment