Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果在数组中找到元素,则尝试打印一件东西;如果在C中不在数组中,则尝试打印其他东西_C_Arrays - Fatal编程技术网

如果在数组中找到元素,则尝试打印一件东西;如果在C中不在数组中,则尝试打印其他东西

如果在数组中找到元素,则尝试打印一件东西;如果在C中不在数组中,则尝试打印其他东西,c,arrays,C,Arrays,如果在for循环中找到某个元素,我会尝试打印一个元素,如果找不到,则打印其他元素。这应该很简单,但我尝试过许多不同的方法,但似乎没有一种有效 int squaresArray[1000]; int numberOfSquares = 1000; int i = 0; int found = 0; int number = 100; for (; i<numberOfSquares; i++) { squaresArray[i] = i*i; if (number==squ

如果在for循环中找到某个元素,我会尝试打印一个元素,如果找不到,则打印其他元素。这应该很简单,但我尝试过许多不同的方法,但似乎没有一种有效

int squaresArray[1000];
int numberOfSquares = 1000;
int i = 0;
int found = 0;
int number = 100;

for (; i<numberOfSquares; i++)
{
    squaresArray[i] = i*i;
    if (number==squaresArray[i])
    {
        found = 1;
    }
            if (found == 1){
                printf("%d is a perfect square", number); 
                break;}
            else {
                printf("%d is not a perfect square", number);
                break;} 
    }
int squaresArray[1000];
int numberOfSquares=1000;
int i=0;
int=0;
整数=100;
对于(;i
int squaresArray[1000];
int numberOfSquares=1000;
int i=0;
int=0;
整数=0;
为了
如果在for循环中找到某个元素,我会尝试打印一件东西,如果找不到,则打印其他东西

伪代码:

int found = 0;
for each element e {
    if e is the one I'm looking for {
        found = 1
        stop the loop
    }
}
if (found)
    print one thing
else
    print something else

使用库函数的另一种方法:

  int size = numberOfSquares / sizeof squaresArray[0];

  qsort(ints, size, sizeof(int), int_cmp);

  int *res = bsearch(&number, squaresArray, size, sizeof(squaresArrays[0]), int_cmp);

  if (res == NULL) {
        printf("%d not found\n", number);
  } 
  else {
        printf("got %d:\n", *res);
  }
您还需要提供比较功能:

int int_cmp(const void* a, const void* b) {
    const int *arg1 = a;
    const int *arg2 = b;
    return *arg1 - *arg2;
}

您显示的代码非常耗时,因为如果数字是平方999,则需要迭代上千次

使用
math.h
中的函数查找给定的数字是否为完美平方

试试这个

double param = 1024.0; //read different inputs with the help of scanf(). 
int i;

if ( ( (  i= (int) (sqrt(param)*10)  ) % 10) == 0 )  

      printf(" is a perfect square");
else
      printf(" is not a perfect square");
从@jongware的评论来看,这比上面提到的要复杂,而且容易理解

   if ( ((int)sqrt(param))*((int)sqrt(param))==param)  

          printf(" is a perfect square");
    else
          printf(" is not a perfect square");     
“我试图不使用math.h编写代码”

以下是不使用
库的解决方案。

我想,您知道您的程序可以找到唯一的“完美正方形”: 无论如何,请看评论:他们会描述一些东西

使用
continue
而不是
break
,以避免中断
for
-循环

    int squaresArray[1000];
    int numberOfSquares = 999;
    int i;
    int found = 0;
    int number = 100;


    for ( i = 0 ; i < numberOfSquares; i++ )
    {
        squaresArray[i] = i*i;
        if (number == squaresArray[i])
            found = 1;
        else
            found = 0;

        if (found == 1){
            printf("%d is a perfect square", number);
            continue;   // Skipping the current loop and incrementing the `i` variable
        }
        else {
         // printf("%d is not a perfect square", number); 
         /* If you remove the slashes, you'll see 999 "N is not a perfect square"s,
            so you see why I've marked it as comment :) */
            continue;   // Skipping the current loop and incrementing the `i` variable
        }
    }
int squaresArray[1000];
int numberOfSquares=999;
int i;
int=0;
整数=100;
对于(i=0;i
试试这个:

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

int main(){

int squaresArray[1000];
int numberOfSquares = 1000;
int i;
int found = 0;
int number = 100;

for (i=0; i<numberOfSquares; i++){
    squaresArray[i] = i*i;

    if (number==squaresArray[i]){
        found = 1;
        break;
    }
 }

if (found == 1){
      printf("%d is a perfect square of %d\n", number,i); 
}else {
      printf("%d is not a perfect square", number);

}
return 0;
}

这是所需要的吗?

在您声明了
数字的地方
?您可能在代码之外的某个地方使用它,但您不需要数组来判断数字是否是完美的正方形。“数字”由用户在main()中输入并作为参数传递给此函数。否则,在不使用完全平方填充数组或使用math.h或类似工具的情况下,如何执行此操作?@user2850602;将您的
main
函数添加到问题中。
if(number==squaresArray[i])
将在第一次迭代后始终变为false。我尝试了此解决方案,但我认为“find”变量无法在if语句之外访问,因此它似乎总是报告该“number”即使是完美正方形,也不是完美正方形。您的代码将仅为第一个元素打印
%d[是/不是]完美正方形“
:语句
如果(找到==1)
不属于
for
-循环。无论如何,有
break
语句,如果它们在
for
-循环中,则会导致退出该循环。是的,此语句似乎不起作用,因为“found”变量超出了if语句的范围,无法访问,所以它总是报告数字不在数组中,即使打印数组的元素证明它确实在数组中。“find”变量超出了if语句的范围“@librik:是的,您是正确的。我似乎无法访问if块之外的“find”的更新值,但现在我可以了。真奇怪。第一个printf语句中的“key”变量指的是什么?只是一个输入错误,它应该是您搜索hanks的数字!!!这很有效。我试着不用数学来编码,但是我可以用数学。这似乎起到了作用。非常欢迎。。如果你有机会使用math.h,你可以使用这个。这里有一件简单的事情,找到数字的sqrt,返回双精度值。检查小数点后是否有任何内容。为了获得更准确的结果,在if语句中,用100乘以并用100查找提醒,或者甚至用1000乘以并用1000查找提醒。因为如果sqrt返回xx.01,上面的一个将显示完美的平方。检查
((int)sqrt(param))*((int)sqrt(param))是否更安全
是否再次返回原始整数?那么你就不必受小数的摆布了。当然,双
sqrt
将被优化为一个。
((int)sqrt(param))*((int)sqrt(param))==param
使这变得非常简单。谢谢。这也应该起作用:
param%((int)sqrt(param))==0
。它用一个sqrt换一个div,然后松开乘法。是的,这是有效的。谢谢我不知道为什么我以前不能访问if语句之外的“find”变量,但这确实有效。它不起作用,因为在第一次迭代之前,您从未让循环进行得更远。。。在else语句中,有一个
break
命令。。所以在第一次迭代中,found不等于1,所以循环进入else语句并中断。。。你的else声明中不应该有中断。。如果您满意,也请接受答案
#include <stdio.h>
#include <stdlib.h>

int main(){

int squaresArray[1000];
int numberOfSquares = 1000;
int i;
int found = 0;
int number = 100;

for (i=0; i<numberOfSquares; i++){
    squaresArray[i] = i*i;

    if (number==squaresArray[i]){
        found = 1;
        break;
    }
 }

if (found == 1){
      printf("%d is a perfect square of %d\n", number,i); 
}else {
      printf("%d is not a perfect square", number);

}
return 0;
}
100 is a perfect square of 10