printf problem

This is a discussion on printf problem within the C Programming forums, part of the General Programming Boards category; I am having a bit of a problem with my code. I only want it to print out Code: printf("Number ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    printf problem

    I am having a bit of a problem with my code. I only want it to print out
    Code:
    printf("Number not found after %d steps\n", steps);
    once at the end when it has finished searching but at the moment it prints out everytime. Any help in correcting this would be tops.

    Code:
    void linearSearch(int numbers[], int value)
    {
       /* Use linear search to find value in numbers.
          Print the number of steps it takes to find or not find value
       */
    
       int steps = 1, i;
    
       for (i=0; i < SIZE; i++)
       {
          if (value == numbers[i])
          {
              printf("Number found after %d steps\n", steps);
              return;
          }
          else
             printf("Number not found after %d steps\n", steps);
             steps++;
       }
    }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    According to your indention, you'll need {} after else and after steps:

    Code:
    } else {
        printf(blah)
        steps++;
    }

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    That wasn't the problem, just poor setting out on my part.

    I have worked it out though.

    Thanks.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Actually there seems to be a few problems with this as there would be a lot of printf calls even when you follow Kennedy's advice.

    Consider something like this:
    Code:
    #include <stdio.h>
    
    void linearSearch (int numbers[], size_t size, int value)
    {
      /* 
      ** Perform a linear search, that is, sequentially check every value
      ** in a group for a match 
      */
      int i;
      int stpflg = 0;
      for (i = 0; !stpflg && i < size; ++i)
      {
        if (numbers[i] == value)
        {
          stpflg = 1;
        }
      }
      printf("Number%sfound after %d steps.\n", stpflg ? " " : " not ", i);
    }
    
    int main(void)
    {
      int numbers[] = { 42, 3, 6, 5, 2, };
      size_t size = sizeof numbers / sizeof numbers[0];
      linearSearch(numbers, size, 5);  /* should be there */
      linearSearch(numbers, size, 7);  /* not supposed to be there */
      return 0;
    }
    $ ./a.exe
    Number found after 4 steps.
    Number not found after 5 steps.
    Last edited by whiteflags; 09-21-2006 at 02:00 AM. Reason: gosh am I late...

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Or you can avoid the stpflg by just breaking out of the loop when you find the value and then changing the condition in the printf() from stpflg to i == size.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 08:38 AM
  2. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 04:35 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21