Stuck!!

This is a discussion on Stuck!! within the C++ Programming forums, part of the General Programming Boards category; Hello, I am stuck in the following program. Try to be nice, I'm new at this! I am needing to ...

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

    Unhappy Stuck!!

    Hello,
    I am stuck in the following program. Try to be nice, I'm new at this! I am needing to return the actual number that occurs the most times in an array. I have figured out which one that is, but can't seem to display it correctly. I've read, and reread my book on the subject concerning pointers (the chapter the problem is from). I still don't understand. I know I need to find the array subscript which correlates to the most reoccuring number. Please HELP!!!

    [code]

    #include <iostream.h>
    #include <iomanip.h>

    //Function prototype

    int findCommon(int array[],int aray[],int number);

    int main(void)
    {
    int numGrades;
    int *grades;
    int *freq;
    cout<<"How many grades do you want to enter?<more than one please.>";

    cin>>numGrades;

    grades=new int [numGrades];
    freq= new int[numGrades];

    for(int count=0;count<numGrades;count++)
    {
    cout<<"Grade "<<(count+1)<<": ";
    cin>>*(grades+count);

    }
    cout<<"Grades :";

    for (int index=0;index<numGrades;index++)
    {
    cout<<setw(3)<<grades[index];
    }
    cout<<endl;
    cout<<"The mode is: "<< findCommon(grades,freq , numGrades)<<endl;
    return 0;

    }

    int findCommon(int grades[], int freq[],int numGrades)
    {

    int count=0;
    for(int number=0;number<=numGrades;number++)
    {

    for (int compare=0;compare<=numGrades;compare++)
    {

    if (grades[number] == grades[compare])
    {
    count=count+1;
    }
    }
    freq[number]=count;
    count=0;
    }
    int most=freq[0];

    for (int k=0;k<=numGrades; k++)
    {
    if (most < freq[k])
    {
    most=freq[k];
    }

    }


    return freq[most];
    //problem area: set most to original array????

    }
    [\code]

    I know this is probably an easy one for anyone helping, but I' ve been staring at it too long, and my teacher can't show me.
    Thanks,
    George

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please read this, then edit your post accordingly. Thanks

    [edit] I see you're only a character out.... wrong type of slash on the closing code tag. Good try
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I just looked over your code quickly, but try using 'return most;', because you are assigning it the value of the most largest element of freq, not the index.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    wow this is messy..
    hmm here is an edited version of your code..
    I dont have time to test/compile it. so dont blame me if doesnt work..

    Code:
     #include <iostream>       //dont use .h use iso headers
     #include <iomanip>
     using namespace std;
     
     
     int findCommon(int array[],int aray[],int number);
     
     int main(void)
     {
             int numGrades;
             int *grades;
             int *freq;
             cout<<"How many grades do you want to enter?<more than one please.>"<<endl;
     
             cin>>numGrades;
     
             grades=new int [numGrades];
             freq= new int[numGrades];
     
             for(int count=0;count<=numGrades;count++)
             {
                     cout<<"Grade "<< count++ <<": ";
                     cin>>grades[count];    // I think this is what u meant?
     
             }
             cout<<"Grades :";
     
             for (int index=0;index<=numGrades;index++)
             {
                     cout<<setw(3)<<grades[index];
             } 
             cout<<endl <<"The mode is: "<< findCommon(grades,freq , numGrades)<<endl;
             return 0;
     
     }
     
     int findCommon(int grades[], int freq[],int numGrades)
     {   
     
             int count=0;
             for(int number=0;number<=numGrades;number++)
             {
                     
                     for (int compare=0;compare<=numGrades;compare++)
                     {
                     
                             if (grades[number] == grades[compare]) 
                             {
                                     count++;
                             }
                     }
                     freq[number]=count;        
                     count=0;        
             }
             int most=freq[0];
             
             for (int k=0;k<=numGrades; k++)
             {
                     if (most < freq[k])
                     {
                             most=freq[k];
                     }
                     
             }
     
     
             return freq[most];
     
     }
    Luigi

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I think this is your error:

    change
    Code:
             for (int k=0;k<=numGrades; k++)
             {
                     if (most < freq[k])
                     {
                             most=freq[k];
                     }
                     
             } 
             return freq[most];
    to
    Code:
             int index=0;
             for (int k=0;k<=numGrades; k++)
             {
                     if (most < freq[k])
                     {
                             most=freq[k];
                             index=k;
                     }
                     
             }
             return freq[index];
    Also, make sure you set the frequency array to all zeros before doing anything with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 08:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 05:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 08:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 09:28 PM

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