需要关于数组和循环的帮助

Need help with arrays and loops

本文关键字:循环 帮助 数组 于数组      更新时间:2023-10-16

使用单下标数组解决以下问题:读入20个数字,每个数字的范围都在10到100之间(含100)。当读取每个数字时,只有当它不是已读取的数字的重复时才打印它。提供所有20个数字都不同的"最坏情况"。使用尽可能小的数组来解决这个问题。

到目前为止是这样的:

#include <stdio.h>
#define SIZE 20
int duplicate (int num[] );
int main ()
{
    int i, numbers[ SIZE ];
    printf( " Enter 20 numbers between 10 and 100:n " );
    scanf_s( "%dn" );
    for (int i = 0; i < SIZE  - 1; i++ ); 
    {

int duplicate( int num[] )
{
    int i, hold;
    for ( i = 0; i <= SIZE - 1; i++ )
        if ( num[i] == num[i=1] ){
            hold = num[i];
            else
                hold = num[i+1];
        }
    printf( "%3dn," num[ i ] );
}

不幸的是,你的教授可能不够聪明,无法解决他自己的问题。对于这个问题,最小的数组大小为2(假设是64位数据类型,这是标准提供的最大数组)。对于32位整数,它需要3个元素,对于128位整数,它只需要1个元素。

#include <stdint.h>
#include <stdio.h>
int main(void)
{
    int_fast64_t visited[2] = { 0 };
    int inputs_left = 20;
    do {
        int input, slot;
        int_fast64_t mask;
        puts("Enter an integer between 10 and 100: ");
        if (!scanf("%d", &input)) {
            puts("That's not a number!n");
            continue;
        }
        if (input < 10 || input > 100) {
            puts("Out of range!n");
            continue;
        }
        slot = (input - 10) >> 6;
        mask = 1 << ((input - 10) & 0x3F);
        if (visited[slot] & mask) {
             puts("Already seen, it is a duplicate.n");
        }
        else {
            visited[slot] |= mask;
            printf("%d is newn", input);
        }
        inputs_left--;
    } while (inputs_left);
    return 0;
}

欢迎你在作业中使用这些代码,如果你能正确地解释它是如何工作的(我希望你的教授教你如何写注释)。

这是我的想法,感谢大家的帮助:

  #include <stdio.h>
  #define MAX 20
  int main()
  {
int a[ MAX ] = { 0 };  /* user input */
int i;                 /* counter */
int j;                 /* counter */
int k = 0;             /* number of integers entered */
int duplicate;         /* notify of duplicates */   
int value;              
printf( "Enter 20 numbers between  10 - 100;n" );
      /* ask user for 20 numbers */
      for ( i = 0; i <= MAX - 1; i++ ){
    duplicate = 0;
    scanf( "%d", &value);
     /* decide if integer is duplicate */
    for ( j = 0; j < k; j++ ) {
     /* notify and stop loop if duplicate */
        if ( value == a[ j ] ) {
        duplicate = 1;
        break;
        { /* end if */

     /* enter number into array if it's not a duplicate */
        if ( !duplicate )
        a[ k++ ] = value;
} /* end if */

你的代码有几个问题:

  • 复制函数在主函数内部。
  • i被多次声明
  • 第一个for循环后不应该有分号。
  • hold变量没有被用于任何事情。它只是被赋值。
  • num[i=1] -不知道你在这里想做什么,但是i=1是将i设置为1。
  • 在第一个for循环中,条件是i if (test) { // code } else { // code }

    if (test)
        // code
    else
        // code
    

对于逻辑:

  • 你只得到一个整数,你没有把它放在数字数组中。你需要一个接一个地得到20个整数,并且每次用户输入一个数字时检查数组。
  • duplicate函数可能应该接受第二个参数,即您想要检查的数字。if语句将检查num[i]是否等于您正在查找的数字。
  • 记住初始化数组值,并且只检查已设置的值。例如,当用户输入第三个数字时,您只需要检查数组中的前两个数字,看看它是否已经存在。

PS:请试着正确缩进你的代码。如果没有正确缩进,许多人甚至不会尝试帮助。

我的C相当生疏,所以这里有一个伪代码解决方案(因为这个作业,你应该自己做一些):

print initial prompt;
declare nums[ array size 20 ]; // I later assume a 0-based index
declare boolean found;
for (i=0; i < 20; i++) {
  // prompt for next number if desired
  read next number into nums[i];
  found = false;
  // compare against all previously read numbers
  for (j=0; j < i; j++) {
    if (nums[j] == nums[i]) {
      found = true;
      break;
    }
  }
  if (!found) {
    print nums[i];
  }
}

注意:这个问题并没有说数字必须是整数。此外,它说"使用尽可能小的数组"—如果为当前数字引入一个非数组变量,则可以使用包含19个元素的数组(因为读取的第20个数字只需要与前19个数字进行检查,而不需要与本身进行检查),但这会使代码更加复杂。

请参阅我在上面发布的评论,其中提到了您的代码中的一些具体错误。检查括号是否匹配