#Exception thrown?

1 messages · Page 1 of 1 (latest)

deep crest
#

I've been at this for several hours and I'm still very confused. I'm trying to make a sorting function using a pointer that's pointing at a struct called Student. I'm still a bit new with pointers, so I'm not completely sure if my code is correct. I figured it should be able to swap the student's scores in ascending order with temp, since that works just fine with everything. Except pointers, anyways. Wouldn't it be (students + i)->score = (students + j->score and vice versa to swap the two?

#include <iostream>
#include <string>

using namespace std;

struct Student
{
    string name;
    int score;
};

void sortScores(Student* students, int numStudents)
{
    int temp;

    for (int i = 0; i < numStudents; i++)
    {
        for (int j = i + 1; i < numStudents; j++)
        {
            if ((students + j)->score < (students + i)->score)
            {
                temp = (students + i)->score;
                (students + i)->score = (students + j)->score;
                (students + j)->score = (students + temp)->score;
            }
        }

    }
}```
novel quarryBOT
#

Helper please have a look, thanks.

#

Changed the category to C|C++.

#

<@&987246683568103514> please have a look, thanks.

upbeat vapor
deep crest
#

The assignment only requests swapping the scores, I dunno

upbeat vapor
#

for (int j = i + 1; j < numStudents; j++)

#

line 18

deep crest
#

What about it..?

upbeat vapor
#

#include <iostream>
#include <string>

using namespace std;

struct Student
{
string name;
int score;
};

void sortScores(Student* students, int numStudents)
{
int temp;

for (int i = 0; i < numStudents; i++)
{
    for (int j = i + 1; j < numStudents; j++)
    {
        int a=(students+j)->score;
        int b=(students+i)->score;
        if (a < b)
            swap(*(students+i),*(students+j));
    }

}

}
int main(){
Student a[5];
a[0].name="james1";
a[0].score=33;
a[1].name="james2";
a[1].score=3;
a[2].name="james3";
a[2].score=31;
a[3].name="james4";
a[3].score=234;
a[4].name="james5";
a[4].score=23;
sortScores(a,5);
cout<<a[4].score;
return 0;
}

#

this is the whole code

#

it works

deep crest
#

That's not with pointers though, in the main block

upbeat vapor
#

a is an address?

upbeat vapor
deep crest
#

With three ` at the start and end

#

Same key as the ~

upbeat vapor
#

`#include <iostream>
#include <string>

using namespace std;

struct Student
{
string name;
int score;
};

void sortScores(Student* students, int numStudents)
{
int temp;

for (int i = 0; i < numStudents; i++)
{
    for (int j = i + 1; j < numStudents; j++)
    {
        int a=(students+j)->score;
        int b=(students+i)->score;
        if (a < b)
            swap(*(students+i),*(students+j));
    }

}

}
int main(){
Student a[5];
a[0].name="james1";
a[0].score=33;
a[1].name="james2";
a[1].score=3;
a[2].name="james3";
a[2].score=31;
a[3].name="james4";
a[3].score=234;
a[4].name="james5";
a[4].score=23;
sortScores(a,5);
cout<<a[4].score;
return 0;
}
`

#

yeah try this

deep crest
#
#include <iostream>
#include <string>

using namespace std;

struct Student
{
    string name;
    int score;
};

void sortScores(Student* students, int numStudents)
{
    int temp;

    for (int i = 0; i < numStudents; i++)
    {
        for (int j = i + 1; i < numStudents; j++)
        {
            if ((students + j)->score < (students + i)->score)
            {
                temp = (students + i)->score;
                (students + i)->score = (students + j)->score;
                (students + j)->score = (students + temp)->score;
            }

    }

    void inputData(Student * students, int numStudents)
    {
        for (int i = 0; i < numStudents; i++)
        {
            cout << "Student " << i + 1 << ":\n";
            cout << "Name: ";
            cin.ignore();
            getline(cin >> ws, (students + 1)->name);
            cout << "Score: ";
            cin >> (students + 1)->score;
        }
    }

float calculateAvg(const Student* students, int numStudents)
{
    float sum = 0, average = 0;

    for (int i = 0; i < numStudents; i++)
    {
        sum += (students + i)->score;
    }

    return average = sum / numStudents;
}

int main()
{
    int numStudents;

    cout << "Enter the number of students: ";
    cin >> numStudents;

    Student* students = new Student[numStudents];

    inputData(students, numStudents);
    sortScores(students, numStudents);
    float average = calculateAvg(students, numStudents);

    //display
    for (int i = 0; i < numStudents; i++)
    {
        cout << "Student " << i + 1 << ":" << endl;
        cout << "Name: " << (students + 1)->name << endl;
        cout << "Score: " << (students + 1)->score << endl;
    }

    cout << "Average Score: " << average;

    delete[] students;
}```
novel quarryBOT
# deep crest ```c++ #include <iostream> #include <string> using namespace std; struct Stude...

Detected code, here are some useful tools:

Formatted code
#include<iostream> #include<string> using namespace std;
struct Student {
  string name;
  int score;
};
void sortScores(Student * students, int numStudents) {
  int temp;
  for (int i = 0; i < numStudents; i++) {
    for (int j = i + 1; i < numStudents; j++) {
      if ((students + j) -> score < (students + i) -> score) {
        temp = (students + i) -> score;
        (students + i) -> score = (students + j) -> score;
        (students + j) -> score = (students + temp) -> score;
      }
    }
    void inputData(Student * students, int numStudents) {
      for (int i = 0; i < numStudents; i++) {
        cout << "Student " << i + 1 << ":\n";
        cout << "Name: ";
        cin.ignore();
        getline(cin >> ws, (students + 1) -> name);
        cout << "Score: ";
        cin >> (students + 1) -> score;
      }
    }
    float calculateAvg(const Student * students, int numStudents) {
      float sum = 0, average = 0;
      for (int i = 0; i < numStudents; i++) {
        sum += (students + i) -> score;
      }
      return average = sum / numStudents;
    }
    int main() {
      int numStudents;
      cout << "Enter the number of students: ";
      cin >> numStudents;
      Student * students = new Student[numStudents] ;
      inputData(students, numStudents);
      sortScores(students, numStudents);
      float average = calculateAvg(students, numStudents);
      //display
      for (int i = 0; i < numStudents; i++) {
        cout << "Student " << i + 1 << ":" << endl;
        cout << "Name: " << (students + 1) -> name << endl;
        cout << "Score: " << (students + 1) -> score << endl;
      }
      cout << "Average Score: " << average;
      delete[] students;
    }
deep crest
#

I've got this

upbeat vapor
#

void inputData(Student * students, int numStudents)

#

this function doesn't seem to work either

deep crest
#

The inputData function works just fine

#

It's only the last line of the sortScores function that's throwing an exception

upbeat vapor
#

this worked

#

I added notes where i changed the code

upbeat vapor
#
#include <string>

using namespace std;

struct Student
{
    string name;
    int score;
};

void sortScores(Student* students, int numStudents)
{
    for (int i = 0; i < numStudents; i++)
    {
        for (int j = i + 1; j < numStudents; j++)//I changed i to j cause that's how insertion sort works 
        {
            if ((students + j)->score < (students + i)->score)
            swap(*(students + j),*(students + i));//here we swap names related to those scores too

    }
}
}
    void inputData(Student * students, int numStudents)
    {
        for (int i = 0; i < numStudents; i++)
        {
            cout << "Student " << i + 1 << ":\n";
            cout << "Name: ";
            cin.ignore();
            getline(cin >> ws, (students + i)->name);//students plus i:to set all the values for students
            cout << "Score: ";
            cin >> (students + i)->score;//same 
        }
    }

float calculateAvg(const Student* students, int numStudents)
{
    float sum = 0, average = 0;

    for (int i = 0; i < numStudents; i++)
    {
        sum += (students + i)->score;
    }

    return average = sum / numStudents;
}

int main()
{
    int numStudents;

    cout << "Enter the number of students: ";
    cin >> numStudents;

    Student* students = new Student[numStudents];

    inputData(students, numStudents);
    sortScores(students, numStudents);
    float average = calculateAvg(students, numStudents);

    //display
    for (int i = 0; i < numStudents; i++)
    {
        cout << "Student " << i + 1 << ":" << endl;
        cout << "Name: " << (students + i)->name << endl;//students plus i:to set all the values for students;and not just assign plus 1's value for all
        cout << "Score: " << (students + i)->score << endl;//same
    }

    cout << "Average Score: " << average;

    delete[] students;
}```
deep crest
#

Thank you, I compared it to my original program and it makes so much more sense now

novel quarryBOT
#

Closed the thread.