Classes and Arrays

Hello, I’m having issues with += operator to calculate the average of an array of type class. This is occurring in my Calculate_Average function. For some reason, it does not agree with the object I’ve created. I was wondering if someone could please point me in the right direction as to what mistake I’m making. The code is to populate the matrices and calculate the average of entries of the grades object matrix from class student (gr). Thanks in advance :

class Student
{
  float [] id;
  float [] gr;
}

void PopulateArray (Student [] ident, Student [] grad)
  {
 for(int i = 0; i < ident.length;i++)
    {
      ident[i].id = ident[i+1].id;
    }
 for(int j = 0; j < grad.length;j++)
    {
    grad[j].gr = grad[(int(random(40,100)))].gr;
    }
}
Float Calculate_Average (Student [] grades)
{
   float average = 0;
   for (int i = 0; i < grades.length; i ++)
   {
   average += grades[i];
   }
   print(average);
   return average;
}
void setup ()
{
  Student [] grades = new Student [10];
  Student [] ID = new Student [10];
  PopulateArray(ID, grades);
  Calculate_Average(grades);
  println(grades.gr);
}
1 Like

Oh.

Here are a lot of misunderstandings of things regarding class and objects

The complete layout / architecture is wrong.

That’s totally normal when starting with object oriented programming, I had it too!!!

Please consider to read the tutorial about objects: https://www.processing.org/tutorials/objects/

And this https://github.com/Kango/Processing-snippets/wiki/Variables,-Arrays-and-object-oriented-programming

long story short:

In setup(): You want to have an array of students. So the array has the name students and it’s of type class Student. So no need to have Student [] grades AND Student [] ID. Instead: Student [] students.

Now, the class represents ONE student. Not all of them. (All of them are in the array I mentioned above!). Since one student only has one ID and one grade, no arrays in the class but just:

class Student
{
  int id=0;
  int gr=0;
  String name="";
}

In your line average += grades[i]; you would add one Student to average. Bad.

Also, you call the function PopulateArray (Student [] ident, Student [] grad) with 2 arrays.
As mentioned when speaking about setup() that’s not necessary. Instead just pass students which is an array of students and each of them holds his or her ID and grade.

So instead of doing all different arrays, have only one array. Data is inside one Student object.

Chrisir



class Student
{
  int id=0;
  int gr=0;
  String name="";
}


// ==================================================================

void PopulateArray (Student [] ident)
{
  for (int i = 0; i < ident.length; i++)
  {
    ident[i] = new Student();
    ident[i].id = 3;
  }

  //
  for (int j = 0; j < ident.length; j++)
  {
    ident[j].gr = int(random(15));
  }
}


Float Calculate_Average (Student [] ident)
{
  float average = 0;
  for (int i = 0; i < ident.length; i ++)
  {
    average += ident[i].gr;
    println(ident[i].gr);
  }
  float result=(average/ident.length);
  println("---->>>>> " + result);
  return result;
}

//--------------------------------------------------------------------

void setup ()
{
  size(700, 99);
  Student [] students = new Student [10];
  PopulateArray(students);
  Calculate_Average(students);
  //  println(grades.gr);
}
//
1 Like