Trouble in understanding how for loops interact with arrays in c++ [homework] -
my teacher gave code user inputs number 10 digits, , each digit stored in array. need format in such way outputs each digit repeated individually. i'm trying accomplish using loop, i'm having difficulty in understanding how loop interacts array (or @ least, think that's trouble coming from). i'm @ far... can explain interaction of loop , array between lines 44 , 50 have appreciation.
#include <iostream> // provides cin, cout, endl, fixed using namespace std; #define digits 10 // number of base 10 digits int main() { // declaration section int number, // number input user digit, // digit of number being processed digit_tally[digits], // keeps track of digits seen i; // general array-element counter // initialize digit tally zeroes, indicating no repeated digits have been found (i = 0; < digits; i++) { digit_tally[i] = 0; } // input section cout << "enter number (any negative number exit): "; cin >> number; // processing section while (number > 0) { digit = number % 10; if (digit_tally[digit] == 0) { digit_tally[digit] = 1; } else if (digit_tally[digit] == 1) { cout << "repeated digits: "; (digit_tally[digit] = 1; digit_tally[digit] <= 10; digit_tally[digit]++) cout << number << " "; } number = number / 10; cout << endl; } } for reference, output should this:
enter number (any negative number exit): 64246
repeated digits: 4 6
as have got logic of storing number of occurrences in index. can proceed approach , once finish lopping through entire number, have loop prints index has more 1 occurrence.
while (number > 0) { digit = number % 10; digit_tally[digit -1] += 1; number = number / 10; cout << endl; } ( int = 0; < 10; ++i) { if (digit_tally[i] > 1) { std::cout << + 1 << " "; } }
Comments
Post a Comment