如果在数组中找到元素,则尝试打印一个东西,或者如果不在C中的数组中则打印其他内容

时间:2013-10-05 23:03:16

标签: 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==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;} 
    }

有几个问题,“found”变量超出了if语句之外的范围,所以我不能在if语句之外执行printf部分,或者它只打印“[number]不是一个完美的广场“几十次。我怎样才能做到这一点?我花了好几个小时来解决这个问题。

6 个答案:

答案 0 :(得分:1)

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

使用math.h中的sqrt()函数查找给定数字是否为完美正方形

尝试一下。

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");     

答案 1 :(得分:0)

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

for (; i<numberOfSquares; i++)
{
    squaresArray[i] = i*i;
    if (number==squaresArray[i]){
        found = 1;
        break;
     }
}
if (found == 1){
                printf("%d is a perfect square", number); 
                break;}
            else {
                printf("%d is not a perfect square", number);
                break; 
    }

答案 2 :(得分: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

答案 3 :(得分:0)

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

  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;
}

答案 4 :(得分:0)

“我试图在不使用math.h的情况下对其进行编码”

以下是未使用<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
        }
    }

答案 5 :(得分:0)

试试这个:

#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

这是期望的吗?