Thread: How to use loops to find numbers from a list?

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    4

    How to use loops to find numbers from a list?

    i have to make a program where a user inputs one number and i have to find the number of a certain amount of number in the program.
    for example:

    user input: 2349354787839
    output: There are two sevens

    i know it has the use of loops but i am having trouble finding a way to scan each individual digit in the input to find if it is a seven or not. or maybe i have the wrong idea...

    can someone help me out?

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I'd try a char array for input. In the loop body, test for the desired number and increment a count variable.

    Code:
    if char_arr[loop_index] equals some_digit
        // increment count;

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    The thing is we cant use arrays yet because we havent learned them. is there a way to do this with just loops?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You could use scanf, inside a loop.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    loop while your input is greater than zero. Check into the modulus operator for finding the digit, compare that value against the target number for counting, and lastly use division on the input to control the loop. There's a certain number base you can use.

    Show your code if you're still stuck.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Simply use getchar() and check each digit if it's the one you are looking for.

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    24
    Quote Originally Posted by Tommy Kim View Post
    i have to make a program where a user inputs one number and i have to find the number of a certain amount of number in the program.
    for example:

    user input: 2349354787839
    output: There are two sevens

    i know it has the use of loops but i am having trouble finding a way to scan each individual digit in the input to find if it is a seven or not. or maybe i have the wrong idea...

    can someone help me out?
    Just do a linear search, it's an O(n) time algorithm but will do the work for you, better if you use Binary Search, it has O(log n) complexity and very efficient for large number of input.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dotnet13
    better if you use Binary Search, it has O(log n) complexity and very efficient for large number of input.
    Binary search is not applicable here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Tommy Kim View Post
    The thing is we cant use arrays yet because we havent learned them. is there a way to do this with just loops?
    That depends on how large you want allow the number that the user enter's to be. As it is, for your example you need to resort to the largest integer type there is. ([unsigned] long long)
    If you want it to be allowed to be really huge, then sorry an array is the only option.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-09-2013, 07:41 PM
  2. find the maximum width & length of a box using for loops?
    By bac3bac0mm in forum C Programming
    Replies: 1
    Last Post: 03-17-2011, 10:23 PM
  3. Help Me Find My Error Please (loops)
    By tru.cutru in forum C Programming
    Replies: 6
    Last Post: 06-30-2008, 07:27 PM
  4. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM