C++ problem

Discussion in 'Mac Programming' started by CuteBoA, Apr 4, 2010.

  1. CuteBoA macrumors newbie

    Joined:
    Mar 12, 2010
    #1
    hello,
    here's part of my program to find the mode..

    Code:
    int id[MAX_STUDENT];
    string name[MAX_STUDENT];
    float mark [MAX_STUDENT];
    
    struct StudentInfo {
    	int id;
    	string name;
    	float mark;
    };
    
    StudentInfo stud[MAX_STUDENT];
    
    
    
    so basically, ive managed to get my program works..
    my program needs to output the NAME OF STUDENT who got the median, lower quartile, and upper quartile mark.. i managed to get the right mark for those 3 mark and ive found the correct mode alsoo.. but the problem is that, the marks dont match the students' name.. so i decided to put those extra codes in the sortMarks function ( the one that i put // in front of the code )..
    but when i put that, i always get the wrong mode eventough now i have the correct students' name who got those 3 mark ( median, etc)


    so whats wrong with it? how come when i added extra codes (with // in front), the mode suddenly changes in value.. but, i though that it shouldnt affect anything for the findMode function.. help!!
     
  2. naples98 macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #2

    If you want to sort the students based on their mark attribute, you need to swap the struct, not just the mark attribute in the struct. The code in red shows what I'm talking about. This should fix the problem with getting students with the wrong mark.

    After studying this, see if you can figure out your other problem.
     
  3. CuteBoA thread starter macrumors newbie

    Joined:
    Mar 12, 2010
    #3
    i've gotten the correct name and mark for those 3 marks after i changed it like your instruction..
    like i said above, the problem is in the findMode function... i got 66.3 for the mode, it should have been 80.5.. whats wrong with that??? thanks..
     
  4. naples98 macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #4
    Please post your updated code, including your entire main() function.
     
  5. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    Also waiting for complete source before continuing!
     
  6. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #6
    What's the point of doing someone home work for him. To prove you are smarter then a CS101 student?

    We can hint about where to look for an error, but not posting of code fixes.

    OK now if you really want to solve this problem you are working way to hard. Inside the standard C library is a sort function. Just load up the arrary of strcts and call qsort.

    See "man 3 qsort"

    There is no reason in the word to write you own fort function except as a homework exercise.
     
  7. CuteBoA thread starter macrumors newbie

    Joined:
    Mar 12, 2010
    #7
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    const int MAX_STUDENT = 100;
    
    int id[MAX_STUDENT];
    string name[MAX_STUDENT];
    float mark [MAX_STUDENT];
    
    struct StudentInfo {
    	int id;
    	string name;
    	float mark;
    };
    
    StudentInfo stud[MAX_STUDENT];
    
    
    	cout << "Mean : " << mean << endl << endl;
    	
    	cout << "Mode : " << mode << endl << endl;
    	
    	system("PAUSE");
    	return 0;
    	
    }
    
    here its.. thank you.. ive been trying to figure out whats wrong with it.. but i couldnt..
     
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    In 'findMode' you allocate and array called 'count' on the stack. What are the initial values of each entry in the array 'count'?

    I'm sure you'll shortly have more questions.
     
  9. naples98 macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #9
    The OP pretty much had the correct idea before I posted any code even though he may not have realized it. I just simplified it for him, explained the concept, and posted some code so he would understand what I was talking about. As you can see, I told him to see if that helped him with his other problem.

    I'm not here to do someone's homework just to prove I'm smarter than a CS 101 student but I have my teaching style and you have yours.
     
  10. CuteBoA thread starter macrumors newbie

    Joined:
    Mar 12, 2010
    #10
    i think i didnt initialize the count to anything for the first time, the count array its just a new array to get track of how many times the same number appears, and then i will compare them and the the maximum one as you can see in the if code.. and then i will return the i, and put it in the stud[number].mark to get the modeee..
     
  11. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #11
    Although I may be wrong I think CureBoA is a Korean female. I think she doing fairly well. She does the work and asks for assistance when she needs it. SHe's learning well on her own and just needs occasional pointers.
     
  12. CuteBoA thread starter macrumors newbie

    Joined:
    Mar 12, 2010
    #12
    do u have any idea whats wrong with my code?
    im really stuck with my program, because i cant think any of way out.. its not because i didnt try to do it.. thank you.
     
  13. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    count is full of garbage values. This is what lloyddean meant by stating that you failed to initialize it. You don't know that they are all the same, so you incrementing a few positions to some new value doesn't mean you're going to be comparing the counts of the values. Tossing a "= {0}" in there will be enough to fix your mode problem.

    If you need a demonstration of what's happening, use this:

    Code:
    float findMode ( int n ) {
            int count[MAX_STUDENT];
            for ( int i = 0; i < n; i++ ) {
                    for ( int j = 0; j < n; j++ )
                    {
                            std::cout << "Comparing: " << stud[i].mark << " and " <<
     stud[j].mark << " are they equal? " << (stud[i].mark == stud[j].mark) << std::e
    ndl;
                            if ( stud[i].mark == stud[j].mark ) {
                                    std::cout << "incrementing position " << i << " 
    from " << count[i] << " to " << count[i] + 1 << std::endl;
                                    count[i]++;
                            }
                    }
            }
    
    
    As the beginning of the findMode function. I think you'll see pretty quickly what's going on, and why you really do need to initialize count.

    -Lee
     
  14. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #14
    That's exactly where I was going. But I was waiting for the questions to be asked.
     
  15. CuteBoA thread starter macrumors newbie

    Joined:
    Mar 12, 2010
    #15
    thank you very much!! its now working properly..
    but, i dont understand it.. would you mind explaining to me? why do i need to put int count[MAX_STUDENT] = {0}??
    why do i need to put = {0}.. what is it used for?
    thank you!

    EDIT: i came across this thought.. int count[MAX_STUDENT] = {0}.. we need that to initialize every count array to zero?
     
  16. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #16
    Every *element* of count to 0. It's sort of cheating to only specify one element, but the default behavior is to set any unspecified value in an array initialization to 0. Otherwise you could simply loop from 0 to MAX_STUDENTS setting each element of count to 0.

    The reason for this is that you never set a "base" value for each element of count, so whatever junk is in memory where count is placed (it will be on the stack, in whichever place findMode gets pushed). Where that is doesn't matter, what does is that any value you don't explicitly set is going to just contain *something*. it is rarely going to be 0, and if it is it's just luck and will come back to bite you if you rely on that behavior.

    Always set your variables using =, memset, etc. or you will get erratic behavior. In this case the first operation you were performing on any element of count was ++, which depends on the current value. As you've seen, that was not 0, it was junk.

    I apologize to lloyddean and others, this was more of a "giveaway" than I intended. The point was to nudge the OP toward the solution since the initialization issue had already been raised. The "= {0}" piece was probably too much, but I posted with some haste.

    -Lee
     
  17. CuteBoA thread starter macrumors newbie

    Joined:
    Mar 12, 2010
    #17
    thank you for the help!!
     
  18. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #18
    The hardest thing in assisting people, sight unseen, is judging their current understanding of programming in general or the language in particular. This effects the approach taken in providing any assistance. This is why I tend to ask so many question before providing assistance.

    I think you did fine, but I myself was trying to see how much she could figure out for herself with what sized hints.

    She came back so we must be doing something right.
     

Share This Page