C 函数和数组,允许您打印';这是重复次数最多的数字
我的代码有问题,它没有打印我期望的结果。此代码允许用户输入任意数量的数字,然后打印重复次数最多的数字 这是:C 函数和数组,允许您打印';这是重复次数最多的数字,c,C,我的代码有问题,它没有打印我期望的结果。此代码允许用户输入任意数量的数字,然后打印重复次数最多的数字 这是: #include <stdio.h> void reading_numbers(int array[]){ int i = 0; int Max = 0; printf("How much long the array will be?\n"); scanf("%d", &Max); while (i < Max) {
#include <stdio.h>
void reading_numbers(int array[]){
int i = 0;
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
}
void most_present_number(int array[], int Max){
int i = 0;
reading_numbers(array);
int current_number = array[i];
int current_number_count = 0;
int most_present_one = 0;
int most_present_one_counter = 0;
while (i < Max) {
if (array[i] == current_number) {
current_number_count++;
i++;
} else {
if (current_number_count > most_present_one_counter){
most_present_one = current_number;
most_present_one_counter = current_number_count;
}
current_number_count = 1;
}
}
printf("This is the most present number %d it is repeated %d times\n", most_present_one,
most_present_one_counter);
}
int main() {
int Max = 0;
int array[Max];
most_present_number(array, Max);
return 0;
}
#包括
无效读取_数(整数数组[]){
int i=0;
int Max=0;
printf(“阵列将有多长?\n”);
扫描频率(“%d”和最大值);
而(i最多出现计数){
most_present_one=当前_编号;
most_present_one_计数器=当前_数_计数;
}
当前数量=1;
}
}
printf(“这是最新出现的数字%d,重复了%d次\n”),最新出现的数字,
大多数人(一个柜台);
}
int main(){
int Max=0;
整数数组[Max];
最大当前数量(数组,最大值);
返回0;
}
我的问题是当我调用函数时,但我不知道如何修复它
我本应该以写为前提,但我对C有点陌生,所以这段代码中可能有些东西没有意义我创建了一个过程来查找结果,int main()必须具有数组的大小(错误逻辑),因为该过程采用了主函数(int main())的参数 这是我的密码:
#include<stdio.h>
void most_present_number(int Max,int T[Max])
{
int i = 0;
while (i < Max)
{
printf("Insert the numbers :");
scanf("%d", &T[i]);
i++;
}
int k=0,cpt1=0,cpt=0;
for(int i=0;i<Max;i++)
{
cpt=0;
for(int j=i+1;j<Max;j++)
{
if(T[i]==T[j])
{
cpt++;
}
}
if(cpt>=cpt1)
{
cpt1=cpt;
k=T[i];
}
}
printf("This is the most present number %d it is repeated %d times\n",k,cpt1+1);
}
int main()
{
int Max = 0;
do
{
printf("How much long the array will be?\n");
scanf("%d", &Max);
}while(Max<1);
int T[Max];
most_present_number(Max,T);
}
#包括
无效最大当前值(int Max,int T[Max])
{
int i=0;
而(i 对于(int i=0;i以下建议代码:
#include <stdio.h>
void reading_numbers( int Max, int array[ Max ][2])
{
for( int i = 0; i < Max; i++ )
{
printf("Insert the numbers\n");
scanf("%d", &array[i][0]);
array[i][1] = 0;
}
}
void most_present_number( int Max, int array[ Max ][2] )
{
for( int i=0; i < Max; i++ )
{
for( int j=i; j<Max; j++ )
{
if ( array[i][0] == array[j][0] )
{
array[i][1]++;
}
}
}
int most_present_one = array[0][0];
int most_present_one_counter = array[0][1];
for( int i=1; i<Max; i++ )
{
if( most_present_one_counter < array[i][1] )
{
most_present_one = array[i][0];
most_present_one_counter = array[i][1];
}
}
printf("This is the most present number %d it is repeated %d times\n",
most_present_one,
most_present_one_counter);
}
int main( void )
{
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
int array[Max][2]; // uses variable length array feature of C
reading_numbers( Max, array );
most_present_number( Max, array );
return 0;
}
干净地编译
执行所需的功能
仅包括那些实际使用的内容的头文件
现在建议的守则是:
#include <stdio.h>
void reading_numbers( int Max, int array[ Max ][2])
{
for( int i = 0; i < Max; i++ )
{
printf("Insert the numbers\n");
scanf("%d", &array[i][0]);
array[i][1] = 0;
}
}
void most_present_number( int Max, int array[ Max ][2] )
{
for( int i=0; i < Max; i++ )
{
for( int j=i; j<Max; j++ )
{
if ( array[i][0] == array[j][0] )
{
array[i][1]++;
}
}
}
int most_present_one = array[0][0];
int most_present_one_counter = array[0][1];
for( int i=1; i<Max; i++ )
{
if( most_present_one_counter < array[i][1] )
{
most_present_one = array[i][0];
most_present_one_counter = array[i][1];
}
}
printf("This is the most present number %d it is repeated %d times\n",
most_present_one,
most_present_one_counter);
}
int main( void )
{
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
int array[Max][2]; // uses variable length array feature of C
reading_numbers( Max, array );
most_present_number( Max, array );
return 0;
}
如果将数组作为参数传递,为什么要在reading_numbers
中询问数组的长度?如果不调整数组的大小,数组的大小怎么可能是0
?@Sub0Zero1990这样一个变长数组int Max=0;int array[Max]的声明;是禁止的。数组可能没有零元素。因此程序没有意义。@Sub0Zero1990首先,您应该要求用户指定数组的元素数,然后在该数为正数的情况下,用指定的元素数声明数组。在循环中,您必须始终递增i
on每次迭代。使用:for(;i
并删除下的i++
,如果
@IrAM很抱歉出错,您也可以处理输入,如12345@MEDLDN谢谢你的回答,但如果可能的话,我想将数字读取操作存储在另一个数据库中function@Sub0Zero1990,不,你不能做你想要的,因为函数应该从主函数(int main())中删除它们的参数。包含未使用的头文件是一种糟糕的编程实践。建议删除:#include