please help, using array C++ printing "it has duplicate"if repeats

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ericnyc
    New Member
    • Nov 2006
    • 2

    please help, using array C++ printing "it has duplicate"if repeats

    Hi,
    I am a newbee in C++
    Please review this is what I wrote , what so ever I understood so far,
    My question is that I have to " Write a program C++ array that reads in an integer number and checks whether any of the digits in the number appear more than once"
    Hint: Use an array called digits_seen of size 10 whose base type is Boolean. It will be indexed from 0 to 9, which corresponds to the 10 possible digits and initially all the entries should be set to false. When the user enters a number n, the program examines the digits and sets the appropriate index entry in the array to true. If when it goes to do this, the entry is already true then you have encountered a duplicate!

    HINT2: use type long int to store the input number number read in…so you get a lot of digits.


    I have just started few weeks ago so this what I know so far.. Please help
    #include<iostre am>
    using namespace std;

    void initialize(bool digit_seen[]);
    const int size=10;

    int main()
    {
    bool digit_seen[size];

    int new_value;
    long int i;


    initialize(digi t_seen);

    cout<<"Enter integers\n";
    cin>>digit_seen[0];
    new_value=digit _seen[0];
    for(i=0; i<size; i++)
    {

    if(digit_seen[i]==true)
    new_value=digit _seen[i];

    cin>>digit_seen[i];

    }

    if(digit_seen[i]!=new_value)

    cout<<"no repeat\n";

    else

    cout<<"repeat\n ";


    return 0;
    }

    void initialize(bool digit_seen[])

    {

    int i;

    for (i = 0; i <10; i++)

    digit_seen[i]=false;

    return;

    }
  • smartway
    New Member
    • Oct 2006
    • 24

    #2
    You have been asked to use type long int to store the input number number read in…so you get a lot of digits.

    The input for your program is a number that should be declared as long int so that you can have long number containing more digits.

    The Logic I can explain you then you try to put it in program
    Use one more array of size 10 corresponding to digits 0 to 9 which stores the count of the corresponding digits....... initially all counts ahould be zero
    % operator gives you remainder
    store the input number in temporary variable
    use %10 to find the remainder that will be last digit of the number
    check the number and increment corresponing count
    then use /10 you will get the number in which last digit will be eliminated

    repeat above till you get number after /10 as 0

    then check in the count array if value is >1 then the corresponding digit appeared more than once

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      a simple way to solve this is to have a char arry which holds the digits of the number, e.g.
      Code:
      char digits[20];
      you would then have a loop which uses each element of digits as an index into your array digit_seen to see if it is already true, if so it prints "repeat" and exits the program e.g.
      Code:
      if(digit_seen[digits[i]]) { cout<<"repeat\n"; return; }
      if digit_sen is not true you set the element to true and then tests the next char in digits, etc.

      Comment

      Working...