Thread: help anyone

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    help anyone

    Ok i've been staring at this code for over an hour and i cant figure out why it wont run properly.

    What i'm trying to do is have a program that i've set values from 0 --99 in an array, i want to check the numbers and see wat the frequency of numbers are.

    ie. 0-9 4 ( there are 4 numbers between 0-9)
    10-19 5 ( there are 5 numbers between 10-19) and so on until 99

    i want to display these in a histogram like

    0-9 4 XXXX
    10-19 5 XXXXX
    ...
    90-99 2 XX


    This is my code. Would anyone be able to tell me where i'm going wrong
    Code:
    #include <iostream> 
    using namespace std ;
     void disp_chars (int freq, char ch) ;
    
    
    void main ()
    
    {	open main
    
    	int N =26;
    	int interval;
    
    	//float numbers[]= { 10, 12, 15, 16, 20, 25, 27, 
    					29, 30, 36, 40, 42, 43, 50, 59, 
    					61, 65, 67, 69, 72, 76, 77, 86, 90, 99 } ;
    
    	int count[10];
    	char ch = 'X' ;
    	
    	disp_chars(5,ch);
    	cout << endl ;
    
    
    	for ( int i = 0 ; i < N ; i++)
    		{
    			
    			interval =numbers[i]/10 ;
    			count[interval]++ ;
    		}
    
    	for( i=0; i<9; i++)
    	{
    		cout << i << " = " << count[i] <<endl ;
    	}
    
    	for( i=0; i<9; i++)
    		{
    			
    			disp_chars(count[i],ch);
    		}
    }//end main
    
    void disp_chars ( int freq, char ch )
    {
    	for ( int i=0; i<freq; i++)
    	{
    			cout << ch;
    	}
    	cout << endl ;
    }

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    First, please explain to me how there are only 4 numbers between 0 and 9???
    Code:
    #include <iostream> 
    using namespace std;
    
    int main()
    {
    
    
    	int array[8] = {5,8,15,19,45,76,79,88};
    
    
    	int math;
    
    	for (int i = 0; i < 7; i++)
    	{
    
    		math = array[i+1] - array[i];
    
    		cout << array[i+1] << " - " << array[i] << " " << math << " ";
    		for(int j = 0; j < math; j++) cout << "X";
    		cout << endl;
    	}
    
    
    	return 0;
    
    	}
    This is not perfect, if the numbers in the array do not keep increasing (i.e. if the nth+n element is smaller than the nth element) it will have a negative value and go into an infinite loop.

    This shows you one method of doing it. How you are attempting to do it is fine (well, if you where doing it correct). It was just easier for me to do it than point out ALL your mistakes.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void main ()
    main returns an int:
    int main ()

    Code:
    >{	open main
    What does "open main" do?

    > for( i=0; i<9; i++)
    This should loop 10 times, so make this:
    for( i=0; i<10; i++)

    And lastly but most important:
    int count[10];
    You must initialize the elements of your count array:
    int count[10] = {0};

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I would set up a frequency counter like so:-

    You can then divide them into appropriate intervals. I'll leave that up to you perhaps?


    Code:
    #include <iostream> 
    using namespace std;
    
    void disp_chars (int freq, char ch);
    
    
    int main ()
    
    {	
    
    	int N = 26;
    	int interval;
    
    	float numbers[]= { 10, 12, 15, 16, 20, 25, 27, 
    			   29, 30, 36, 40, 42, 43, 50, 
                               59, 61, 65, 67, 69, 72, 76, 
                               77, 86, 90, 99, 99} ;
        
        int counter_me[101];
        for (int k=0; k<100; k++)
        {
            counter_me[k]=0;
            /*initialise these babies*/
        }    
            
        
        
        for (int i=0; i<26; i++)
        {
            for (int j=0; j<100; j++)
            {
               if(numbers[i]==j)
               {
                   counter_me[j]++;
                   /*increment number count*/
               }
           }
       }
       
        for (int a=0; a<100; a++)
        {
            cout<<a<<":"<<counter_me[a]<<endl;
            /*print these babies*/
        }
        cin.get();
    }

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    2
    Quote Originally Posted by Enahs
    First, please explain to me how there are only 4 numbers between 0 and 9???

    it was just an example.


    Quote Originally Posted by Enahs
    This shows you one method of doing it. How you are attempting to do it is fine (well, if you where doing it correct). It was just easier for me to do it than point out ALL your mistakes.
    Could you tell me what my mistakes are?

    @swoopy.. sorry i left out a comment there where i had open main

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should make sure that N matches the number of elements in your array. As is, N is 26 but you have 25 elements in the array. Add an element to the array or change N to 25 (and follow swoopy's suggestions) and your program will run.

Popular pages Recent additions subscribe to a feed