1. 数组分区(指针作为函数参数)
本题由三个部分构成
(1) 编写一个函数CountEvensAndOdds,其功能是统计一个整型数组中奇数和偶数的个数。该函数有4个参数,整数型数组、数组的长度和两个指针(用于返回奇数、偶数的个数)
(2) 利用(1)中的CountEvensAndOdds函数,编写一个Partition函数,其功能是找出给定长度的一个整型数组中的所有奇数和偶数,并把它们分别保存在两个新的整型数组中(用C语言的库函数malloc函数动态创建),然后返回这两个新数组及其长度。该函数的参数有6个,整型数组、数组长度、奇数数组及其长度、偶数数组及其长度。
(3) 编写主函数,要求能够定义一个数组,并能够用随机数对该数组进行初始化,然后调用(2)中的函数Partition,将原始数组、奇数数组、偶数数组输出。
第1个回答 2021-05-29
C语言_071_指针的定义和使用_上
第2个回答 2013-11-30
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
void CountEvensAndOdds(int *parr,int arr_len,
int *even_count,int *odds_count) {
int i;
int count_even = 0;
int count_odds = 0;
for ( i=0; i<arr_len; i++ ) {
if ( (*(parr+i)%2) == 0 )
count_even++;
else
count_odds++;
}
*even_count = count_even;
*odds_count = count_odds;
}
void Partition(int *parr,int arr_len,
int **even_arr,int *even_len,
int **odds_arr,int *odds_len) {
int even_count;
int odds_count;
int i;
int m = 0,n = 0;
int count_even = 0;
int count_odds = 0;
CountEvensAndOdds(parr,arr_len,&even_count,&odds_count);
*even_arr = (int *)malloc(sizeof(int)*even_count);
*odds_arr = (int *)malloc(sizeof(int)*odds_count);
for ( i=0; i<arr_len; i++ ) {
if ( (*(parr+i)%2) == 0 ) {
*(*even_arr+m) = *(parr+i);
m++;
}
else {
*(*odds_arr+n) = *(parr+i);
n++;
}
}
*even_len = m;
*odds_len = n;
}
void print_arr(int *parr,int arr_len) {
int i;
for ( i=0; i<arr_len; i++ )
printf("%d ",*(parr+i));
printf("\n");
}
int main () {
int *parr;
int arr_len;
int i;
int *even_arr;
int even_len;
int *odds_arr;
int odds_len;
srand(getpid());
arr_len = rand()%100;
parr = (int *)malloc(sizeof(int) *arr_len);
printf("arr_len = %d \n",arr_len);
for ( i=0; i<arr_len; i++ ) {
parr[i] = rand()%100;
}
Partition(parr,arr_len,&even_arr,&even_len,&odds_arr,&odds_len);
printf("arr:\n");
print_arr(parr,arr_len);
printf("even_arr len = %d\n",even_len);
printf("even_arr:\n");
print_arr(even_arr,even_len);
printf("odds_arr len = %d\n",odds_len);
printf("odds_arr:\n");
print_arr(odds_arr,odds_len);
return 0;
}本回答被提问者采纳
#include <stdlib.h>
#include <process.h>
void CountEvensAndOdds(int *parr,int arr_len,
int *even_count,int *odds_count) {
int i;
int count_even = 0;
int count_odds = 0;
for ( i=0; i<arr_len; i++ ) {
if ( (*(parr+i)%2) == 0 )
count_even++;
else
count_odds++;
}
*even_count = count_even;
*odds_count = count_odds;
}
void Partition(int *parr,int arr_len,
int **even_arr,int *even_len,
int **odds_arr,int *odds_len) {
int even_count;
int odds_count;
int i;
int m = 0,n = 0;
int count_even = 0;
int count_odds = 0;
CountEvensAndOdds(parr,arr_len,&even_count,&odds_count);
*even_arr = (int *)malloc(sizeof(int)*even_count);
*odds_arr = (int *)malloc(sizeof(int)*odds_count);
for ( i=0; i<arr_len; i++ ) {
if ( (*(parr+i)%2) == 0 ) {
*(*even_arr+m) = *(parr+i);
m++;
}
else {
*(*odds_arr+n) = *(parr+i);
n++;
}
}
*even_len = m;
*odds_len = n;
}
void print_arr(int *parr,int arr_len) {
int i;
for ( i=0; i<arr_len; i++ )
printf("%d ",*(parr+i));
printf("\n");
}
int main () {
int *parr;
int arr_len;
int i;
int *even_arr;
int even_len;
int *odds_arr;
int odds_len;
srand(getpid());
arr_len = rand()%100;
parr = (int *)malloc(sizeof(int) *arr_len);
printf("arr_len = %d \n",arr_len);
for ( i=0; i<arr_len; i++ ) {
parr[i] = rand()%100;
}
Partition(parr,arr_len,&even_arr,&even_len,&odds_arr,&odds_len);
printf("arr:\n");
print_arr(parr,arr_len);
printf("even_arr len = %d\n",even_len);
printf("even_arr:\n");
print_arr(even_arr,even_len);
printf("odds_arr len = %d\n",odds_len);
printf("odds_arr:\n");
print_arr(odds_arr,odds_len);
return 0;
}本回答被提问者采纳