【问题标题】:C programming_--Program dumped- I wrote this code to check if the number duplicates in array and should start at 1 while the code starts at 11 tooC 编程——程序转储——我编写了这段代码来检查数组中的数字是否重复,并且应该从 1 开始,而代码也从 11 开始
【发布时间】:2021-11-18 09:19:41
【问题描述】:

代码应在代码被转储时检查数字是否重复。你能帮我弄清楚吗

A) 编写一个以整数数组作为输入的函数。此函数检查数组中是否有任何重复值。如果有,则返回 true(或 1)。如果没有,则返回 false(或 0)。

#include<stdio.h>
#include<stdbool.h>
bool part_A (int array_A[10]);

int main ()
//THIS IS THE MAIN FUNCTION
{
int array_A [10];
int i=0;

for (i = 0; i < 10; i++);
{
    printf("Input number %d: ", i+1);
    scanf("%d", &array_A[i]);
}
int return_A = part_A (array_A);
//function

if (return_A == true)
{
      printf("There are duplicate values.\n");
}
else
{
    printf ("All values are unique.\n");
}

printf("-----------------\n");

return 0;
}


bool part_A (int array_A[10])
//function 
{
int i, j;
//making loop and checking
for (i=0; i<10; i++);
    for (j=i+1; j<10; j++);
    {
    if (array_A[i]== array_A[j]);
    //checking for duplicates
        return true;
    //if repeated return true
    }
   
    return false;
 //if no duplicates returning false which means unique value
 
}

【问题讨论】:

  • for (i=0; i&lt;10; i++); - ; 不好。养成在 for 本身 (for (int i = ...) 中声明循环变量的习惯,这将使编译器能够告诉您有问题。
  • 您显示的代码有什么问题?当你构建它时会发生什么(启用额外的警告)?当你运行它时会发生什么?当你运行它时,你期望会发生什么?你给程序的输入是什么?你得到什么输出?你期望的输出是什么?请花一些时间阅读the help pages,阅读SO tour,阅读How to Ask,以及this question checklist。最后请edit你的问题来改进它。
  • 您应该在提问时将错误日志与代码一起发布。这是一个很好的做法。

标签: arrays c


【解决方案1】:

试试这个:

#include<stdio.h>
#include<stdbool.h>

//function 
bool part_A (int array_A[10]) {
//making loop and checking
    for (int i = 0; i < 10; i++)  {
        for (int j = i + 1; j < 10; j++) {
            //checking for duplicates
            //if repeated return true
            if (array_A[i] == array_A[j])
                return true;
        }
   
    }
    //if no duplicates returning false which means unique value
    return false;
}

int main ()
//THIS IS THE MAIN FUNCTION
{
int array_A[10];

printf("Input 10 elements in the array :\n");
for (int i = 0; i < 10; i++) {
    printf("Input number %d: ", i);
    scanf("%d", &array_A[i]);
}

printf("\nElements in array are: ");  
for(int i = 0; i < 10; i++)  {  
    printf("%d  ", array_A[i]);  
} 
printf("\n");

int return_A = part_A (array_A);
//function

if (return_A == true) {
      printf("There are duplicate values.\n");
} else {
    printf ("All values are unique.\n");
}

printf("-----------------\n");

return 0;
}

执行:

$ gcc dup.c 
$ ./a.out 
Input 10 elements in the array :
Input number 0: 10
Input number 1: 9
Input number 2: 9
Input number 3: 8
Input number 4: 7
Input number 5: 6
Input number 6: 5
Input number 7: 4
Input number 8: 3
Input number 9: 2

Elements in array are: 10  9  9  8  7  6  5  4  3  2  
There are duplicate values.
-----------------
$ ./a.out 
Input 10 elements in the array :
Input number 0: 10
Input number 1: 9
Input number 2: 8
Input number 3: 7
Input number 4: 6
Input number 5: 5
Input number 6: 4
Input number 7: 3
Input number 8: 2
Input number 9: 1

Elements in array are: 10  9  8  7  6  5  4  3  2  1  
All values are unique.
-----------------

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 2018-03-22
    • 1970-01-01
    相关资源
    最近更新 更多