Thread: Help with Counting values in Arrays

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    Help with Counting values in Arrays

    Hello all, I was hoping to get a little help for some code I'm writing. I have to declare an array of 20 integers, fill the array with random numbers between 1-6, and then write a loop to count how many times each of the values appear in the array. Also I have to use an array of 6 elements to keep track.

    This is what I have so far. Any help would be appreciated.



    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    main() {
    
    	int random[20];
    	int occur[6];
    	int i;
    	srand(time(NULL));
    
    	for(i = 0; i < 20; i++)
    	{
    		random[i] = rand() % 6 + 1;
    	}
    		
    
    
    	for (i = 0; i < 10; i++) 
    	{
    
    		if (random[i] == 6)
    			occur[i]++;
    
    
    	}
    	
    
    
    	system("pause");
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    It's looking very good so far, well done on that.

    main should be declared as int main( void ) as it takes no arguments and returns an integer to the operating system.

    The part where you're generating the random numbers and placing them in the array looks good.

    To count occurences in an array you put the frequency of the first thing in the first element, the frequency of the second thing in the second element, so on. Thinking of this, what would be appropriate to use as an array index into 'occur'?

    Also, you're going to need to go through each element to be able to count the frequency of the numbers, so I'm not quite sure where you pulled 10 from.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    Quote Originally Posted by Vontrapp View Post
    Hello all, I was hoping to get a little help for some code I'm writing. I have to declare an array of 20 integers, fill the array with random numbers between 1-6, and then write a loop to count how many times each of the values appear in the array. Also I have to use an array of 6 elements to keep track.

    This is what I have so far. Any help would be appreciated.



    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    main() {    // should be main (void) as you stated
    
    	int random[20];
    	int occur[6];
    	int i;
    	srand(time(NULL));
    
    	for(i = 0; i < 20; i++)
    	{
    		random[i] = rand() % 6 + 1;
    	}
    		
    
    
    	for (i = 0; i < 10; i++)  // This should be for (i = 0; i < 20; i++), typo on my part
    	{
    
    		if (random[i] == 6)
    			occur[i]++;
    
    
    	}
    	
    
    
    	system("pause");
    }
    unfortunately I didn't quite grasp your explanation about counting occurrences.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Note that in your second loop, you are going to get a segfault since occur has only 6 spaces, but you are looping all the way to i=20. Woops! I think what you meant to say there was:

    Code:
    	for (i = 0; i < 10; i++)  // This should be for (i = 0; i < 20; i++), typo on my part
    	{
    
    		if (random[i] == 6)
    			occur[5]++; 
    
    
    	}
    Note that it is 5, and not 6, since a 6 element array has indices 0,1,2,3,4,5

    If you want to use occur[i] to store the number of occurences of i in random, you are going to need 7 entries in occur because arrays in C start at 0.

    If you do that, why not count all occurences of all the numbers in one go?

    Code:
    for (i=0; i<20; i++)
    {
        occur[random[i]]++;
    }
    Should work, as long as occur has 7 entries. You can always just ignore the zero entry. Or if you prefer not to waste space:

    Code:
    for (i=0; i<20; i++)
    {
        occur[random[i]-1]++;
    }
    Then occur[j] stores the number of occurences of j+1.


    Great job so far though. Keep it up


    PS: make sure you intitialize occur[] to all 0's before the loop. I forget offhand if C does that for you when you declare an array as you did, but it can't hrt to program defensively
    Last edited by KBriggs; 05-29-2010 at 02:20 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    8
    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    main(void) {
    
    	int random[20];
    	int occur[7];
    	int i;
    	srand(time(NULL));
    
    	for(i = 0; i < 20; i++)
    	{
    		random[i] = rand() % 6 + 1;
    	}
    		
    
    	for (i = 0; i < 20; i++) 
    	{
    
    	occur[random[i]]++;
    	
    	}
    
    	for (i = 0; i < 7; i ++)
    	
    		
    		printf("%i\n\n",occur[i]);  
    	
    
    
    	system("pause");
    }



    I'm getting wacky numbers when I run this:


    -858993460

    -858993459

    -858993456

    -858993454

    -858993455

    -858993459

    -858993457

    Press any key to continue . . .

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You never initialize your array's values. All you do is add to whatever is there. Set them all to zero first.


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

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're still not initializing the occur array to all zeros to begin with. You could set each element to zero in a loop, or you could use this convenient syntax:
    Code:
    int occur[7] = {0};
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Enable your compiler warning.
    for gcc , use -Wall flag.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    So many errors I found in your program.It seems that you should spend more time on your book.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Undoubtedly true, and welcome to the forum, zhangzhongke007.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    8
    awesome!! Thanks everyone for your help.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    8
    LOL you know what else too, sometimes looking at code for sooooo long you don't even see where your mistakes are. Just like writing a paper and not catching certain grammatical errors. This is pretty much new to me but I am absorbing a lot of information but once again thanks everyone for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Arrays!
    By J-Camz in forum C Programming
    Replies: 3
    Last Post: 11-27-2008, 02:35 AM
  2. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  3. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. Assigning values to arrays using fscanf
    By Hallu in forum C Programming
    Replies: 5
    Last Post: 03-24-2004, 05:50 PM