【问题标题】:Can't Create Function to Create Unique Array无法创建函数来创建唯一数组
【发布时间】:2020-03-27 17:37:39
【问题描述】:

代码的目的: 我正在尝试从用户输入创建一个代码,它将从用户输入生成一个“大小(不超过 7)”长度的数组,使用函数 'uniqueRand' 然后它将生成 1-9 的随机数,然后使用函数'found' 检查数组中是否已经存在该数字,如果存在则创建一个新数字。

问题: 编译时,我收到一个警告说变量'value'没有被使用,但我在函数中声明它,然后在它下面的for循环中使用它

#include <stdio.h>

#include <time.h>

#include<stdlib.h>

int uniqueRand (int a[], int size);

int found (int a[], int size, int value);

int main (){
int size, guesses;
printf ("Number of digits?: ");
scanf ("%d", &size);
int a[size];
printf ("\nNumber of guesses?: \n"); 
scanf ("%d", &guesses);  
int uniqueRand (int a[],int size);  
    for(int i=0;i<size;i++){
        printf ("%d",a[i]);
            //Print Answer Code  
    }      
}
int uniqueRand (int a[], int size){
      //Generate Random Array Function
int value;
    for (int i = 0; i < size; i++){
        if (i == 0){
            a[0] = rand () % 8 + 1;
        }  
        else{
            while (1){
                a[i] = rand () % 8 + 1;
                value = a[i]; //value to check for     
                int found (int a[],int size,int value);//Check if value is in a[], if true then redo
            }
        }
    }
return 1;
}

int found (int a[], int size, int value){   
    for(int i=0;i<size;i++){    
        if (a[i] == value){ //checks if it is in a[i]  
            break;
            return 1;
        }
    //if it is return 1 and break, because need a new number          
    }
return 0;
}

【问题讨论】:

  • 您的while(1) 陷入了无限循环。而且您在该循环中给出了found 函数的原型,而不是调用该函数。
  • 你觉得这条线有什么作用? int found (int a[],int size,int value);//Check if value is in a[], if true then redo 它具有原型声明的语法,但您的评论似乎表明您认为它是对该函数的调用。这意味着 value 被写入,但从未被读取。
  • 感谢大家的帮助。我了解调用问题,并且在没有任何警告的情况下解决了它,但是我在运行时陷入了无限循环。通过引入 while(1),我调用了 found(a, size, value),如果值已经存在于 a 中则返回 1,如果不存在则返回 0。为什么不退出?
  • 请为您的新问题创建一个新答案。同时,请告诉我如何才能使我对这个问题的回答(按要求)对您更有帮助。

标签: c arrays function


【解决方案1】:

这一行

int found (int a[],int size,int value);//Check if value is in a[], if true then redo

具有原型声明的语法,但您的评论似乎表明您认为它是对函数的调用。它不是。除了在函数中不寻常地声明原型(我建议不要...),这意味着值被写入,但从不被读取。

我不得不猜测,但您打算做的可能需要函数的调用语法,类似于以下内容(注意没有类型):

found(a, size, value);

我并不是说它在功能上实现了 yoru 目标,但语法意味着现在 value 实际上用于某事,而不是被写入或被忽略。

您的函数有一个返回值,您可能会想要使用它(返回值上方的行被忽略...)。但这不在您的问题范围内。

【讨论】:

    【解决方案2】:

    我还有另一个运行时错误,似乎是 found(a,size, value) 函数。如果 find 找到了值,则返回 i,如果没有找到则返回 -1,while(1){ 应该会中断,但不会,代码会永远运行。

    #include <stdio.h>
    
    #include <time.h>
    
    #include<stdlib.h>
    
    int uniqueRand (int a[], int size);
    
    int found (int a[], int size, int value);
    
    int main (){
    
    int size, guesses;
    
    printf ("Number of digits?: ");
    
    scanf ("%d", &size);
    
    int a[size];
    printf ("\nNumber of guesses?: \n");
    
    scanf ("%d", &guesses);
    
    uniqueRand (a,size);
    //Generate Random Array Callout
    
            for(int i=0;i<size;i++){
    
            printf ("%d",a[i]);
    //Print Answer Code  
        }   
    
    }
    
    int uniqueRand (int a[], int size){
    //Generate Random Array Function
        int value;
        srand(time(NULL));
        for (int i = 0; i < size; i++){
    
            if (i == 0){    
                a[0] = rand () % 8 + 1; 
            }         
            else
            {
                while (1){
                    a[i] = rand () % 8 + 1;
                    value = a[i]; //value to check for     
                    found (a,size,value);
                 //Checks to see if value is in a[] already, if true then redo 
                        if (found(a,size,value) == -1) 
                        //If -1 return, number is unique, break
                            break;
                }
    
            }
    
        }
    return a[size];
    
    }
    
    int found (int a[], int size, int value){
    
        for(int i=0;i<size;i++){
    
            if (a[i] == value) //checks if value is in a[i]  
                return i; //if yes, returns positive number
        }                       
        return -1; //return -1 if unique        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 2015-11-14
      • 2017-07-02
      相关资源
      最近更新 更多