Thread: C program to count integers

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

    C program to count integers

    I have been given an assignment to write a program where the user inputs 10000 integers, and the program counts how many 1s, 2s, etc up to 99s there are, and then how many are out of range (<=100). The hint was to use an array, but my teacher is terrible and no one in there has figured it out as of yet. I've never programmed before. So far, we have covered if statements, for loops, and he mentioned while and do...while loops, but I don't think I'm supposed to use that. He also sort of explained what an array is.
    I'm not asking for a handout, if someone could just point me in the right direction, with a little explanation...

    Code:
    #include <stdio.h>
    int input_number(int a);
    int main(void)
    {
    int out_of_range=0;
    int count;
    int numbers[10000];
    int b;
    	for (count==1;count==10000;count++)
    	{		
    		for (b=0;b<100;b++)
    		{
    			if (input_number<100)
    			{
    			
    			}
    			else
    			{
    			out_of_range++;
    			}
    		
    	printf("There are %d integers that are out of range",out_of_range);
    	printf("There are %d %ds",??,??);
    	}
    return 0;
    }
    
    int input_number
    {
    int a;
    printf("Input a positive integer greater than 0");
    scanf("%d",&a);
    return a;
    }
    I should also mention that he claimed he could do this with only one if statement. I'm not entirely sure I should use nested for loops, but it seemed to make sense, I'm just not sure how to tell the computer to separate and count the 1s, 2s, etc.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    you need 101 counters


    Code:
    int counters[101] = {0};
    ...
    if (out of range [1-100])
       counter[0] ++;
    else
       counter[entered number]++;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    Alrighty, here's some updated c code-I chose to use 100 counters because 1-99 makes 99 counters and then i only need one counter for out of range.
    Upon compiling, I get some syntax error before { in input_number, so I put () after input_number and that went away. My errors now are that int a=input_number "makes integer from pointer without a cast" and a syntax error at very end of input, last line. I have no idea what the first one is, or why my syntax is wrong for the second one.
    Code:
    #include <stdio.h>
    int input_number(int a);
    int main(void)
    {
    int out_of_range=0;
    int count;
    int numbers[100];
    int a=input_number;
    int b;
    	for (count==1;count==10000;count++)
    	{		
    		for (b=0;b<100;b++)
    		{
    			if (a>=100)
    			{
    			numbers[0]++;
    			}
    			else
    			{
    			numbers[a]++;
    			}
    
    		}
    	printf("There are %d integers that are out of range",out_of_range);
    	printf("There are %d %ds",numbers[a],a);
    	}
    return 0;
    }
    
    int input_number(int a)
    {
    printf("Input a positive integer greater than 0 ");
    scanf("%d",&a);
    return a;
    }
    fixed the syntax my for statement was missing a }
    Last edited by starwarsyeah; 03-07-2009 at 09:54 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    You always need () to call a function. Always.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    Gotcha. Now I get a syntax error before int in line 7 before the int c
    Code:
    #include <stdio.h>
    int input_number(int a);
    int main(void)
    {
    int out_of_range=0;
    int numbers[100];
    int c=input_number(int a);
    int b;
    			
    		for (b=0;b<10000;b++)
    		{
    			if (c>=100)
    			{
    			numbers[0]++;
    			}
    			else
    			{
    			numbers[c]++;
    			}
    		
    		
    		printf("There are %d integers that are out of range",out_of_range);
    		printf("There are %d %ds",numbers[c],c);
    		}
    return 0;
    }
    
    int input_number(int a)
    {
    printf("Input a positive integer greater than 0 ");
    scanf("%d",&a);
    return a;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    The syntax error is before the int in "int a", not "int c". You need to pass something into the function, if the function expects an int.

    On the other hand, your input_number function, if it was designed appropriately, wouldn't expect to be passed in an integer, so I would recommend designing your input_number function appropriately. Make the variable a local to the function rather than a parameter passed into it.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    Hmm yeah in retrospect that makes a lot of sense. My teacher is a rather crude c programmer and doesn't do a good job of explaining these things.
    I moved c=input_number() inside the for loop.
    So it compiles now, but won't run correctly. I used 10 instead of 10000 for simplicity, and after the first number I enter, say, 4, it says 7143533 integers out of range and 7209078 4s.
    It seems like loop isn't actually looping, but just taking one value and then ending.
    Code:
    #include <stdio.h>
    int input_number();
    int main(void)
    {
    int numbers[100];
    int c;
    int b;
    			
    		for (b=0;b<10;b++)
    		{
    			c=input_number();
    			if (c>=100)
    			{
    			numbers[0]++;
    			}
    			else
    			{
    			numbers[c]++;
    			}	
    		
    		printf("There are %d integers that are out of range\n",numbers[0]);
    		printf("There are %d %ds\n",numbers[c],c);
    		}
    return 0;
    }
    
    int input_number()
    {
    int a;
    printf("Input a positive integer greater than 0 ");
    scanf("%d",&a);
    return a;
    }
    messing around by moving the two printf's outside the for loop now allows me to input ten numbers, but still gives me junk answers
    Last edited by starwarsyeah; 03-07-2009 at 10:51 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    So at the beginning of the program, numbers[0] is "019283y4r5o9iuhsdvpas89fhugpq3ijn4t6". If you add three to that, do you expect to get 3 as an answer?

    You should initialize your array to 0 before you start.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    Once again that makes sense, in retrospect lol.
    Okay so the two things I tried: numbers[0]=0;
    This compiled and worked, I entered 5 1s, 4 2s, and a 150, and the only thing it came back with was there is 1 number out of range (so that part is fine) then it said it had encountered an unexpected problem and needed to close.
    I tried initializing numbers[0] and numbers[c] at zero, and then the program encounters the error before even running.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    It seems like the program won't initialize numbers[c]=0 for some reason. It still gives me junk values for that, and if I input more than one integer out of range it still says there is only one out of range. Logically, I can't seem to explain this because if it is out of range, it should increment numbers[0] by one for each integer out of range. also, numbers[c] should increment by one per value of c, if there are three 5s entered, then it should increment 3 times, right?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    What is your current code?
    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

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    For this code, to test, I entered 10 100s and got that there was 1 integer out of range and 22 100s. This is what I have now:
    Code:
    #include <stdio.h>
    int input_number();
    int main(void)
    {
    int numbers[100];
    int c;
    int b;			
    		for (b=0;b<10;b++)
    		{
    			c=input_number();
    			if (c>99)
    			{
    			numbers[0]=0;
    			numbers[0]++;
    			}
    			else
    			{
    			numbers[c]=0;
    			numbers[c]++;
    			}	
    		}
    		printf("There are %d integers that are out of range\n",numbers[0]);
    		printf("There are %d %ds\n",numbers[c],c);
    		
    return 0;
    }
    
    int input_number()
    {
    int a;
    printf("Input a positive integer greater than 0 ");
    scanf("%d",&a);
    return a;
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Firstly, you need to indent your code consistently, e.g.,
    Code:
    #include <stdio.h>
    
    int input_number();
    
    int main(void)
    {
        int numbers[100];
        int c;
        int b;
        for (b = 0; b < 10; b++)
        {
            c = input_number();
            if (c > 99)
            {
                numbers[0] = 0;
                numbers[0]++;
            }
            else
            {
                numbers[c] = 0;
                numbers[c]++;
            }
        }
        printf("There are %d integers that are out of range\n", numbers[0]);
        printf("There are %d %ds\n", numbers[c], c);
    
        return 0;
    }
    
    int input_number()
    {
        int a;
        printf("Input a positive integer greater than 0 ");
        scanf("%d", &a);
        return a;
    }
    Next, instead of repeatedly setting each element of numbers to 0, you should initialise numbers to hold 0s, as tabstop suggested:
    Code:
    int numbers[100] = {0};
    Now, you can remove those lines that assign 0 to numbers[0] and numbers[c].

    This line looks like it belongs in a loop:
    Code:
    printf("There are %d %ds\n", numbers[c], c);
    A point to note: do you need to handle negative input? Even if you don't, you should.
    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

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    Ok, I wondered why tabstop put the 0 in {}, I wasn't sure what the difference was, but thank you for explaining.
    I don't hve the printf in the loop because that would print it every time a number was input, right?
    Also, I think I can take care of the negative issue by using the else for out of range.
    updated code:
    Code:
    #include <stdio.h>
    int input_number();
    int main(void)
    {
    	int numbers[100]={0};
    	int c;
    	int b;			
    	for (b=0;b<10;b++)
    	{
    		c=input_number();
    		if (c<99)
    		{
    		numbers[c]++;
    		}
    		else
    		{
    		numbers[0]++;
    		}	
    	}
    	printf("There are %d integers that are out of range\n",numbers[0]);
    	printf("There are %d %ds\n",numbers[c],c);
    		
    return 0;
    }
    
    int input_number()
    {
    int a;
    printf("Input a positive integer greater than 0 ");
    scanf("%d",&a);
    return a;
    }
    Problems: I enter 3 1s, 3 2s, 3 3s, and 1 4 and I get 0 integers out of range and 1 4s, and that's it. The numbers out of range are correct.
    The problem is that the program is only sending the last value for c to the printf statement, but I need the printf AFTER the loop instead of printing every time I enter a value, but this results in only the last number entered being counted.
    I'm not sure how to go about making the program keep a count of every integer, and then print it out at the very end.

    EDIT
    I neglected the negatives for now until I get the rest straightened out

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by starwarsyeah
    I don't hve the printf in the loop because that would print it every time a number was input, right?
    Not in the loop that does the counting, but in another loop.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. count words program problem
    By Tony654321 in forum C Programming
    Replies: 8
    Last Post: 10-19-2004, 11:23 AM
  4. Why is this program converting to integers
    By 3DPhreak in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2004, 09:44 AM
  5. word count program need a bit of help!
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-19-2002, 08:15 PM