Hi..i saw this a randomly..i got some part don't understand:
http://www.daniweb.com/forums/thread214624.html
@ancient dragon's reply

1)
const unsigned int MAX_SIZE = 2;

student array[MAX_SIZE];

Q: Why MAX_SIZE = 2 ?

2)15: void insert(student array[],const unsigned int MAX_STUDENT);

33: void mySort(student array[],const unsigned int MAX)
55: void display(const student array[], unsigned int MAX);

Q: 1. From the function above, why sometime he use MAX sometime use MAX_STUDENT and MAX_SIZE??
2. Is the function must use "const" / "unsigned int"? Can i just declare like void display( student array[], int MAX);??

That all my questions.LOL..Sry for my stpdity
Hope somebody can answer.

Hi..i saw this a randomly..i got some part don't understand:
http://www.daniweb.com/forums/thread214624.html
@ancient dragon's reply

1)
const unsigned int MAX_SIZE = 2;

student array[MAX_SIZE];

Q: Why MAX_SIZE = 2 ?

2)15: void insert(student array[],const unsigned int MAX_STUDENT);

33: void mySort(student array[],const unsigned int MAX)
55: void display(const student array[], unsigned int MAX);

Q: 1. From the function above, why sometime he use MAX sometime use MAX_STUDENT and MAX_SIZE??
2. Is the function must use "const" / "unsigned int"? Can i just declare like void display( student array[], int MAX);??

That all my questions.LOL..Sry for my stpdity
Hope somebody can answer.

This thread refers to another person's thread. I suggested the OP start his own thread. I don't see a reply by Ancient Dragon on the thread. He had commented on some earlier threads, but I don't see the one you refer to.

He probably used a global constant called MAX_SIZE and assigned it to equal 2 because he knew the array would never be larger than 2 and it's more descriptive and informative to the reader to see something called MAX_SIZE than to simply do this:

student array[2];

Why did he use MAX some places and MAX_STUDENT other places? He probably wanted to avoid confusion and make it clear that these were two different variables. It's hard to speculate without seeing the post. Maybe one function related to the number of students and the others didn't. You can name them whatever you want. They are LOCAL variables. All the functions can use the same name or all different. It's entirely up to the programmer to pick the variable names. The compiler couldn't care less.

Regarding when to use const and why, google "const C++". There are some good examples of when and how it works. "const" means "constant", which means it is not allowed to change. Often it is used as a safety mechanism so you can't accidentally change a variable that shouldn't be changed.

" Why MAX_SIZE = 2 ? "

Choosing 2 was for demonstrative
purpose, it could have been 1000. It represent the max number of student
allowed.

"Q: 1. From the function above, why sometime he use MAX sometime use MAX_STUDENT and MAX_SIZE?? "

The name does not matter. The function prototype can has its own
arguments with its own unique name.


"Is the function must use "const" / "unsigned int"? Can i just declare like
void display( student array[], int MAX);??"

Yes you can, although you might have to typecast it. The use of
unsigned int is because there will never be a negative students,
just 0 to max number of students.

LOL..i am so sorry..is fistperson's reply...anywhere thanks for you explaination ^^

" Why MAX_SIZE = 2 ? "

Choosing 2 was for demonstrative
purpose, it could have been 1000. It represent the max number of student
allowed.

"Q: 1. From the function above, why sometime he use MAX sometime use MAX_STUDENT and MAX_SIZE?? "

The name does not matter. The function prototype can has its own
arguments with its own unique name.


"Is the function must use "const" / "unsigned int"? Can i just declare like
void display( student array[], int MAX);??"

Yes you can, although you might have to typecast it. The use of
unsigned int is because there will never be a negative students,
just 0 to max number of students.

owh i see..so what u mean is.. i can use same name like "n" ti replace the MAX, MAX_NUMBER all over the program?

switch(opt)
    {
        case 1:    std::sort(array,array+MAX,sortByID); break;
        case 2: std::sort(array,array+MAX,sortByName); break;
        case 3: std::sort(array,array+MAX,sortByNationality);break;
    }

this part i also not understand

std::sort(array,array+MAX,sortByID)--->what is the purpose for this?? o.0''

switch(opt)
{
case 1: std::sort(array,array+MAX,sortByID); break;
case 2: std::sort(array,array+MAX,sortByName); break;
case 3: std::sort(array,array+MAX,sortByNationality);break;
}

this part i also not understand
std::sort(array,array+MAX,sortByID)--->what is the purpose for this?? o.0''

You know the function call says a lot there :

case 1:    std::sort(array,array+MAX,sortByID); break;

If the users picks 1 the do the following :

std::sort(array,array+MAX,sortByID); break;

std:: <-- from the standard namespace
sort(...) from the standard namespace use its sort functions

sort(array, array + MAX, sortByID);

- Reads as, sort array from the beginning up until Its end, and
use the compare function that is passed to it, which happens
to sort by id.

Remembers, if Array is an array data_type, then its name
is the first address that it points to, so its element 0.
Similarly, Array + 1, is the first element plus 1 over, which is the
second element, and Array + MAX, is the first element plus the
one-pass-the end element. It is one passed the end element
because the sort function uses '<' operator and not the '<=' operator in its for loop.

The same could be said for the other case in the switch statement.

You know the function call says a lot there :

case 1:    std::sort(array,array+MAX,sortByID); break;

If the users picks 1 the do the following :

std::sort(array,array+MAX,sortByID); break;

std:: <-- from the standard namespace
sort(...) from the standard namespace use its sort functions

sort(array, array + MAX, sortByID);

- Reads as, sort array from the beginning up until Its end, and
use the compare function that is passed to it, which happens
to sort by id.

Remembers, if Array is an array data_type, then its name
is the first address that it points to, so its element 0.
Similarly, Array + 1, is the first element plus 1 over, which is the
second element, and Array + MAX, is the first element plus the
one-pass-the end element. It is one passed the end element
because the sort function uses '<' operator and not the '<=' operator in its for loop.

The same could be said for the other case in the switch statement.

Ahh..now i understand! Thanks for your explaination ^^''

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.