2道C语言编程题,题目是英语的,答对追加一定分数
2道题,第一道题是:a.Nameyourprogramfilelab11a.c.c.Createthefilebelow,lab11a.in.-107-149-17232...
2道题,第一道题是:
a. Name your program file lab11a.c.
c. Create the file below, lab11a.in.
-10 7 -1 4 9 -17 23 28 -37 38 43 45 46
d. 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 a while loop to read in the next integer:
e. 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.
f. 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.
g. 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.
h. Write the integers in descending order to the output file lab11a.out. The file
format is identical to lab11a.in.
第二道题:
a. Name your program file lab11b.c.
c. 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
d. 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.
e. 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.
f. 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[].
g. 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.
c. Create the file below, lab11a.in.
-10 7 -1 4 9 -17 23 28 -37 38 43 45 46
d. 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 a while loop to read in the next integer:
e. 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.
f. 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.
g. 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.
h. Write the integers in descending order to the output file lab11a.out. The file
format is identical to lab11a.in.
第二道题:
a. Name your program file lab11b.c.
c. 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
d. 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.
e. 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.
f. 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[].
g. 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、以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>
#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次
for (i = 0; I < MAX_D; i++)
{
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);
}
//降序排列,并出错处理。
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、以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>
#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次
for (i = 0; I < MAX_D; i++)
{
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);
}
//降序排列,并出错处理。
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.将你的程序文件命名为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中。(下一句不会)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
电话里可能我恨你地方环境;lhnodfnholfjhmnpodfjhnopsihgoihgofskhgoishgiuzslnoxkhngisd
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这~~~~~分是不是少了点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询