c语言:这个怎么做?? 帮帮忙哦! 先谢了!
2.打开程序Cprog012.C,完成其中的函数fun2(inta[],intn,intb[],intc[]),实现:(1)将数组a中大于-20的元素,依次存放到数组b中...
2. 打开程序Cprog012.C,完成其中的函数fun2(int a[ ], int n, int b[ ], int c[ ]),实现:
(1)将数组a中大于-20的元素,依次存放到数组b中;
(2)将数组b中的元素按照从小到大的顺序存放到数组c中;
(3)函数返回数组b中的元素个数。
#include <string.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
int fun2(int a[],int n,int b[],int c[])
{
/**/
/**/
}
void main()
{ int n = 10, i, nb;
int aa[10] = {12, -10, -31, -18, -15, 50, 17, 15, -20, 20};
int bb[10], cc[10];
clrscr();
printf("There are %2d elements in aa.\n", n);
printf("They are: ");
for(i=0; i<n; i++) printf("%6d", aa[i]);
printf("\n");
nb = fun2(aa, n, bb, cc);
printf("Elements in bb are: ");
for (i=0; i<nb; i++) printf("%6d", bb[i]);
printf("\n");
printf("Elements in cc are: ");
for(i=0; i<nb; i++) printf("%6d", cc[i]);
printf("\n");
printf("There are %2d elements in bb.\n", nb); 展开
(1)将数组a中大于-20的元素,依次存放到数组b中;
(2)将数组b中的元素按照从小到大的顺序存放到数组c中;
(3)函数返回数组b中的元素个数。
#include <string.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
int fun2(int a[],int n,int b[],int c[])
{
/**/
/**/
}
void main()
{ int n = 10, i, nb;
int aa[10] = {12, -10, -31, -18, -15, 50, 17, 15, -20, 20};
int bb[10], cc[10];
clrscr();
printf("There are %2d elements in aa.\n", n);
printf("They are: ");
for(i=0; i<n; i++) printf("%6d", aa[i]);
printf("\n");
nb = fun2(aa, n, bb, cc);
printf("Elements in bb are: ");
for (i=0; i<nb; i++) printf("%6d", bb[i]);
printf("\n");
printf("Elements in cc are: ");
for(i=0; i<nb; i++) printf("%6d", cc[i]);
printf("\n");
printf("There are %2d elements in bb.\n", nb); 展开
3个回答
展开全部
VS2005编译通过,希望对你有帮助!
int fun2(int a[],int n,int b[],int c[])
{
int i,j,temp;
int index=0;
for(i = 0; i < n; i++)
{
if(a[i] > -20)
{
c[index]=b[index]=a[i];
index++;
}
}
for(i=0;i<index;i++)
{
for (j=i+1;j<index;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
return index;
}
int fun2(int a[],int n,int b[],int c[])
{
int i,j,temp;
int index=0;
for(i = 0; i < n; i++)
{
if(a[i] > -20)
{
c[index]=b[index]=a[i];
index++;
}
}
for(i=0;i<index;i++)
{
for (j=i+1;j<index;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
return index;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询