
2道C语言编程题,英语的!追加40分! 30
1.Problem1a.Nameyourprogramfilelab11a.c.b.Createthefilebelow,lab11a.in.-107-149-17232...
1.Problem 1
a. Name your program file lab11a.c.
b. Create the file below, lab11a.in.
-10 7 -1 4 9 -17 23 28 -37 38 43 45 46
c. Read the input file until EOF is reached, counting the number of elements in the
array as you read them in. Declare the array to have a maximum dimension
MAX_D of 12. If there are more than MAX_D elements to scan, ignore
everything after the MAX_Dth element. For the file above, the last integer will
not be scanned. Use while loop to read in the next integer:
d. Write the function select_sort() to put the input integer values in
descending order. The arguments are the integer array to the sorted and the length
of the array.
e. As part of your solution write and call the function get_imax(), which is a
function that returns the index where the maximum value occurs over a sub-array
of integers from index i_init to index i_last.
f. As part of the select sort algorithm, write and call a function swap2() that has as
input/output parameter two integers that have their values swapped.
g. Write the integers in descending order to the output file lab11a.out. The file
format is identical to lab11a.in.
2. Problem2: Test Score Averages
a. Name your program file lab11b.c.
b. For this problem, we will have an input file with a 2-D array of test scores for a class
of students. Students are identified by an ID number (an integer) followed by three
exam scores (doubles). A sample input file hw11b.in is shown below:
10707 57.9 64.6 98.3
10004 69.4 83.7 92.1
10995 75.2 98.3 69.5
11005 83.2 79.6 89.1
10664 84.1 91.0 79.0
c. Write a function scan_scores()to scan in the ID numbers to a 1-D integer array
and the test scores into a 2-D double array. Return the number of students in the
class, or -1 if there is an error reading the file. As you are scanning in the values, be
sure not to exceed the maximum array dimension for the number of students in the
class, MAX_S, which we will set equal to 100.
c. Write a function avg_stud_score() to compute the average value that each
student scored on the three exams. Store these averages in another double array.
d. Write the function test_ max_avg() to compute the maximum and average
values for each test. Store these values in two separate double arrays, say,
test_max[] and test_avg[].
e. Write the function display_scores() to display the inputs and outputs in a
table. For the input file about, the output to the screen would look like:
Stud ID Test 1 Test 2 Test 3 Avg
-------- -------- -------- -------- --------
10707 57.9 64.6 98.3 xx.x
10004 69.4 83.7 92.1 xx.x
10995 75.2 98.3 69.5 xx.x
11005 83.2 79.6 89.1 xx.x
10664 84.1 91.0 79.0 xx.x
Max: 84.1 98.3 98.3
Avg: yy.y yy.y yy.y 展开
a. Name your program file lab11a.c.
b. Create the file below, lab11a.in.
-10 7 -1 4 9 -17 23 28 -37 38 43 45 46
c. Read the input file until EOF is reached, counting the number of elements in the
array as you read them in. Declare the array to have a maximum dimension
MAX_D of 12. If there are more than MAX_D elements to scan, ignore
everything after the MAX_Dth element. For the file above, the last integer will
not be scanned. Use while loop to read in the next integer:
d. Write the function select_sort() to put the input integer values in
descending order. The arguments are the integer array to the sorted and the length
of the array.
e. As part of your solution write and call the function get_imax(), which is a
function that returns the index where the maximum value occurs over a sub-array
of integers from index i_init to index i_last.
f. As part of the select sort algorithm, write and call a function swap2() that has as
input/output parameter two integers that have their values swapped.
g. Write the integers in descending order to the output file lab11a.out. The file
format is identical to lab11a.in.
2. Problem2: Test Score Averages
a. Name your program file lab11b.c.
b. For this problem, we will have an input file with a 2-D array of test scores for a class
of students. Students are identified by an ID number (an integer) followed by three
exam scores (doubles). A sample input file hw11b.in is shown below:
10707 57.9 64.6 98.3
10004 69.4 83.7 92.1
10995 75.2 98.3 69.5
11005 83.2 79.6 89.1
10664 84.1 91.0 79.0
c. Write a function scan_scores()to scan in the ID numbers to a 1-D integer array
and the test scores into a 2-D double array. Return the number of students in the
class, or -1 if there is an error reading the file. As you are scanning in the values, be
sure not to exceed the maximum array dimension for the number of students in the
class, MAX_S, which we will set equal to 100.
c. Write a function avg_stud_score() to compute the average value that each
student scored on the three exams. Store these averages in another double array.
d. Write the function test_ max_avg() to compute the maximum and average
values for each test. Store these values in two separate double arrays, say,
test_max[] and test_avg[].
e. Write the function display_scores() to display the inputs and outputs in a
table. For the input file about, the output to the screen would look like:
Stud ID Test 1 Test 2 Test 3 Avg
-------- -------- -------- -------- --------
10707 57.9 64.6 98.3 xx.x
10004 69.4 83.7 92.1 xx.x
10995 75.2 98.3 69.5 xx.x
11005 83.2 79.6 89.1 xx.x
10664 84.1 91.0 79.0 xx.x
Max: 84.1 98.3 98.3
Avg: yy.y yy.y yy.y 展开
3个回答
展开全部
第一题:
A、以lab11a.c作为你的程序名
C、创建内容如下,名为lab11a.in的文件(你这里写错了,序号a过了应该是序号b,不知道你怎么搞的,直接到c了,管它呢,照着你抄)
-10 7 -1 4 9 -17 23 28 -37 38 43 45 46
D、读取输入文件,直到遇到EOF,统计数组中你读入的元素的个数。声明数组的最大容量为MAX_D,值为12。如果有多余MAX_D个元素被读到,则忽略第MAX_D个后的元素。以上面的文件为例,最后一个整数将不会被读到。使用while循环读取下一个数据。
E、写一个为名select_sort()的函数来实现降序排列,参数为这个整形数组和数组的长度
F、写一个名为get_imax()的函数,并且调用之,用来返回从索引值i_init到索引值i_last之间,最大值元素的索引值,作为你解决方案的一部分。
G、写一个名为swap2()的函数,该函数交换两个整数的值,并调用之,作为选择排序算法的一部分。
H、以降序方式,将这些整数写到lab11a.out文件,这个文件的格式如lab11a.in。
/****************程序如下*****************/
//lab11a.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define MAX_D 12
Int select_sort(int *arr, int lenth);
Int get_imax(int *arr, int i_init, int i_last, int lenth);
void swap2(int *a, int *b);
int main()
{
Int arr[MAX_D];
int ret;
int i,j;
char number[5];
FILE *in = fopen(“./ lab11a.in”, “a+”);
FILE *out = fopen(“./ lab11a.out”, “a+”);
fwrite(“-10 7 -1 4 9 -17 23 28 -37 38 43 45 46”, sizeof(“-10 7 -1 4 9 -17 23 28 -37 38 43 45 46”), 1, in);
ret = fclose(in);
if (ret != 0)
{
perror(“fclose(in)”);
exit(errno);
}
//只读方式打开输入文件
in = fopen(“./ lab11a.in”, “r”);
//以空格符或EOF(文件结束符)为界,读取输入文件内容,读取MAX_D次
i = 0;
while (i < MAX_D)
{
bzero(number, sizeof(number));
j = 0;
do
{
number[j++] = fgetc(in);
}
while (number[j-1] != ‘ ’ || number[j-1] != EOF)
number[j-1] = ‘\0’;
//atoi函数,将字符串转化为整数
arr[i] = atoi(number);
i++;
}
//降序排列,并出错处理。
ret = select_sort(arr, MAX_D);
if (ret != 0)
{
perror(“select_sort(arr, MAX_D)”);
exit(errno);
}
//将arr数组的内容输出到输出文件。
for (i = 0; i < MAX_D; i++)
fprintf(out, “%d ”, arr[i]);
fclose(in);
fclose(out);
return 0;
}
int select_sort(int *arr, int lenth)
{
int i,j;
int max_index;
//对输入参数有效性进行检测,无效返回-1,代表出错
if (arr == NULL || lenth <= 0)
return -1;
//冒泡算法实现降序排列
for (i = 0; i < lenth-1; i++)
for(j = i; j <= lenth-1; j++)
{
max_index = get_imax(arr, j, j+1, max_d);
//出错处理
if (max_index < 0)
return -1;
if (j < max_index)
swap2(&arr[j], &arr[j+1]);
}
//返回0,表示成功(正规it产业都以逻辑0为成功)
return 0;
}
int get_imax(int *arr, int i_init, int i_last, int lenth)
{
int i;
int max_index = i_init;
//对输入参数有效性进行检测,无效返回-1,代表出错
if (arr = NULL || i_init >= i_last || i_last >= lenth || i_init < 0 || i_last <= i_init || lenth <= 0)
return -1;
for (i = i_init; i < i_last; i++)
{
if (arr[i] <= arr[i+1])
max_index = i+1;
}
return max_index;
}
void swap2(int *a, int *b)
{
int c = *a;
*a = *b;
*b = c;
return ;
}
第二题:
a、将你的程序命名为lab11b.c
c、对这个问题,我们有一个输入文件,文件中的是一个班学生考试分数的二维数组。学生通过ID号(一个整数)来区分,紧接着是三个考试分数(double型)。一个正常的输入文件hw11b.in如下所示:
10707 57.9 64.6 98.3
10004 69.4 83.7 92.1
10995 75.2 98.3 69.5
11005 83.2 79.6 89.1
10664 84.1 91.0 79.0
d、写一个名为scan_scores()的函数,将ID号存入一个1维的整形数组中,将考试分数存入一个2维的double型数组中,该函数返回值为学生数量,如果返回值为-1,代表读取输入文件出错。当你读取数值时,要确定别越过数组的最大界限(以学生数量为界限),MAX_S值为100,作为学生数量。
e、写一个名为avg_stud_score()的函数,来计算出每个学生3门考试成绩的平均分。将平均分存入另外一个double型数组中。
f、写一个名为test_max_avg()的函数,来计算出每一门考试的最高分和平均分,将结果存入名为test_max[] 和 test_avg[]的double型数组中。
g、写一个名为display_scores()的函数,来显示输入文件的内容,以表格形式展示。对于输入文件,输出到屏幕的形式应如下:
Stud ID Test 1 Test 2 Test 3 Avg
-------- -------- -------- -------- --------
10707 57.9 64.6 98.3 xx.x
10004 69.4 83.7 92.1 xx.x
10995 75.2 98.3 69.5 xx.x
11005 83.2 79.6 89.1 xx.x
10664 84.1 91.0 79.0 xx.x
Max: 84.1 98.3 98.3
Avg: yy.y yy.y yy.y
/****************程序如下*****************/
//lab11b.c
//程序假定
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#define MAX_S 100
int scan_scores(int *arr_ID, double **arr_score, FILE *input_file);
int avg_stud_score(double **arr_score, double *avg);
int test_max_avg(double **arr_score, double *test_max, double *test_avg);
int display_scores();
//将字符串s所代表的double型数转化为double型。static修饰,则该函数只限于本文件中的别的函数调用,对别的文件而言是不可见的
static double str_to_double(char *s);
int student_number = 0;
int main()
{
int id[MAX_S];
int ret;
double score[MAX_S][3];
double avg[MAX_S];
double test_max[3], test_avg[3];
FILE *f = fopen("./hw11b.in", "r");
student_number = scan_scores(id, score, f);
if (student_number < 0)
{
perror("scan_scores(id, score, f)");
exit(errno);
}
ret = avg_stud_score(score, avg);
if (ret < 0)
{
perror("avg_stud_score(score, avg)");
exit(errno);
}
ret = test_max_avg(score, test_max, test_avg);
if (ret < 0)
{
perror("test_max_avg(score, test_max, test_avg)");
exit(errno);
}
ret = display_scores(id, score, avg, test_max, test_avg);
if (ret < 0)
{
perror("display_scores(id, score, avg, test_max, test_avg)");
exit(errno);
}
return 0;
}
int scan_scores(int *arr_ID, double **arr_score, FILE *input_file)
{
char data[64];
char item[16];
char *ptr;
int counter = 0;
int flag = 0;
int i = 0;
int j = 0;
int k;
//每次读取一行,每次都将ID号和分数存入对应的数组中,循环MAX_S次
do
{
bzero(data, sizeof(data));
ptr = fgets(data, sizeof(data), input_file);
if (ptr != NULL)
{
counter++;
flag = 1;
bzero(item, sizeof(item));
//将ID号读入item中
while (data[i] != ' ')
{
item[j] = data[i];
i++;
j++;
}
i++;
item[j] = '\0';
arr_ID[counter-1] = atoi(data);
j = 0;
//将分数读入item中,循环3次,直到读完3门考试分数
for (k = 0; k <= 2; k++)
{
while (data[i] != ' ' || data[i] != '\0' || data[i] != '\n' || data[i] != '\r')
{
item[j] = data[i];
i++;
j++;
}
i++;
item[j] = '\0';
arr_score[counter-1][k] = str_to_double(item);
j = 0;
}
}
}
while (ptr != NULL && counter < MAX_S);
if (flag == 1)
return counter;
else
return -1;
}
int avg_stud_score(double **arr_score, double *avg)
{
int i;
if (arr_score == NULL || avg == NULL)
return -;
for (i = 0; i < student_number; i++)
{
double sum = arr_score[i][0] + arr_score[i][1] + arr_score[i][2];
avg[i] = sum/3;
}
return 0;
}
int test_max_avg(double **arr_score, double *test_max, double *test_avg)
{
double sum[3];
int i;
if (arr_score == NULL || test_max == NULL || test_avg == NULL)
return -1;
test_max[0] = sum[0] = arr_score[0][0];
test_max[1] = sum[1] = arr_score[0][1];
test_max[2] = sum[2] = arr_score[0][2];
for (i = 0; i < student_number; i++)
{
if (i > 0)
{
sum[0] = arr_score[i][0];
sum[1] = arr_score[i][1];
sum[2] = arr_score[i][2];
}
if (arr_score[i][0] > test_max[0])
test_max[0] = arr_score[i][0];
if (arr_score[i][1] > test_max[1])
test_max[1] = arr_score[i][1];
if (arr_score[i][2] > test_max[2])
test_max[2] = arr_score[i][2];
}
test_avg[0] = sum[0]/student_number;
test_avg[1] = sum[1]/student_number;
test_avg[2] = sum[2]/student_number;
return 0;
}
int display_scores(int *arr_ID, double **arr_score, double *avg, double *test_max, double *test_avg)
{
int i;
if (arr_ID == NULL || arr_score == NULL || avg == NULL || test_max == NULL || test_avg == NULL)
return -1;
printf("Stud_ID Test_1 Test_2 Test_3 Avg\n");
printf("-------- -------- -------- -------- --------\n");
for (i = 0; i < student_number; i++)
printf("%10d%10.1lf%10.1lf%10.1lf%10.1lf\n", arr_ID[i], arr_score[i][0], arr_score[i][1], arr_score[i][2], avg[i]);
printf("Max %10.1lf%10.1lf%10.1lf\n", test_max[0], test_max[1], test_max[2]);
printf("Avg %10.1lf%10.1lf%10.1lf\n", test_avg[0], test_avg[1], test_avg[2]);
return 0;
}
static double str_to_double(char *s)
{
if (s = NULL)
return -1;
double a = 0;
int i;
int after_dot_counter = 0;
int flag = 0;
for (i = 0; strlen(s) >= i + 1; i++)
{
if (s[i] != '.')
{
if (flag == 1)
after_dot_counter++;
a = a*10 + s[i];
}
else
flag = 1;
}
for (i = 1; i <= after_dot_counter; i++)
a = a/10;
return a;
}
哎,搞你的问题我昨天搞到3:10,今天上班困的要命。多给加点分吧!!!
A、以lab11a.c作为你的程序名
C、创建内容如下,名为lab11a.in的文件(你这里写错了,序号a过了应该是序号b,不知道你怎么搞的,直接到c了,管它呢,照着你抄)
-10 7 -1 4 9 -17 23 28 -37 38 43 45 46
D、读取输入文件,直到遇到EOF,统计数组中你读入的元素的个数。声明数组的最大容量为MAX_D,值为12。如果有多余MAX_D个元素被读到,则忽略第MAX_D个后的元素。以上面的文件为例,最后一个整数将不会被读到。使用while循环读取下一个数据。
E、写一个为名select_sort()的函数来实现降序排列,参数为这个整形数组和数组的长度
F、写一个名为get_imax()的函数,并且调用之,用来返回从索引值i_init到索引值i_last之间,最大值元素的索引值,作为你解决方案的一部分。
G、写一个名为swap2()的函数,该函数交换两个整数的值,并调用之,作为选择排序算法的一部分。
H、以降序方式,将这些整数写到lab11a.out文件,这个文件的格式如lab11a.in。
/****************程序如下*****************/
//lab11a.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define MAX_D 12
Int select_sort(int *arr, int lenth);
Int get_imax(int *arr, int i_init, int i_last, int lenth);
void swap2(int *a, int *b);
int main()
{
Int arr[MAX_D];
int ret;
int i,j;
char number[5];
FILE *in = fopen(“./ lab11a.in”, “a+”);
FILE *out = fopen(“./ lab11a.out”, “a+”);
fwrite(“-10 7 -1 4 9 -17 23 28 -37 38 43 45 46”, sizeof(“-10 7 -1 4 9 -17 23 28 -37 38 43 45 46”), 1, in);
ret = fclose(in);
if (ret != 0)
{
perror(“fclose(in)”);
exit(errno);
}
//只读方式打开输入文件
in = fopen(“./ lab11a.in”, “r”);
//以空格符或EOF(文件结束符)为界,读取输入文件内容,读取MAX_D次
i = 0;
while (i < MAX_D)
{
bzero(number, sizeof(number));
j = 0;
do
{
number[j++] = fgetc(in);
}
while (number[j-1] != ‘ ’ || number[j-1] != EOF)
number[j-1] = ‘\0’;
//atoi函数,将字符串转化为整数
arr[i] = atoi(number);
i++;
}
//降序排列,并出错处理。
ret = select_sort(arr, MAX_D);
if (ret != 0)
{
perror(“select_sort(arr, MAX_D)”);
exit(errno);
}
//将arr数组的内容输出到输出文件。
for (i = 0; i < MAX_D; i++)
fprintf(out, “%d ”, arr[i]);
fclose(in);
fclose(out);
return 0;
}
int select_sort(int *arr, int lenth)
{
int i,j;
int max_index;
//对输入参数有效性进行检测,无效返回-1,代表出错
if (arr == NULL || lenth <= 0)
return -1;
//冒泡算法实现降序排列
for (i = 0; i < lenth-1; i++)
for(j = i; j <= lenth-1; j++)
{
max_index = get_imax(arr, j, j+1, max_d);
//出错处理
if (max_index < 0)
return -1;
if (j < max_index)
swap2(&arr[j], &arr[j+1]);
}
//返回0,表示成功(正规it产业都以逻辑0为成功)
return 0;
}
int get_imax(int *arr, int i_init, int i_last, int lenth)
{
int i;
int max_index = i_init;
//对输入参数有效性进行检测,无效返回-1,代表出错
if (arr = NULL || i_init >= i_last || i_last >= lenth || i_init < 0 || i_last <= i_init || lenth <= 0)
return -1;
for (i = i_init; i < i_last; i++)
{
if (arr[i] <= arr[i+1])
max_index = i+1;
}
return max_index;
}
void swap2(int *a, int *b)
{
int c = *a;
*a = *b;
*b = c;
return ;
}
第二题:
a、将你的程序命名为lab11b.c
c、对这个问题,我们有一个输入文件,文件中的是一个班学生考试分数的二维数组。学生通过ID号(一个整数)来区分,紧接着是三个考试分数(double型)。一个正常的输入文件hw11b.in如下所示:
10707 57.9 64.6 98.3
10004 69.4 83.7 92.1
10995 75.2 98.3 69.5
11005 83.2 79.6 89.1
10664 84.1 91.0 79.0
d、写一个名为scan_scores()的函数,将ID号存入一个1维的整形数组中,将考试分数存入一个2维的double型数组中,该函数返回值为学生数量,如果返回值为-1,代表读取输入文件出错。当你读取数值时,要确定别越过数组的最大界限(以学生数量为界限),MAX_S值为100,作为学生数量。
e、写一个名为avg_stud_score()的函数,来计算出每个学生3门考试成绩的平均分。将平均分存入另外一个double型数组中。
f、写一个名为test_max_avg()的函数,来计算出每一门考试的最高分和平均分,将结果存入名为test_max[] 和 test_avg[]的double型数组中。
g、写一个名为display_scores()的函数,来显示输入文件的内容,以表格形式展示。对于输入文件,输出到屏幕的形式应如下:
Stud ID Test 1 Test 2 Test 3 Avg
-------- -------- -------- -------- --------
10707 57.9 64.6 98.3 xx.x
10004 69.4 83.7 92.1 xx.x
10995 75.2 98.3 69.5 xx.x
11005 83.2 79.6 89.1 xx.x
10664 84.1 91.0 79.0 xx.x
Max: 84.1 98.3 98.3
Avg: yy.y yy.y yy.y
/****************程序如下*****************/
//lab11b.c
//程序假定
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#define MAX_S 100
int scan_scores(int *arr_ID, double **arr_score, FILE *input_file);
int avg_stud_score(double **arr_score, double *avg);
int test_max_avg(double **arr_score, double *test_max, double *test_avg);
int display_scores();
//将字符串s所代表的double型数转化为double型。static修饰,则该函数只限于本文件中的别的函数调用,对别的文件而言是不可见的
static double str_to_double(char *s);
int student_number = 0;
int main()
{
int id[MAX_S];
int ret;
double score[MAX_S][3];
double avg[MAX_S];
double test_max[3], test_avg[3];
FILE *f = fopen("./hw11b.in", "r");
student_number = scan_scores(id, score, f);
if (student_number < 0)
{
perror("scan_scores(id, score, f)");
exit(errno);
}
ret = avg_stud_score(score, avg);
if (ret < 0)
{
perror("avg_stud_score(score, avg)");
exit(errno);
}
ret = test_max_avg(score, test_max, test_avg);
if (ret < 0)
{
perror("test_max_avg(score, test_max, test_avg)");
exit(errno);
}
ret = display_scores(id, score, avg, test_max, test_avg);
if (ret < 0)
{
perror("display_scores(id, score, avg, test_max, test_avg)");
exit(errno);
}
return 0;
}
int scan_scores(int *arr_ID, double **arr_score, FILE *input_file)
{
char data[64];
char item[16];
char *ptr;
int counter = 0;
int flag = 0;
int i = 0;
int j = 0;
int k;
//每次读取一行,每次都将ID号和分数存入对应的数组中,循环MAX_S次
do
{
bzero(data, sizeof(data));
ptr = fgets(data, sizeof(data), input_file);
if (ptr != NULL)
{
counter++;
flag = 1;
bzero(item, sizeof(item));
//将ID号读入item中
while (data[i] != ' ')
{
item[j] = data[i];
i++;
j++;
}
i++;
item[j] = '\0';
arr_ID[counter-1] = atoi(data);
j = 0;
//将分数读入item中,循环3次,直到读完3门考试分数
for (k = 0; k <= 2; k++)
{
while (data[i] != ' ' || data[i] != '\0' || data[i] != '\n' || data[i] != '\r')
{
item[j] = data[i];
i++;
j++;
}
i++;
item[j] = '\0';
arr_score[counter-1][k] = str_to_double(item);
j = 0;
}
}
}
while (ptr != NULL && counter < MAX_S);
if (flag == 1)
return counter;
else
return -1;
}
int avg_stud_score(double **arr_score, double *avg)
{
int i;
if (arr_score == NULL || avg == NULL)
return -;
for (i = 0; i < student_number; i++)
{
double sum = arr_score[i][0] + arr_score[i][1] + arr_score[i][2];
avg[i] = sum/3;
}
return 0;
}
int test_max_avg(double **arr_score, double *test_max, double *test_avg)
{
double sum[3];
int i;
if (arr_score == NULL || test_max == NULL || test_avg == NULL)
return -1;
test_max[0] = sum[0] = arr_score[0][0];
test_max[1] = sum[1] = arr_score[0][1];
test_max[2] = sum[2] = arr_score[0][2];
for (i = 0; i < student_number; i++)
{
if (i > 0)
{
sum[0] = arr_score[i][0];
sum[1] = arr_score[i][1];
sum[2] = arr_score[i][2];
}
if (arr_score[i][0] > test_max[0])
test_max[0] = arr_score[i][0];
if (arr_score[i][1] > test_max[1])
test_max[1] = arr_score[i][1];
if (arr_score[i][2] > test_max[2])
test_max[2] = arr_score[i][2];
}
test_avg[0] = sum[0]/student_number;
test_avg[1] = sum[1]/student_number;
test_avg[2] = sum[2]/student_number;
return 0;
}
int display_scores(int *arr_ID, double **arr_score, double *avg, double *test_max, double *test_avg)
{
int i;
if (arr_ID == NULL || arr_score == NULL || avg == NULL || test_max == NULL || test_avg == NULL)
return -1;
printf("Stud_ID Test_1 Test_2 Test_3 Avg\n");
printf("-------- -------- -------- -------- --------\n");
for (i = 0; i < student_number; i++)
printf("%10d%10.1lf%10.1lf%10.1lf%10.1lf\n", arr_ID[i], arr_score[i][0], arr_score[i][1], arr_score[i][2], avg[i]);
printf("Max %10.1lf%10.1lf%10.1lf\n", test_max[0], test_max[1], test_max[2]);
printf("Avg %10.1lf%10.1lf%10.1lf\n", test_avg[0], test_avg[1], test_avg[2]);
return 0;
}
static double str_to_double(char *s)
{
if (s = NULL)
return -1;
double a = 0;
int i;
int after_dot_counter = 0;
int flag = 0;
for (i = 0; strlen(s) >= i + 1; i++)
{
if (s[i] != '.')
{
if (flag == 1)
after_dot_counter++;
a = a*10 + s[i];
}
else
flag = 1;
}
for (i = 1; i <= after_dot_counter; i++)
a = a/10;
return a;
}
哎,搞你的问题我昨天搞到3:10,今天上班困的要命。多给加点分吧!!!
展开全部
(再占个地儿)
先翻译一题,不是英语专业,不知道合格不,所以先偷点懒,合格再继续。
a.将你的程序文件命名为lab11a.c。
c.创建一个文件lab11a.in,其中含有数据-10 7 -1 4 9 -17 23 28 -37 38 43 45 46。
d.读取文件内容直到遇到EOF,将读入的数据放入数组。声明数组有一个最大容量设置为12.如果读入数据长度超过这个最大值,最大值之后的数据舍弃。对于新创建的文件而已,最后一个整数是不被读入的。使用while循环来读入下个整型数据。
e.编写一个函数select_sort(),将输入的整数值降序排列。形参是整数数组和数组长度。
f.在程序中编写并调用函数get_imax(),该函数返回数组中最大值的下标,数组下标由 i_init和i_last确定。
g.在选择排序算法中编写并调用函数swap2()来交换两个整数的值。整数值通过实参传递。
h.将降序排列的数组输出到文件lab11a.out中。(下一句不会)
先翻译一题,不是英语专业,不知道合格不,所以先偷点懒,合格再继续。
a.将你的程序文件命名为lab11a.c。
c.创建一个文件lab11a.in,其中含有数据-10 7 -1 4 9 -17 23 28 -37 38 43 45 46。
d.读取文件内容直到遇到EOF,将读入的数据放入数组。声明数组有一个最大容量设置为12.如果读入数据长度超过这个最大值,最大值之后的数据舍弃。对于新创建的文件而已,最后一个整数是不被读入的。使用while循环来读入下个整型数据。
e.编写一个函数select_sort(),将输入的整数值降序排列。形参是整数数组和数组长度。
f.在程序中编写并调用函数get_imax(),该函数返回数组中最大值的下标,数组下标由 i_init和i_last确定。
g.在选择排序算法中编写并调用函数swap2()来交换两个整数的值。整数值通过实参传递。
h.将降序排列的数组输出到文件lab11a.out中。(下一句不会)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
求翻译
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询