这个程序是要把第一个数组中大于等于x的数复制到第二个数组中,但是我不论输入x是多少,这个程序都会把所有的都复制到数组二中,实在看不出啥问题,请您帮忙看看。
#include
#include
int select1(double a[], double b[], int n, double x);
int main()
{
double shuzu1[]={3.,8.,4.,68.,24.,55.,33.5,86.};
int b=8;
double shuzu2[b];
double x;
printf("input a number:");
scanf("%d",&x);
b=select1(shuzu2,shuzu1,b,x);
printf("\nthe new shuzu is:");
int i;
for(i=0;iprintf("%f\n",shuzu2[i]);
return 0;
}
int select1(double a[], double b[], int n, double x)
{
int c,d;
for(c=0,d=0;cif(b[c]>=x)
{
a[d]=b[c];
d++;
}
}
return c;
}
写代码要学会调试,你一步一步的把用到的变量都打印出来看也行,断点跟也行,看哪一步开始不符合你的预期,再做调整
不要靠猜
你的x是个double,但是你用了%d来格式化它,那不乱码了,里面应该是个0
for(i=0;iprintf("%f\n",shuzu2[i]);//你这是啥啊
#include<stdio.h>
int select1(double a[], double b[], int n, double x);
int main()
{
double shuzu1[]={3.,8.,4.,68.,24.,55.,33.5,86.};
int b=8;
double shuzu2[b];
double x;
printf("input a number:");
scanf("%d",&x);
b=select1(shuzu2,shuzu1,b,x);
printf("\nthe new shuzu is:");
int i;
// for(i=0;printf("%f\n",shuzu2[i])
return 0;
}
int select1(double a[], double b[], int n, double x)
{
int c,d;
// for(c=0,d=0;cif(b[c]>=x)
// {
// a[d]=b[c];
// d++;
// }
// }
return c;
}
先把你自己的代码写对再说吧,注释出来的都是错误
你的代码都是错误,语法什么的都不对
修改如下,供参考:
#include <stdio.h>
int select1(double a[], double b[], int n, double x);
int main()
{
double shuzu1[] = { 3.,8.,4.,68.,24.,55.,33.5,86. };
const int b = 8; //修改
double shuzu2[b];
double x;
printf("input a number:");
scanf("%lf", &x); //scanf("%d", &x); 修改
int B = select1(shuzu2, shuzu1, b, x); //修改
printf("\nthe new shuzu is:");
int i;
for (i = 0; i < B; i++)
printf("%f ", shuzu2[i]);
return 0;
}
int select1(double a[], double b[], int n, double x)
{
int c, d;
for (c = 0, d = 0; c < n; c++)
if (b[c] >= x)
{
a[d] = b[c];
d++;
}
return d; //return c; 修改
}