2026年1月22日
收获:
1.没有进while()时,注意是否有多余的分号。
2.字符串数组的长度,可以用数组指针所占字节的长度/数组第一项的长度计算出来。
3.字符串赋值要用strcpy()函数,可以用sprintf(cards[n].face,“%d”,j+1)将数字转为字符串存储在第一个变量中。
题目:
1、Q2147.(语言: C)编程计算1-1/3+1/5-1/7+…,直到最后一项小于10e-4。
(已知正确的运行结果为:sum=0.785349)
下面程序中存在比较隐蔽的错误,
请通过分析和调试程序,发现并改正程序中的错误。
要求改正后的程序仍然用while结构实现。
注意:将修改后的完整的源程序写在答题区内。
对于没有错误的语句,请不要修改,
修改原本正确的语句也要扣分。
当且仅当错误全部改正,且程序运行结果调试正确,
才得分,如果只改正了部分错误,则不加分。
#include <stdio.h>
main()
{
float sum = 0, term = 1, ; //多了个逗号
int n = 1, sign = 1;
while (fabs(term) >= 1e-4); //多了个分号,且fabs()要引头文件
{
sum = sum + term;
sign = -sign;
n = n + 2;
term = sign / n; //整数相除,结果是整数
}
printf("sum=%f\n",sum);
}
改后运行成功代码:
#include <stdio.h>
#include <math.h>
main()
{
float sum = 0, term = 1 ;
int n = 1, sign = 1;
while (fabs(term) >= 1e-4)
{
sum = sum + term;
sign = -sign;
n = n + 2;
term = sign*1.0 / n;
}
printf("sum=%f\n",sum);
}
2、Q542.(语言: C)从键盘输入学生的成绩(参加人数最多不超过40人),当输入为负数时,表示输入结束,试编程将分数按从高到低顺序进行排序输出。
通过调用sort函数进行分数排序。
**输入格式要求:“%d”
提示信息:“Total students are %d\n” “Sorted scores:” “Input score:”
**输出格式要求:“%4d”
程序的运行示例如下:
Input score:84
Input score:83
Input score:88
Input score:87
Input score:51
Input score:-1
Total students are 5
Sorted scores: 88 87 84 83 51
#include <stdio.h>
void sort(int score[],int n);
int main()
{
int score[40],flag=1,n=0;
while(flag){
printf("Input score:");
scanf("%d",&score[n])


859

被折叠的 条评论
为什么被折叠?



