//程序阅读,输出结果 #include <stdio.h> int sum(int a[], int len); int main() { int a[10] = { 1,2,3,4,5,6,7,8,9,10 }; printf("%d", sum(a,8)); return 0; } int sum(int a[], int len) { int s = 0; for (int i = 0; i<len; i++) if (i%2) s += a[i]; return s; }
时间: 2024-04-05 09:33:09 浏览: 26
这段程序的输出结果是 20。程序的执行过程是这样的:
1. main 函数定义一个长度为 10 的整型数组 a,并初始化为 {1,2,3,4,5,6,7,8,9,10}。
2. main 函数调用 sum(a,8)。
3. sum 函数遍历 a 数组,对于数组下标为奇数的元素,将其累加到 s 变量中。
4. 遍历结束后,sum 函数返回 s 的值,即累加结果。
5. main 函数输出 sum 函数的返回值,即输出 20。
6. 程序执行完毕,返回 0。
相关问题
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<windows.h> struct stu { int id; char name[10]; float grade[11]; float sum; float average; float gpa; struct stu* next; }stu* head;stu *tail;按照这个结构体来续写C语言代码。要求将学生信息保存在硬盘文件中,通过制定文件路径和文件名称来导入学生信息;按照学号对所有学生信息进行升序、降序排列,并输出到屏幕显示或输出到文件
好的,我会为你续写代码。以下是完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
struct stu {
int id;
char name[10];
float grade[11];
float sum;
float average;
float gpa;
struct stu* next;
};
typedef struct stu stu;
stu* head = NULL;
stu* tail = NULL;
// 读取学生信息
void read_info(char* file_name) {
FILE* fp;
fp = fopen(file_name, "r");
if(fp == NULL) {
printf("Failed to open file %s\n", file_name);
return;
}
while(!feof(fp)) {
stu* s = (stu*)malloc(sizeof(stu));
fscanf(fp, "%d%s%f%f%f", &s->id, s->name, &s->grade[0], &s->grade[1], &s->grade[2]);
s->sum = s->grade[0] + s->grade[1] + s->grade[2];
s->average = s->sum / 3.0;
s->gpa = s->average / 20.0 - 1.5;
if(head == NULL) {
head = s;
tail = s;
tail->next = NULL;
} else {
tail->next = s;
tail = s;
tail->next = NULL;
}
}
fclose(fp);
}
// 写入学生信息
void write_info(char* file_name) {
FILE* fp;
fp = fopen(file_name, "w");
if(fp == NULL) {
printf("Failed to open file %s\n", file_name);
return;
}
stu* p = head;
while(p != NULL) {
fprintf(fp, "%d %s %.2f %.2f %.2f\n", p->id, p->name, p->grade[0], p->grade[1], p->grade[2]);
p = p->next;
}
fclose(fp);
}
// 按照学号升序排序
void sort_by_id_ascending() {
int len = 0;
stu* p = head;
while(p != NULL) {
len++;
p = p->next;
}
p = head;
for(int i = 0; i < len - 1; i++) {
for(int j = 0; j < len - i - 1; j++) {
if(p->id > p->next->id) {
stu* tmp = p->next;
p->next = tmp->next;
tmp->next = p;
if(p == head) {
head = tmp;
} else {
stu* q = head;
while(q->next != p) {
q = q->next;
}
q->next = tmp;
}
p = tmp;
}
p = p->next;
}
p = head;
}
}
// 按照学号降序排序
void sort_by_id_descending() {
int len = 0;
stu* p = head;
while(p != NULL) {
len++;
p = p->next;
}
p = head;
for(int i = 0; i < len - 1; i++) {
for(int j = 0; j < len - i - 1; j++) {
if(p->id < p->next->id) {
stu* tmp = p->next;
p->next = tmp->next;
tmp->next = p;
if(p == head) {
head = tmp;
} else {
stu* q = head;
while(q->next != p) {
q = q->next;
}
q->next = tmp;
}
p = tmp;
}
p = p->next;
}
p = head;
}
}
// 输出学生信息
void print_info() {
stu* p = head;
while(p != NULL) {
printf("%d %s %.2f %.2f %.2f %.2f\n", p->id, p->name, p->grade[0], p->grade[1], p->grade[2], p->gpa);
p = p->next;
}
}
int main() {
char file_name[100];
printf("Please enter the file path and name: ");
scanf("%s", file_name);
read_info(file_name);
printf("Before sorting:\n");
print_info();
sort_by_id_ascending();
printf("After sorting by id in ascending order:\n");
print_info();
sort_by_id_descending();
printf("After sorting by id in descending order:\n");
print_info();
printf("Please enter the file path and name you want to save the sorted information: ");
scanf("%s", file_name);
write_info(file_name);
return 0;
}
```
这个程序可以通过文件读取来导入学生信息,根据学号升序或降序排列学生信息,并输出到屏幕或文件。请注意,在运行程序时,需要输入导入文件的路径和名称,以及希望将排序信息保存到的文件路径和名称。
#include <stdio.h> #include <string.h> int main() { char arr[1000], brr[1000]; int i = 0, count = 0, sum = 0; scanf("%s", arr); while (i < sizeof(arr)) { if (arr[i] == arr[i + 1]) count += 1; else { if (count == 1) { brr[sum] = arr[i]; sum++; } else { brr[sum] = count; brr[sum + 1] = arr[i]; sum += 2; count = 1; } } } printf("%s", brr); return 0; }检查有无问题
这段代码有几个问题,首先是在输入字符串时没有限制输入长度,可能会导致数组越界;其次是在 while 循环中没有对 i 进行更新,会导致死循环;还有在输出字符串时,如果 brr 数组中有数字,直接输出会被当成 ASCII 码输出,需要对其进行处理。以下是修正后的代码:
```
#include <stdio.h>
#include <string.h>
int main() {
char arr[1000], brr[1000];
int i = 0, count = 1, sum = 0;
scanf("%s", arr);
int len = strlen(arr);
while (i < len) {
if (arr[i] == arr[i + 1]) {
count += 1;
} else {
if (count == 1) {
brr[sum] = arr[i];
sum++;
} else {
brr[sum] = '0' + count;
brr[sum + 1] = arr[i];
sum += 2;
count = 1;
}
}
i++;
}
brr[sum] = '\0';
printf("%s", brr);
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)