简体   繁体   中英

Trying to print one thing if found element in array, or print something else if not in array in C

I'm trying to print one thing if a certain element is found within a for loop, or print something else if it's not found. This should be simple, but I've tried to do it many different ways, and none of them seem to work.

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

There are a couple of problems, the "found" variable goes out of scope outside of the if statement, so I can't do the printf part outside of the if statement, or it just prints "[number] is not a perfect square" dozens of times. How can I do this? I've spent hours on this problem.

The code which you are showing is very time consuming because You need to iterate thousand times if the number is square 999.

Use sqrt() function from math.h to find whether the given number is perfect square are not

Give a try to this.

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

from @jongware comment, this is tricky than above and easy to understand.

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

          printf(" is a perfect square");
    else
          printf(" is not a perfect square");     
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; 
    }

I'm trying to print one thing if a certain element is found within a for loop, or print something else if it's not found

Pseudocode:

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

Another approach using library functions:

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

You also need to provide the comparison function:

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

"I was trying to code it without using math.h"

Here is the solution without using <math.h> library.

I suppose, you know that there is the only "perfect square" that your program can find :) Anyway, see the comments: they will describe something.

Use continue instead of break in order not to break the for -loop.

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

Try this :

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

this gives me the following output:

100 is a perfect square of 10

Is this what is desired?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM