-3

this is code for linear search in 10 array table btw it has to be in do-while or while loop not for loop.

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

int
search(int t[], int x, int n)
{
    int i;

    i == 0;

    do {
        i = i + 1;
    } while ((t[i] != x) || (n > i));

    if (t[i] == x) {
        printf("index=%d", i + 1);
    }
    else {
        printf("element not found");
    }
}

int
main()
{
    int t[10];
    int x, i;

    printf("enter x: ");
    scanf("%d", &x);

    for (i = 0; i < 10; i++) {
        printf("enter table element : ");
        scanf("%d", &t[i]);
    }

    search(t, x, 10);

    return 0;
}

i excepted to get the index of the specified number (x) in the table but the problem is that results are wrong (it show 3 digit number) and sometimes won't show anything at all

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
sans 1990
  • 1
  • 1
  • Please auto-indent/auto-format/beautify your code, as a basic courtesy to those you are asking to help you. – hyde Nov 26 '22 at 21:39
  • Try changing your `||` to `&&`. Also, to prevent a range error you'll want that check against `n` to be first. – 500 - Internal Server Error Nov 26 '22 at 21:39
  • 1
    Turn on warnings and fix them! Or, if a warning is unclear, ask *about the warning*. Or , ask how to turn on warnings, if Google doesn't help. – hyde Nov 26 '22 at 21:43
  • You should probably initialize i to 0 instead of comparing an uninitialized value to 0. And then note your loop will never look at the first element of the array. – Shawn Nov 26 '22 at 21:45
  • @500 - Internal Server Error thanks that worked – sans 1990 Nov 26 '22 at 21:54
  • 1
    @hyde sorry i'm new to this didn't realize this fomat is considered ugly,i will make sure to beautify my codes next time – sans 1990 Nov 26 '22 at 21:54
  • 1
    Welcome to Stack Overflow. Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal Complete Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Taking a few minutes to familiarize yourself with the norms and expectations for questions will help everyone help you (and help you avoid downvotes in the future). Welcome aboard @sans1990 – David C. Rankin Nov 26 '22 at 22:06

1 Answers1

0

This line of code has no effect. The == is comparison operator

i == 0;

Instead, you should use asignemnt operator:

i = 0;

I slightly changed your code and made it works. Please pay attention of forma t strings in scanf function, it is important.Please read article scanf() leaves the newline character in the buffer

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

int
search(int t[], int x, int n)
{
    int i = 0;

    do {
           if( t[i] == x )
           {
              printf("index=%d", i);   
              return 0;
           }  
    } while (i++ < n );

    printf("element not found");
    return 1;
}



int
main()
{
    int t[10];
    int x;

    printf("enter x: ");
    scanf(" %d*c", &x);
    if( errno) perror("scanf x");

    for (int i = 0; i < 10; i++) {
        printf("enter table element : ");
        scanf(" %d*c\n", &t[i]);
    }

    search(t, x, 10);

    return 0;
}
risbo
  • 188
  • 7