Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HellO Guys .. :)

First of all this is not the home work. This is my Lab Test .
So, i really need your help here ..

Actually I want to do is find the Maximum CGPA Of A Student.
here is my code ..
C++
#include <iostream>
#include <iomanip>
using namespace std;

class Student
{
public:
    void GetData(Student *student, int size);
    void PrintData(Student *student, int size);
    void is_greater(Student *student, int size);
    Student();
private:
    int RollNo;
    float CGPA;
};
Student::Student()
{
    RollNo = 0;
    CGPA = 0;
}

void Student::GetData(Student *student, int size)
{
    for(int i = 0; i < size; i ++)
    {
        cout << "\t" << "## STUDENT ## {" << i +1 << "}" << endl;
        cin >> student->RollNo;
        cin >> student->CGPA;
        student++;
    }

}
void Student::PrintData(Student *student, int size)
{
    cout << setw(8) << "ROLLNO" << setw(16) << "CGPA" << endl;

    for(int i = 0; i < size; i ++)
    {
        cout << setw(8) << student->RollNo << setw(16) << student->CGPA << endl;
 //      *(student++);
        student ++;
    }

}

void Student::is_greater(Student *student, int size)
{
    float min_cgpa = student->CGPA; //min_cgpa = student[0].CGPA;
    int pos;
    for(int i = 0; i < size; i ++)
    {
            if(student->CGPA > min_cgpa)    //if(student[i].CGPA > min_cgpa)
            {
                min_cgpa = student->CGPA;   //min_cgpa = student[i].CGPA;
                pos = i;

            }
            student++;
    }
//    cout << "\t" << "Index Of Highest GPA -> " << (pos +1) << " That Is " << min_cgpa << endl;
    cout << "\t" << "... Student With Highest CGPA ..." << endl;
    cout << "\t" << student[pos].RollNo << "\t"  << student[pos].CGPA << endl;
}

int main()
{
    const int size = 3;
    Student *student = new Student[size];
    student->GetData(student, size);
    student->PrintData(student, size);
    student->is_greater(student, size);
    return 0;
}


The statement cout << "\t" << student[pos].RollNo << "\t" << student[pos].CGPA << endl;
cause the undefined behavior .. The value of POS is correct but when i use as index it will print the garbage ..
After using debugger and watch windows, i still unable to track the error ..

Simply Sir I unable to understand the Rocket Science Behind That .. ??!

Please ANYONE ELABORATE THIS ..
I Know There Are A Lot Of Methods To Do That ..
But I Want It In My Way ..

Suggestions Would Be Appreciated .. !!!!
Posted
Updated 14-Nov-13 10:39am
v6

Your real problem is not exactly the pointer arithmetic (you are actually using much index arithmetic, functionally the same, but safer, and you use it correctly), your problem is the way you ask the question. Look thoroughly: in your code sample, you have student[pos].RollNo (which is correct and should work), in the text under code sample you have student[pos+1].RollNo, which is incorrect and would cause the problem.

[EDIT]

As I just noticed, your code sample is also wrong: you should not do *(student)++, as it creates the same problem.

[END EDIT]

There is nothing wrong with you use of indexes, but +1 creates the situation where you try to access the object behind the end of the array. You run the loop in 0 .. 2, and the size of the array is 3. So far, so good. But the index pos+1 would become 3, which is past the memory allocated for the array. That's all.

Next time, please make sure that the code shown in your code sample is exactly the same as the one you run. Use copy-paste without any modifications to the code which manifests the problem in question.

—SA
 
Share this answer
 
v7
Comments
Usman Hunjra 14-Nov-13 16:29pm    
yes .. i'm sorry that's the writing mistake ..
sir consider this .. student[pos].RollNo and student[pos].CGPA..
But it not works .. :(
Sergey Alexandrovich Kryukov 14-Nov-13 16:35pm    
See also Solution 2. Did you also remove *(student)++?
Please see my comment to that solution.
—SA
Usman Hunjra 14-Nov-13 17:07pm    
yes sir .. :/
nv3 14-Nov-13 18:45pm    
Sergey, someone didn't like our answers ;-) What a weird world!
Sergey Alexandrovich Kryukov 14-Nov-13 18:58pm    
I know, it happens all the time, without apparent reason (or worse, by suspected easy-to-guess reasons). It does not worth caring about... :-)
I, by the way, up-voted your as I wanted to do it in first place but apparently forgot to do so; thank you for reminding me, it just happens...
By the way, in some cases, people voted 1 just because they thought 1 is for the best answers and is 5 for the worst, as they told about it later... :-)
—SA
If you are looking for the maximum CGPA, why do you name your variable min_cgpa? Just to confuse the reader -- or yourself :-)

By just reading your code I can spot a couple of things:

(a) Ask yourself what happens when the first student is the one with the maximum CGPA. Does pos get assigned any value in that case?

(b) Why would you want to access student[pos+1] if pos is the index of the maximum, as you suggest in the text below your code? pos could have the value of size-1 and in that case you would be accessing an undefined object.

(c) What do you intend with the line
C++
*(student)++; // misleading!

You probably meant to write:
C++
++student;

And even simpler and more intuitive (and safer) would be to access student[i] in those loops. Otherwise, if you increment the student pointer one time too much or too little you will end up accessing the wrong objects.

And, of course, this is no homework :-)
 
Share this answer
 
Comments
Usman Hunjra 14-Nov-13 16:31pm    
Yes ,, i understand sir ..
Sir please consider POS not POS+1 .. I've updated .. but the code doesn't work ..
Sergey Alexandrovich Kryukov 14-Nov-13 16:41pm    
No, you consider it. :-)
After all, run it under the debugger and see what is going on. You trying to access behind the array in different ways, but you should eliminate all ways of doing so.
—SA
Usman Hunjra 14-Nov-13 16:37pm    
yes i understand the point a) that should be (> =) to ..
but still that statement printing garbage ..
Sergey Alexandrovich Kryukov 14-Nov-13 16:39pm    
You are perfectly right, but using ++student will get student[POS] past the array.
OP simply jungles with two related bugs each of them leads to trying to access the memory after the array.
The code will work if
1) Neither *(student)++ nor ++student it used;
2) student[POS] is used, not student[POS+1] (but I don't know why doing it at all; not the best code style anyway...
—SA
Usman Hunjra 14-Nov-13 16:41pm    
yes,, i remove *(student)++ and pos+1 too ..
but still printing garbage value .. :( :(
Here are some suggestions:

- stop messing about with pointers, arrays and dynamic memory allocation. They're not worth the hassle 99% of the time and this isn't the 1% that is

- learn about the standard library - std::vector and std::max_element makes your problem relatively trivial...

- ...but only if you know how to write a class that supports their operation. Check out how to write operator<.

Sooner or later in your C++ education you'll have to use pointers for something. Until then treat them like a bottles of lager spiked with rohypnol - use them and you'll wake up feeling dirty with a pain in your bum. If you feel the urge to use a pointer then consider why you want to use it and:

- if you want to represent ownership consider using a smart pointer - std::unique_ptr or std::shared_ptr are good bets

- if you want to represent iteration across a collection then consider using an iterator

- if you want to use a dynamically allocated array then consider using a std::vector instead

Get your head around this lot and you'll be able to turn a solution out in less than 20 minutes.
 
Share this answer
 
Comments
Usman Hunjra 15-Nov-13 3:34am    
Thanks Bro ..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900