Thread: Program to count 1 through 9

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    17

    Program to count 1 through 9

    OK, so this is similar to the program I needed help on before, only slightly modified.
    I need to take input numbers between 1 and 9 from a user and when an out of range number is entered, the program prints out counts of what was entered.
    Code:
    #include <stdio.h>
    
    int input_number(void);
    int main(void)
    {
    int numbers[10000]={0};
    int a,b,c;		
    c=input_number();	
    if (c<=9&&c>0)
    {
    	for (b=0;b<10000;b++) 
    	{
    	numbers[c]++;					
    	}
    }		
    else
    {
    	for (a=1;a<=9;a++)
    	{
    	printf("There are %d %ds\n",numbers[a],a);
    	}
    }
    
    return 0;
    }
    I left the input_number function off for length.
    An input between 1 and 9 will simply terminate the program, but an input out of range prints the counts, as it should.
    I'm confused because an entry of 4 should initiate the if part, thus counting numbers[4] to 1, and then it should take another input, I think.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Not quite. The if part is executed (and btw, that for loop is wrong!), after that it jumps over the else and goes to the return 0 at the end of main. When you return from main, you exit the program.

    QuantumPete
    edit: Why do you need an array of 10000 ints anyway???
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    our teacher always says "Make your arrays very large" That's why ten k lol.
    What you say makes sense, I'll play and let you know in a minute.
    What's wrong with the for loop?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    wait i see nevermind lol

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    Ok, I've played around with it and have only succeeded in making it worse.
    So, from the same start code as before, how do I make the program stop taking input when an out of range is entered?
    As i said before, when an out of range is entered, it immediately printed the counts.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I'm not sure what your code is meant to do, but if it should count how many times a particular number has been entered, you don't need the loop at all, just the statement inside it.

    To end the program, you can check whether the input is out of range (>9 OR <1) and then print the stats and return 0.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    I figured it out.
    Code:
    #include <stdio.h>
    
    int input_number(void);
    int main(void)
    {
    int numbers[10000]={0};
    int b,c;	
    	
    for(b=0;b<10000;b++)
    {
    	c=input_number();
    if (c<=9&&c>0)
    {
    	numbers[c]++;					
    }	
    
    if(c<1||c>9)
    {
    	int a;
    	for (a=1;a<=9;a++)
    	{
    	printf("There are %d %ds\n",numbers[a],a);
    	}
    }
    }
    return 0;
    }
    
    int input_number()						
    {
    int a;
    printf("Input a positive integer ");
    scanf("%d",&a);
    return a;
    }
    I had one of those ifs outside the for loop and it would just terminate, lol.
    I take that back, it doesn't actually work, it just prints the count, then keeps asking for an input....

  8. #8
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    So what does your new code look like now? You had no code to change the values of anything in your array. If you enter a '4', you are setting numbers[4] to something close to 10000. since you are adding 1 to numbers[4] 10000 times.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    fixed it again, left off a return 0; before ending the for loop.
    Thanks for your help!

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I still don't understand what you are trying to do. You have an array of 10000 ints, and only ever use the first 9...? Since c is never >9, you're never accessing numbers[10] or numbers[100] or even numbers[1000]

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. I'm missing something obvious
    By crash88 in forum C Programming
    Replies: 7
    Last Post: 07-02-2006, 12:51 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM