# subject

Write a program to realize the following functions: a study group has 5 people, each of whom has the test scores of Math, C and Database. Calculate the average scores of each subject and all scores of the group and output them to the screen.

# Problem solving steps

(1) Give the structure; (2) Analysis variables; (3) Calculate the average score of the total / all subjects; (4) Output results;

# Java

```import java.util.Scanner;

public class Demo {
static class Student {
private float Math;
private float C;
private float Database;
private float Sum = 0;

public void SetValue(float Math, float C, float Database) {
this.Math = Math;
this.C = C;
this.Database = Database;
Sum = Math + C + Database;
}

public float GetMath() {
return Math;
}

public float GetC() {
return C;
}

public float GetDataBase() {
return Database;
}

public float GetSum() {
return Sum;
}
}

public static void main(String[] args) {
float Math = 0, C = 0, Database = 0, Total = 0, MathSum = 0, CSum = 0, DatabaseSum = 0;
Student[] student = new Student;
Scanner user = new Scanner(System.in);
for (int i = 0; i < 5; i++)
{
System.out.format("Please enter the math score of the %d student:", i + 1);
Math = user.nextFloat();
System.out.format("Please enter the C language score of the %d student::", i + 1);
C = user.nextFloat();
System.out.format("Please enter the database score of the %d student::", i + 1);
Database = user.nextFloat();
student[i] = new Student();
student[i].SetValue(Math, C, Database);
}
for (int i = 0; i < 5; i++) {
MathSum += student[i].GetMath();
CSum += student[i].GetC();
DatabaseSum += student[i].GetDataBase();
Total += student[i].GetSum();
}
System.out.format("MathAverage=%f     CAverage=%f     DatabaseAverage=%f", MathSum / 5.0, CSum / 5.0, DatabaseSum / 5.0);
System.out.format("\nTotalScoreAverage=%.3f", Total / 5.0);
}
}```
copy

## explain

1. Java is a high-level language. It discards the error prone pointers and some unnecessary language structures (including structures) in C/C++. Therefore, if we use the Java language to solve this problem, we need to use classes and objects.
2. We include its attributes and methods in the class. It is not easy to think of the operation that the grades of the three subjects are received from the keyboard and need to be cycled five times: use the object array Student[] student = new Student; And instantiate it to dynamically receive data.
3. In classes and objects, we add methods to solve problems as long as there are requirements. For example, the method of returning the scores of three subjects is set in this question to calculate the average score of a single subject and the average score of the total score.
4. The object array needs to be instantiated. student[i] = new Student();, Otherwise, an error is reported: null pointer error!!!

# C language

```#include <stdio.h>

struct Student
{
float Math;
float C;
float Database;
} student;

int main()
{
float Math, C, Database, Sum;
for (int i = 0; i < 5; i++)
{
printf("please enter the three door subject grades of the first %d name classmate:", i + 1);
scanf("%f%f%f", &student[i].Math, &student[i].C, &student[i].Database); //Attention&
Sum += student[i].Math + student[i].C + student[i].Database;
Math += student[i].Math;
C += student[i].C;
Database += student[i].Database;
}
printf("MathAverage=%f     CAverage=%f     DatabaseAverage=%f", Math / 5.0, C / 5.0, Database / 5.0);
printf("\nTotalAverage=%f", Sum / 5.0);
return 0;
}```
copy

## explain

1. The key point is how to distinguish the grades of different subjects from the students. Here we use the structure array method: the structure array student with 5 elements contains three variables Math, C and Database corresponding to different subject grades. With this in mind, the problem becomes very simple. As long as the input value is received, the calculation can be carried out.
2. In the calculation, it should be considered carefully. The result may be of float type. The / sign used to calculate the average value is an integer, so when we do division, the divisor should be 5.0 instead of 5.

### Similarities and differences:

The following question is similar to this one. When using scanf(), if the structure contains an array, you need to pay attention to whether it needs to exist. Compare the source code to further deepen the understanding.

```#include <stdio.h>

struct student
{
int num;
char name;
float score;
};

void main()
{
struct student s; //struct student is equivalent to int, in essence: it writes a data type
int i, j;
float sum, avg;
for (i = 0; i < 3; i++)
{
sum = 0;
scanf("%d", &s[i].num); // num is an int type. It is a common variable and does not represent an address
scanf("%s", s[i].name); // &s.name is not required because the array name name represents the first address
printf("Please enter the scores of three subjects:\n");
for (j = 0; j < 3; j++)
{
scanf("%f", &s[i].score[j]);
sum = sum + s[i].score[j];
}
avg[i] = sum / 3.0;
}
printf("Output information:\n");
for (i = 0; i < 3; i++)
printf("Student ID:%d,full name:%s,average:%.2f\n", s[i].num, s[i].name, avg[i]);
}```
copy

Posted by jolinar on Fri, 03 Jun 2022 07:33:40 +0530