gpt4 book ai didi

c - printf 数组中选定的数字

转载 作者:太空宇宙 更新时间:2023-11-04 08:03:52 25 4
gpt4 key购买 nike

在我的程序中,用户输入一个数组,PC 生成一个随机数组(从用户那里选择最大数量),然后程序创建第三个数组,显示第一个数组中不在第二个数组中的数字。然而,我只想每次输入一次数字,但程序会一次又一次地输入它们,直到数组已满。

例如,如果我输入 0 3 1 并且生成的数字是 1 5 9,它将打印 0 3 3

我只想打印 0 3 。完整代码:

    #include <stdio.h>
#define N 7
void input(int ar1[N]);
void max(int* mx);
int input2(int ar2[N], int mx);
int input3(int ar3[N], int ar1[N], int ar2[N]);

int main()
{
int ar1[N], mx, ar2[N], ar3[N], i;
input(ar1);
max(&mx);
input2(ar2, mx);
input3(ar3, ar1, ar2);
printf("array1 = %d %d %d %d %d %d %d\n", ar1[0], ar1[1], ar1[2], ar1[3], ar1[4], ar1[5], ar1[6]);
printf("array2 = %d %d %d %d %d %d %d\n", ar2[0], ar2[1], ar2[2], ar2[3], ar2[4], ar2[5], ar2[6]);
printf("array3 = %d %d %d %d %d %d %d\n", ar3[0], ar3[1], ar3[2], ar3[3], ar3[4], ar3[5], ar3[6]);
}

void input(int ar1[N])
{
int i;
printf("Enter 7 numbers: ");
for (i = 0; i < N; i++)
scanf("%d", &ar1[i]);
}

void max(int* mx)
{
printf("Enter random number bigger than 0 :- ");
scanf("%d", mx);
if (mx < 0)
{
printf("you have entered a number smaller than 0 please enter a number bigger than 0 :-");
scanf("%d", mx);
}
return mx;
}

int input2(int ar2[N], int mx)
{
int i;
srand(time(0));
for (i = 0; i < N; i++)
{
ar2[i] = 0 + rand() % (mx - 0 + 1);
printf("%d ", ar2[i]);
}
return ar2;
}

int input3(int ar3[N], int ar1[N], int ar2[N])
{
int i, j, a = 0;
for (i = 0; i < N; i++)
{
if (ar1[i] != ar2[0] && ar1[i] != ar2[1] && ar1[i] != ar2[2] && ar1[i] != ar2[3] && ar1[i] != ar2[4] && ar1[i] != ar2[5] && ar1[i] != ar2[6])
a = ar1[i];
ar3[i] = a;
}
return ar3;
}

感谢您的帮助。

最佳答案

以下建议代码:

  1. 处理问题评论中暴露的问题
  2. 干净地编译
  3. 添加了一些“适当的”水平间距以提高可读性

警告:我尚未验证 OP 发布的逻辑是否正确。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 7

// prototypes
void input ( int ar1[N] );
void max ( unsigned* mx );
void input2( int ar2[N], int mx );
void input3( int ar3[N], int ar1[N], int ar2[N] );


int main( void )
{
int ar1[N];
int mx;
int ar2[N];
int ar3[N];

srand( (unsigned)time(0) );

input( ar1 );
max( (unsigned*)&mx );

input2( ar2, mx );

input3( ar3, ar1, ar2 );

printf( "array1 = %d %d %d %d %d %d %d\n",
ar1[0], ar1[1], ar1[2], ar1[3], ar1[4], ar1[5], ar1[6] );
printf( "array2 = %d %d %d %d %d %d %d\n",
ar2[0], ar2[1], ar2[2], ar2[3], ar2[4], ar2[5], ar2[6] );
printf( "array3 = %d %d %d %d %d %d %d\n",
ar3[0], ar3[1], ar3[2], ar3[3], ar3[4], ar3[5], ar3[6] );
} // end function: main


void input( int ar1[N] )
{
printf( "Enter 7 numbers: " );

for ( size_t i = 0; i < N; i++ )
{
if( 1 != scanf("%d", &ar1[i]) )
{
perror( "scanf failed" );
exit( EXIT_FAILURE );
}
}
} // end function: input


void max( unsigned* mx )
{
printf("Enter random number bigger than 0 :- ");

if( 1 != scanf("%u", mx) )
{
perror( "scanf failed" );
exit( EXIT_FAILURE );
}
} // end function: max


void input2( int ar2[N], int mx )
{
for ( size_t i = 0; i < N; i++ )
{
ar2[i] = 0 + rand() % (mx - 0 + 1);
printf("%d ", ar2[i]);
}
} // end function: input2


void input3( int ar3[N], int ar1[N], int ar2[N] )
{
int a = 0;

for (size_t i = 0; i < N; i++)
{
if( ar1[i] != ar2[0]
&& ar1[i] != ar2[1]
&& ar1[i] != ar2[2]
&& ar1[i] != ar2[3]
&& ar1[i] != ar2[4]
&& ar1[i] != ar2[5]
&& ar1[i] != ar2[6] )
a = ar1[i];

ar3[i] = a;
}
} // end function: input3

关于c - printf 数组中选定的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44101115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com