Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CuteBoA

macrumors newbie
Original poster
Mar 12, 2010
21
0
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!!
 
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];

void sortMarks ( StudentInfo stud[], int n ) {
	//float temp;
	//int temp_id;
	//string temp_name;
        [COLOR="Red"]StudentInfo temp_student;[/COLOR]
	int comps = 0;
	for ( int i = 0; i < n; i++ ) {
		for ( int j = 0; j < n-1; j++ ) {
			comps++;
			if ( stud[j].mark > stud[j+1].mark ) {
				//temp = stud[j+1].mark;
				//stud[j+1].mark = stud[j].mark;
				//stud[j].mark = temp;
                                [COLOR="red"]temp_student = stud[j+1];
                                stud[j+1] = stud[j];
                                stud[j] = temp_student;[/COLOR]

			}
		}
	}
	cout << "COMPS: " << comps << endl;
}


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.
 
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.

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..
 
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.
 
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..
 
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.
 
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.

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.
 
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.

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..
 
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.
 
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.

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.
 
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
 
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.

That's exactly where I was going. But I was waiting for the questions to be asked.
 
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
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?
 
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
 
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

thank you for the help!!
 
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.