#include <stdio.h> int main(void) { int a[10],i,num; for(i=0;i<10;i++) scanf("%d",&a[i]); scanf("%d",&num); /*输入要删除的元素序号*/ for(i=num;i<10;i++) /***********found***********/ a[i]= a[i+1]; a[9]=0; /***********found***********/ for(i=0;i<10;i++) printf("%d ", a[i]); return 0; }
时间: 2023-07-23 22:27:00 浏览: 33
这段程序存在一个问题,即在删除元素后,最后的一个元素不会被清空,会保留原来的值。应该将`a[9]=0`修改为`a[8]=0`。
下面是修改后的程序代码:
```
#include <stdio.h>
int main(void) {
int a[10], i, num;
for (i = 0; i < 10; i++)
scanf("%d", &a[i]);
scanf("%d", &num); /*输入要删除的元素序号*/
for (i = num; i < 9; i++)
a[i] = a[i + 1];
a[8] = 0;
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
return 0;
}
```
这样修改后,程序就能正确地输出结果了。
相关问题
C语言程序填空:从键盘输入10个字符,将其全部输出到一个磁盘文件“data.dat"中保存起来#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp; int num; int i=0; if((fp=fopen("data.dat","w"))==NULL) { printf("打不开文件\n"); exit(0); } while(i<=9) { fprintf(fp,"%d",num); i++; } }
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() {
FILE *fp;
char ch[10];
int i=0;
if((fp=fopen("data.dat","w"))==NULL) {
printf("打不开文件\n");
exit(0);
}
printf("请输入10个字符:\n");
while(i<=9) {
ch[i] = getch();
fprintf(fp,"%c",ch[i]);
i++;
}
fclose(fp);
printf("已将输入的字符保存到文件data.dat中!");
}
注意:程序中使用了 getch() 函数,需要包含 conio.h 头文件。另外,文件操作完成后需要关闭文件,使用 fclose() 函数。
#include <stdio.h> #include <stdlib.h> void Swap(int x, int y){ int tmp; tmp = x; x = y; y = tmp; } int main(){ int num1 = 10; int num2 = 20; Swap(num1,num2); printf("num1 = %d,num2 = %d\n",num1,num2); } 为什么这个程序输出结果不交换,原因及多种正确代码表示
这个程序输出结果不交换的原因是因为在 Swap 函数中,传入的参数是按值传递(pass by value),也就是说,在函数内部修改的是传入参数的副本,而不是传入参数本身。因此,函数内部的交换操作并不会影响到调用函数时传入的参数。要解决这个问题,可以使用指针或者引用作为函数参数,这样就可以修改传入参数的值。以下是两种正确的代码表示:
使用指针:
```c
#include <stdio.h>
#include <stdlib.h>
void Swap(int *x, int *y){
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
int main(){
int num1 = 10;
int num2 = 20;
Swap(&num1,&num2);
printf("num1 = %d,num2 = %d\n",num1,num2);
}
```
使用引用:
```c
#include <stdio.h>
#include <stdlib.h>
void Swap(int &x, int &y){
int tmp;
tmp = x;
x = y;
y = tmp;
}
int main(){
int num1 = 10;
int num2 = 20;
Swap(num1,num2);
printf("num1 = %d,num2 = %d\n",num1,num2);
}
```
在以上两种情况下,传入的参数都可以被正确的交换。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)