GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Oct-2005, 00:59
Yvonne Yvonne is offline
New Member
 
Join Date: Aug 2005
Posts: 5
Yvonne is on a distinguished road

Unable to search for the nearest value


Hi,

I'm having a problem on the searching for the nearest value. Here's my code, can anyone help to see what's the problem of my code?

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define N 5
#define TRUE 1
#define FALSE 0

int main()
{
    int search = 0;
    int prev = 0;
    int i = 0;
    int array[5];
    int found = FALSE;
    int temp=0;
    int check = 0;
    int curr=0;

    array[0] = 6;
    array[1] = 30;
    array[2] = 15;
    array[3] = 40;
    array[4] = 50;

    for (i = 0; i < N ; i++)
        printf("array = %d\n", array[i]);

    i = 0;

    printf("\n-------------------\n\n");
    printf("Please input search = ");
    scanf("%d", &search);
    for (i = 0; i < N; i++)
    {
        curr = array[i];

        if (curr == search)
        {
            found = TRUE;
            temp = curr;
            break;
        }
        else
        {
            if (N > 1)
            {
                if (search < curr)
                {

                    if (temp < check)
                    {
                        temp = check;
                    }
                    else
                    {
                        temp = curr;
                        if (temp < check)
                        {
                            temp = temp;
                        }
                    }
                    check = curr;
                }
                else
                {
                    continue;
                }
            }
            else
            {
                if (curr > search)
                {
                    found = TRUE;
                    temp = curr;
                    break;
                }
            }
        }
    }


    if (found)
        printf("\n\n    Congratulation! Search value [%d] was found at position [%d] and it was [%d].\n", search , i, array[i]);
    else
        printf("\n\n    Search value [%d] was not found.\n", search);

    return 1;
}
Last edited by LuciWiz : 07-Oct-2005 at 01:19. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 07-Oct-2005, 04:07
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Unable to search for the nearest value


hi..

Your code has no errors.
it compiled and ran successfully.
what compiler do you use?

did your program displays the array, asks for the input and exits immediately without showing you the output?

Then you should pause your program for a while to show the output.

You may use getchar() to make your program wait.
But there are other options..

You can get more information on pausing a program here.


i think your program is too much complicated to find a number in an array.
i have written a simple(i think) C code for searching a value in an array.

the program is:
CPP / C++ / C Code:
#include<stdio.h>

int main()
{
    int a[] = {1,2,3,4,5,6,7,8,9,10};   //the array which has values..

    int  b , i , count=0;                    

     printf("The values in the array are:  \n");

    for( i = 0; i<10 ; i++)
    {
    printf(" %d   ",a[i]);                    //prints all the values in a
    }

    printf(" \n Enter the number you wanted to find...   " );
    scanf( "%d" ,&b);                       //b is the value to be searched
    

    for( i=0 ; i<10 ; i++)                   //search till the end of the array  
    {

        if( b == a[ i ] )                       //checks whether there is any value in the array like b
        {

           printf(" Occurence of %d found in the array at position %d.. " , b , i );               //prints that you have found a value.           
           count++;                           //count is used to check whether we have found a value or not. count is incremented here implies that we have found a value

        }
                              
    }

    if( count==0 )                            //if count is 0 then we have not found a value like b
    {
         printf( " Search value %d not found in array..  " , b );
    }
    
     getchar();                               //wait until we press a key...and then exit.

     return 0;

}                          //end of the main program

Bye,
Paramesh
  #3  
Old 07-Oct-2005, 04:16
Yvonne Yvonne is offline
New Member
 
Join Date: Aug 2005
Posts: 5
Yvonne is on a distinguished road

Re: Unable to search for the nearest value


Hi,

I'm using GCC as my compiler. My program is correct, I'm trying to search for the nearest value and not the exact value in unsorted array.

For example, if I search for value 14, the result should gave me 15 (the nearest) but now it gave me 30.

Anyway, thanks for your help.

Regards,
  #4  
Old 07-Oct-2005, 09:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Unable to search for the nearest value


I agree that the program is more complex than it needs to be. What you could do is simply run a loop like Paramesh did. Keep track of
1) the index of the current minimum
2) target is the value looking for
and test for
CPP / C++ / C Code:
if( (abs(target - a[j]) < abs(a[index] - a[j]) ) index = i;
instead.

So which value to you save if you find two values? Like if 15 and 13 are in the list?
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #5  
Old 07-Oct-2005, 10:40
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Unable to search for the nearest value


hi Yvonne,
i am terribly sorry that i misunderstood your program..
what mislead me was your last two printf statements..
CPP / C++ / C Code:
   if (found)
        printf("\n\n    Congratulation! Search value [%d] was found at position [%d] and it was [%d].\n", search , i, array[i]);
    else
        printf("\n\n    Search value [%d] was not found.\n", search);

Bye,
Paramesh
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
database search skyloon MySQL / PHP Forum 6 04-Jun-2006 13:09
Web Site Optimization - the basic things dimos31 Search Engine Optimization Forum 3 19-Jun-2005 13:00
Using meta tags help in ranking on some search engine pcx Search Engine Optimization Forum 8 29-Mar-2005 16:42
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 11:34
[class] 404 search function code jrobbio MySQL / PHP Forum 6 22-Apr-2003 10:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.