Sum the array values

Consider,

typedef struct{
int english,maths,science;
string name;
}marks_t;

initial begin
marks_t sheet;
sheet=‘{’{80,90,70,“John”},'{60,70,80,“Peter”}…};

Qn1: Is it possible to add the marks of all the 3 subject,to find whose total is greater than 200?
Qn2: How to sort the students who scored above 60 in english and maths?

In reply to Design Engineer:
For the sum you can refer to this 7.12.3 Array reduction methods .


class A;
    typedef struct {
    int english,maths,science;
    string name;
    } mark_t;
    mark_t sheet ;
    mark_t q[$];
    mark_t result[$];
    int val ;
   
 task process_stuct_data();
      sheet = '{50,60,80,"john"};
      q.push_back(sheet);
      sheet = '{60,70,50,"peter"};
      q.push_back(sheet);
      sheet = '{70,60,90,"nathan"};
      q.push_back(sheet);
      
      result = q.find with ( item.english >= 60 && item.maths >= 60);
      // find the sum 
      foreach(q[i]) begin 
        val = q.sum with ( item.index == i ? item.english + item.maths + item.science : 0);   
    
        if(val > 200) begin 
          
          $display("%p",q[i]);
          
        end 
    endtask 
  endclass

In reply to kddholak:

Sorry,as i am new to SV,i don’t know how to proceed,I don’t know how to display the array and result

Plz find my code below and help me to work it out

module eg;
class A;
typedef struct {
int english,maths,science;
string name;
} mark_t;
mark_t sheet ;
mark_t q[]; mark_t result[];

task process_stuct_data();
sheet = '{50,60,80,“john”};
q.push_back(sheet);
sheet = '{60,70,50,“peter”};
q.push_back(sheet);
sheet = '{70,60,90,“nathan”};
q.push_back(sheet);
$display(“value=%p”,sheet);

  result = q.find with ( item.english >= 60 && item.maths >= 60);

$display(“value=%p”,result);

endtask 

A a1 = new ;
A a1=sheet;
foreach(a1.A[i])
$display(“%b”,a1.A[i]);`
endmodule

In reply to Design Engineer:

I updated the array sum in original post. you will have to refer to LRM section of array.

In reply to kddholak:

Thanks a lot for ur support @ kddholak

In reply to kddholak:

Qn1:How to arrange the student mark list in descending order?
Qn2:How to find the student who has more mark in maths than english?
Qn3:How to find the first student with more than 50 marks in english?

In reply to Design Engineer:
you should be able to solve the rest of the question based on below example
result = q.find with ( item.english >= 60 && item.maths >= 60);
7.12 Array manipulation methods, you should refer this chapter in LRM. Try to come with your solution.

In reply to kddholak:

Ya i have gone through the array manipulation methods.It helps me.
But i am stuck with descending order it.
I just came to know i have to use rsort()
But don"t know how to proceed.

Here is my code

module data;
typedef struct {
int english,maths,science;
string name;
} mark_t;
mark_t sheet ;
mark_t q[]; mark_t result[];
int total;
int i;
initial begin
q=‘{’{50,60,80,“john”},‘{60,70,50,“peter”},’{70,60,90,“nathan”}};
$display(“student list =%p”,q);

foreach(q[i]) begin 
    total = q.sum with ( item.index == i ? item.english + item.maths + item.science : 0);   

  if(total > 200) begin 

      $display("%p",q[i]);
    
    result = q.find with ( item.english >= 60 && item.maths >= 60);
    $display(" above 60 in english,maths=%p",result);
    
    result=q.find with (item.english>item.maths);
    $display ("more marks in english=%p",result);
    
    result=q.find_first with (item.english>50);
    $display("english more than 50=%p",result);
    
   **foreach(q[i])begin
      q[i].rsort();** //How to do descending??
      
    end
      
    
    end
end

end
endmodule

In reply to Design Engineer:
arrys all elements (stuck) will be sorted based on each argument


q.rsort with ({item.english, item.maths , item.science});

In reply to kddholak:

It works
Thanks a lot @ kddholak