Count_nums (need help ASAP!)

This is a discussion on Count_nums (need help ASAP!) within the C Programming forums, part of the General Programming Boards category; Count_nums in an array --------------------------------------------------------- Hi..i need to get a solution to this pretty trivial problem asap. This is a ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Unhappy Count_nums (need help ASAP!)

    Count_nums in an array
    ---------------------------------------------------------
    Hi..i need to get a solution to this pretty trivial problem asap.

    This is a random number generator that has header functions that are meant to do most of the work.

    The part im having trouble with is the count_nums section where the program is meant to output the number of occurrences of numbers 1->9 in the array a[i] which contains random numbers

    Ive been able to do it with switch statements...but it needs to be done just using the variables *a (the array), range (defined range), and num (the number 1->9 which will be incased in a loop) as well as maybe int count or int i too.

    I also need to be able to accept in a character r or R from the keyboard to restart the whole thing , and q to exit!!!!

    PLLLLLLLLLLLEEEEEEEEEEEEASSSSSEEE help me if you can!

    ---------------------------------------------------------------------
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>
    
    
    
    /*int*a will be our array, and int s the size of the array*/
    
    	void SetRandomSeed(void);
    	int my_random(int range);
    	int get_max(int*a,int s); /* array, array size */
    	int get_min(int*a,int s); /*array , array size */
    	void count_nums(int*a,int range,int num);
    
    int count_num(int a[], int range, int num)
    /* checks array a for number of occurrances of value */
    {
       int i, count=0;
       for (i=0; i<range; i++)
       {
    	 if (a[i] == num)
    	 {
    		++count; /* it was found */
    	 }
       }
       return(count);
    }
    
    void print_array(int a[], int range)
    {
       int i;
       for(i=0; i<range; i++)
       {
    	 printf("%d ", a[i]);
       }
       printf("\n");
    }
    
     
    int get_max(int*a,int s)
    				/* returns largest number */
    {
       	int i, num_large;   /* cnum_large is largest value so far      */
      
       /*  Initial array element is largest so far                   */ 
       	num_large = a[0];       
    
       /*  Compare remaining list to the largest so far; save larger */
       	for (i=1; i<s; ++i)
          		if (a[i] > num_large)
             		num_large = a[i];
       return (num_large);
    }
    
    
    int get_min(int*a,int s)
    		/* returns smallest number */
    {
       			int i, num_small;   /* num_small is smallest value so far      */
      
       /*  Initial array element is smallest so far                   */ 
      			 num_small = a[0];       
    
       /*  Compare remaining list to the smallest so far; save smallest */
       			for (i=1; i>s; ++i)
          			if (a[i] < num_small)
             		num_small = a[i];
       return (num_small);
    }
    
    
    
    void SetRandomSeed(void)
    {
       /* call time and feed it to srand */
       	srand( (int) time(0) ) ;
    }
    
    
    
    
    int my_random(int range)
    {
     return 1+rand()%range;
    }
    
    
    int main () 
    {
       int a[10],one; /*array*/
          int i,range,s,c; /*global variables*/
            int max,min,randc; /*secondary variables*/
             int num_occ, value;
    
      
      /*Welcome line*/
      printf("\nWelcome to the Random Number Generator.\n");
       /* range */
       	printf("\nPlease enter the value for the range:\n");
       	   scanf("%d",&range);
       		printf("\n----------------------------------\n");
       			printf("This program will generate random numbers in the range %d ",range);
    
    			
       /*input number of random numbers you would like generated*/
    		printf("\nHow many random numbers would you like generated?\n");
    		   scanf("%d",&s); /*length of s*/
    		 
        for (i=0;i<s;++i){
          a[i] = my_random(range);//int n
               printf("\nrandom number %d: %d\n",i,a[i]);//int n ends
    }
            printf("\nPress any key to continue...\n");
    		getch();
    	
    		
    
       SetRandomSeed();
       /* set the random seed from the time */
    	printf("\nRAND_MAX equals %d \n",RAND_MAX);
    	    printf("range equals %d",range);
    	       printf("\n----------------------------------\n");
    
       /* print the occurrances*/
    
        for(value=0; value<=9; value++)
       {
    	  num_occ = count_num(a,range, value);
    	  printf("The value %d was found %d times.\n", value, num_occ);
       }
    
       	
        /*max and min of range*/
       printf("\nPress enter for the max and min values for the range\n");
          getch();
            printf("\n min %d\n",get_min (a,s));
                printf("\n max %d\n",get_max (a,s));
      getch();
    
     
       /* if everything okay return no errors */
       	return (0);
    
    
    
    }
    ----------------------------------------------------------------------

    Thanks a lot!
    Legacye
    Attached Files Attached Files

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int total[10] = {0};
    int array_to_count[] = { 1, 3, 4, 9 , 0, 0, 3, 4, 5, 6, 7, 7, 0, 2, 1, 5, 6, 8, 8, };
    int x;
    
        /* in a loop */
        total[ array_to_count[ x ] ] ++;
    What this does is take the value at "array_to_count[ x ]", find the appropriate cell in 'total', and increment that value by one.

    At the end, you can loop through 'total' and see how much of each position there is.

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

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    thanks...but could someone show me how to do that in the header?

    This is the section that does the count work:
    Code:
    int count_num(int a[], int range, int num)
    /* checks array a for number of occurrances of value */
    {
       int i, count=0;
       for (i=0; i<range; i++)
       {
     if (a[i] == num)
     {
    ++count; /* it was found */
     }
       }
       return(count);
    }
    How can i apply quzah's method to this part of the program..so that i get the occurrances to output in the main() part of the program that loops as shown above?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Perhaps you should be doing
    Code:
        for(value=0; value<=range; value++)
       {
    	  num_occ = count_num(a,s, range);
    	  printf("The value %d was found %d times.\n", value, num_occ);
       }
    It would help if you
    a) picked better variable names
    b) were more consistent about your use of range and num
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Unhappy no that doesnt work

    i Tried that Salem, but all it does is extend the output past 9 (up to the maximum value in the array)..it doesnt help the functionality of occurrances at all...

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Unhappy Tried the first way...no success

    I tried the total [nums] etc...with and without Salems suggested way and it keeps crashing ...and doesnt count the numbers correctly

    Code:
    int count_num(int a[], int range, int num)
    /* checks array a for number of occurrances of value */
    {
     
    int total[10]={0};
    int answer;
    
    {
       total[a[num]]++;
       answer=total[a[num]];
    }
       return(answer);
    }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Bah, close

    Code:
        for(value=0; value<=range; value++)
       {
    	  num_occ = count_num(a,s,value);
    	  printf("The value %d was found %d times.\n", value, num_occ);
       }
    But then you should have been able to figure that out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help ASAP ,
    By Haytham in forum C Programming
    Replies: 3
    Last Post: 05-14-2007, 10:21 AM
  2. Need Help ASAP....
    By Maxwell in forum C++ Programming
    Replies: 16
    Last Post: 09-14-2005, 06:56 PM
  3. Help Needed Please... Asap
    By Shabba in forum C Programming
    Replies: 2
    Last Post: 01-13-2004, 03:24 PM
  4. Strange characters in output Need Help ASAP
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2003, 05:35 PM
  5. Help Needed Asap With Searching An Array!
    By HelpMe++ in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2003, 04:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21