我作为家庭作业的一部分完成了练习。一切正常,除了3个值在打印中获得不同的值。有趣的是,这些值正确显示在调试器中。 通常,它是一个程序,可以像分数一样从用户读取数据,并对它们求和并返回平均值。输入出现在代码中。如果您解决问题,我会很高兴。
正确的输出: 80 2 7 8 304877772 1
现有输出: 80 2 1.75 9.24243e + 008 304877772 10
#include <iostream>
#include <iomanip>
#include <cstdlib>
//------------------------------------------------
using std::cin;
using std::cout;
using std::endl;
using std::setw;
//------------------------------------------------
const int NUM_OF_GRADES = 7;
const int CLASS_SIZE = 10;
const int NUM_OF_COURSES = 10;
//------------------------------------------------
struct Stud
{
int _stud_id;
int _grades[NUM_OF_GRADES];
double _average;
double _height;
Stud():_average(0),_height(0){}
};
struct Course
{
int _course_id;
double _credit_points;
unsigned int _hours_of_course;
Course():_credit_points(0){}
};
//------------------------------------------------
void read_a_student(struct Stud &s, const int s_id);
void read_a_courses(struct Course &c
,const int c_id,unsigned int h_course[]);
void read_student(struct Stud studs[],int &num_of_s);
void read_courses(struct Course courses[]
,unsigned int h_course[NUM_OF_COURSES]);
void read_studs_avg(struct Stud studs[]
,double avg[CLASS_SIZE],double &height_avg,int &num_of_s);
void calc_studs_avg(struct Stud &s,double &temp_g
,double &height_avg,double avg[],int &num_of_s);
void read_courses_avg(struct Course courses[]
,unsigned int h_course[NUM_OF_COURSES],double &credit_p_avg);
void calc_courses_avg(struct Course &c,double &temp_h
,unsigned int h_course[],double &credit_p_avg,int &index);
void sort_studs_calc(const struct Stud s[],double avg[CLASS_SIZE]
,unsigned int h_course[NUM_OF_COURSES],double &height_avg
,double &total_avg,int &id_high_avg
,const int num_of_s,int data_s_arr_4_gen_f[],double data_c_arr_4_gen_f[]);
void sort_courses_calc(const unsigned int h_course[]
,double &total_c_avg,double &credit_p_avg,int &index_l
,double data_c_arr_4_gen_f[],int data_s_arr_4_gen_f[]);
void generic_cast_n_print(void* data_c_arr_4_gen_f,void* data_s_arr_4_gen_f);
void print_check (double data_c_arr_4_gen_f[4],int data_s_arr_4_gen_f[2],
double total_avg,double height_avg,double credit_p_avg
,double total_c_avg);//, int id_high_avg,int index_l);
//------------------------------------------------
int main()
{
struct Stud studs[CLASS_SIZE];
struct Course courses[NUM_OF_COURSES];
double avg[CLASS_SIZE] = {0};
double data_c_arr_4_gen_f[4]; //data course arr for generic cast function
double height_avg;
double credit_p_avg;
double total_avg = 0; //total student average
double total_c_avg = 0; //total course hours average
unsigned int h_course[NUM_OF_COURSES] = {0};
int data_s_arr_4_gen_f[2]; //data student arr for generic cast function
int id_high_avg;
int num_of_s = 0;
int index_l; //index of longest course
read_student(studs,num_of_s);
read_courses(courses,h_course);
read_courses_avg(courses,h_course,credit_p_avg);
read_studs_avg(studs,avg,height_avg,num_of_s);
sort_studs_calc(studs,avg,h_course
,height_avg,total_avg,id_high_avg,num_of_s
,data_s_arr_4_gen_f,data_c_arr_4_gen_f);
sort_courses_calc(h_course,total_c_avg,credit_p_avg
,index_l,data_c_arr_4_gen_f,data_s_arr_4_gen_f);
generic_cast_n_print(data_c_arr_4_gen_f,data_s_arr_4_gen_f);
return EXIT_SUCCESS;
}
//------------------------------------------------
void read_student(struct Stud studs[],int &num_of_s)
{
int s_id;
cin >> s_id;
for (int i = 0; i < CLASS_SIZE && s_id != 0; i++)
{
read_a_student(studs[i],s_id);
cin >> s_id;
num_of_s++; // here
}
}
//------------------------------------------------
void read_a_student(struct Stud &s, const int s_id)
{
int i;
int grades;
s._stud_id = s_id;
for (i = 0; i < NUM_OF_GRADES; i++)
{
cin >> grades;
if(grades != -1)
s._grades[i] = grades;
else
break;
}
cin >> s._height;
}
//------------------------------------------------
void read_courses(struct Course courses[],unsigned int h_course[])
{
int c_id;
cin >> c_id;
for (int i = 0; i < NUM_OF_COURSES && c_id != 0; i++)
{
read_a_courses(courses[i],c_id,h_course);
cin >> c_id;
}
}
//------------------------------------------------
void read_a_courses(struct Course &c
,const int c_id, unsigned int h_course[])
{
static int index = 0;
c._course_id = c_id;
cin >> c._credit_points;
cin >> c._hours_of_course;
h_course[index] = c._hours_of_course;
index++;
}
//------------------------------------------------
void read_studs_avg(struct Stud studs[]
,double avg[CLASS_SIZE],double &height_avg,int &num_of_s)
{
double temp_g = 0;
height_avg = 0;
for (int i = 0; i <= num_of_s; i++)
calc_studs_avg(studs[i],temp_g,height_avg,avg,num_of_s);
}
//------------------------------------------------
void calc_studs_avg(struct Stud &s,double &temp_g
,double &height_avg,double avg[],int &num_of_s)
{
static int index = 0;
int i;
temp_g = 0;
for(i=0; i <= num_of_s; i++)
temp_g += s._grades[i];
s._average = temp_g/i;
avg[index] = s._average;
height_avg += s._height;
index++;
}
//------------------------------------------------
void read_courses_avg(struct Course courses[]
,unsigned int h_course[],double &credit_p_avg)
{
static int index = 0;
double temp_h = 0;
credit_p_avg = 0;
for (int i = 0; i < NUM_OF_COURSES; i++)
calc_courses_avg(courses[i],temp_h,h_course,credit_p_avg,index);
}
//------------------------------------------------
void calc_courses_avg(struct Course &c,double &temp_h,
unsigned int h_course[],double &credit_p_avg,int &index)
{
temp_h = 0;
credit_p_avg += c._credit_points;
temp_h += c._hours_of_course;
h_course[index] = temp_h;
index++;
}
//------------------------------------------------
void sort_studs_calc(const struct Stud s[],double avg[CLASS_SIZE],
unsigned int h_course[NUM_OF_COURSES],double &height_avg
,double &total_avg,int &id_high_avg,const int num_of_s
,int data_s_arr_4_gen_f[],double data_c_arr_4_gen_f[])
{
int index_avg = 0;
int i;
double highest_avg = avg[1];
for(i=0; i<num_of_s; i++)
{
total_avg += avg[i];
if(avg[i] > highest_avg)
{
highest_avg = avg[i];
index_avg = i;
}
}
total_avg = total_avg/i;
id_high_avg = s[index_avg]._stud_id;
height_avg = height_avg/i;
data_s_arr_4_gen_f[0] = id_high_avg;
data_c_arr_4_gen_f[0] = total_avg;
data_c_arr_4_gen_f[1] = height_avg;
}
//------------------------------------------------
void sort_courses_calc(const unsigned int h_course[]
,double &total_c_avg,double &credit_p_avg,int &index_l
,double data_c_arr_4_gen_f[],int data_s_arr_4_gen_f[])
{
int i;
unsigned int l_c = h_course[1]; //l_c = longest course
index_l = 1;
for(i=0; h_course[i] != 0; i++)
{
total_c_avg += h_course[i];
if(h_course[i] > l_c)
{
l_c = h_course[i];
index_l = i;
}
}
total_c_avg = total_c_avg/i;
credit_p_avg = credit_p_avg/i;
data_s_arr_4_gen_f[1] = index_l;
data_c_arr_4_gen_f[2] = credit_p_avg;
data_c_arr_4_gen_f[3] = total_c_avg;
}
//------------------------------------------------
void generic_cast_n_print(void * data_c_arr_4_gen_f,void * data_s_arr_4_gen_f)
{
double *data_c = (double *)data_c_arr_4_gen_f;
int *data_s = (int *)data_s_arr_4_gen_f;
for(int i=0; i<4; i++)
cout << data_c[i] << " ";
for(int i=0; i<2; i++)
cout << data_s[i] << " ";
cout << endl;
} ```