Thread: Newbie question

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Newbie question

    Hi, I have an array of integers and I am trying to figure out how many elements in the array have exactly two "2"s. I'm not sure how to do this and was hoping someone here can point me in the right direction. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22
    int ar[2];
    ar[0] = 2' ar[1] =2; (ar[2] = '\0')
    Is this what you were trying to understand....

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    RE:

    Sorry, what I meant was if the array has numbers 12342, 111, 1341122, and 12345, then which array elements have exactly two "2" digits. Answer is 12342 and 1341122 but I don't know how to approach this.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22
    only ar[2] has two "2" digits
    ar[0] = 12342;
    ar[1] = 111;
    ar[2] = 1341122;
    ar[3] = 12345;
    Is that clearer?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    RE:

    Sorry I don't think you understood my question. I'm just trying to make a program that reads integer elements from an array and that prints out the elements with two '2' digits inside of it.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    sounds like you need to convert the integers to strings and then count the 2s in it.

    try sprintf to convert the integer into a string, and then use a loop to count the 2s.
    Last edited by robwhit; 12-05-2007 at 01:38 AM. Reason: MacGyver

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you're trying to convert an integer into a string, I believe sprintf() is the correct one. sscanf() would be better suited to read an int from a string.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    You can extract the digits, starting at 1's, then 10's, then 100's place and so on, from the given integer and keep track of number of 2 encountered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM