C language programming> 20th week ⑦ Please add the fun function, the function of this function is to put the odd-numbered elements in the array a behind the original array in the original order.

Example: Please add the fun function. The function of this function is to put the odd-numbered elements in the array a behind the original array in the original order.

例如,输入 “5,7,2,42,35,32,28,37,68,13”,则输出 “2,42,32,28,68,5,7,35,37,13”。
请勿改动主函数main与其它函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。

代码如下:

#include<stdio.h>
#define N 10
void fun(int a[])
{
    
    
	int i,j=0,k=0;
	int b[N];
	for(i=0;i<N;i++)
		if(a[i]%2!=0)
			b[k++]=a[i];
		else
			a[j++]=a[i];
	for(i=0;i<k;i++,j++)
		a[j]=b[i];
}
main()
{
    
    
	int i;
	int a[N]={
    
    5,7,2,42,35,32,28,37,68,13};
	printf("The original list is\n");
	for(i=0;i<N;i++)
		printf("%4d",a[i]);
	fun(a);
	printf("\nThe result list is\n");
	for(i=0;i<N;i++)
		printf("%4d",a[i]);
	printf("\n");
}

The output running window is as follows:
Insert picture description here

越努力越幸运!
加油,奥力给!!!

Guess you like

Origin blog.csdn.net/qq_45385706/article/details/112799811