Thread: Help with number counting program

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    14

    Help with number counting program

    Honestly ya, it's a class program, but im not asking for the answer, just help.

    I have to create a program that lets the user input as many numbers (1 through 9) as he/she wants and use 0 to cancel the input and continue the program which will then take the numbers and display how many times each number was entered. Say the user enter 1 2 1 1 3 4 3 2 , it would read out:
    1: 3 time(s)
    2: 2 time(s)
    3: 2 time(s)
    4: 1 time(s)
    I've been playing around with some for loops and arrays (which we haven't been taught yet, so I'm guessing using arrays wouldn't be accepted), and keep keeping odd outputs.
    Here are a few examples I've been playing with, non go up to 9 or, just some tests that Im thinking are taking me no where. Any help would be great.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i, j, k, counter;
    	counter = 0;
    	
    	printf("enter some ........\n");
    	while (i != 0)
    	{
    		scanf("%d", &i);
    	}
    		for(i > 0; i <= 9; i++)
    		{
    			if(i == 1)
    			{
    				counter = counter + 1;
    				counter = k;
    				printf("1 was entered %d times\n", k);
    				}
    				}
    				system("PAUSE");
    				return 0;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
     int i, j, k;
    
     printf("Enter some numbers:\n");
     while(i != 0){
     scanf("%d", &i);
    
     
    
    
    
        for(i > 0; i <= 9; i++)
        {
    		for(k = 1; k < 9; k++)
    		{
    			printf("k is %d", k);
    		}
      
    	}
        system("PAUSE");
        return 0;
        }
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You'll need a loop, of course, that keeps the program asking for more numbers each time the user doesn't 0.
    Check what the users enters and use counters to increment for each type, maybe.
    Then output those variables after the loop is finished.

    Think about it a little while. Maybe a solution will come to you.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    I have been working on this exact same problem for days now and i would please like to see the solution to this problem.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Imagine you had 9 cups in front of you, and I kept handing you a numbered piece of paper, over and over.

    What could you do with those 9 cups to keep track of 9 digits, and help you count up (at the end), all the tallys of all the numbers. (not the sums, the tallys)

    Get yourself 9 cups/jars/saucers, any 9 containers, and you sit down and start picking up numbered pieces of paper, and you will see the answer in 15 minutes, tops. (probably in 1).

    Usually, we'd use an array or linked list for this, but you can use nine variables, just like you're using those nine cups, perfectly well.

    Don't increment the "container". Increment the individual cup in your program.
    Last edited by Adak; 04-24-2009 at 12:01 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't you be using ten containers, since there are ten digits?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nope, they are counting up only nine digits. Zero is just to quit entry of digits.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, if you're going to actually read the first post.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    LOL!

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    I have a problem similar to this one. It is to find the maximum number in the list and count and print out the number of times it was entered. Can someone please help me.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's pretty much identical to this issue. Just add another variable to keep track of the max, and check for it in the loop. Post your code attempt.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    YoungVito, did you try the 9 cups and 9 digits on several pieces of paper, exercise?

    No, you did not!

    If you would, you'd have the answer by now. Instead, you're here again, looking for someone to code it up for you.

    And what's ironic is that I already coded up this min and max example, just earlier today, for someone else. (TammyS, I believe).

    So search for it with the forum search tool, or show your code attempt at doing this.

    This is not rocket science. Give it a shot!
    Last edited by Adak; 04-24-2009 at 09:22 PM.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    Here is my attempt at this program could someone please give me some advice.

    insert
    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int max;
        int i;
        int current;
        int numbers;
        int count = 0;
        printf("\nHow many numbers would you like to enter: ");
        scanf("%d", &numbers);
        
        for (i = 0; i < numbers; i++)
        {
            scanf("%d", &current);
            if (max > current)
            {
               max = current;
               count++;
            }
        }
        printf("Max: %d     Number of times: %d\n", max, count);  
        system("PAUSE");	
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Fibonacci number program problem(assembly)
    By ok_good in forum Tech Board
    Replies: 7
    Last Post: 04-07-2006, 07:27 PM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  5. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM