Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,198 members, 7,953,730 topics. Date: Friday, 20 September 2024 at 01:32 AM

Help With This Php Code. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help With This Php Code. (1672 Views)

Pls Help Me With This Php Code / Help On Php Code For A Dice Game / I Need Help With This Php Code For Email And Sms Autoresponse (2) (3) (4)

(1) (Reply) (Go Down)

Help With This Php Code. by okeyxyz(m): 5:41am On Jan 25, 2012
Hi guys, I need help with this php code. i'm trying this simple binary-search code to confirm whether an input number is present in an ordered array, but i'm not getting any feedback from it so far. it's supposed to reply TRUE or FALSE. i donno what's wrong. thx.

<?php 

//sample array
$MyArray =array(12, 13, 17, 21, 24, 35, 37, 42, 49, 50, 53, 60);

//Define search function
function confirm_value(& $Array, $Value){
$Value1 =$Value;        //value to check for
$High =count($Array) -1;                //last array index
$Low =0;        //first array index
$Index;        //selected index


while($Low <= $High){
$Index =(int)($High + $Low)/2;

if ($Array[$Index] ==$Value1){
return TRUE;
}
else if ($Array[$Index] <$Value1){
$High =$Index -1;
}
else $High =$Index +1;

}
return FALSE;
}

//try search function
echo confirm_value($MyArray, 49);


sorry guys, i tried posting with the site code button but the fonts r too small. so i'm reposting with the quote button.

<?php
//sample array
$MyArray =array(12, 13, 17, 21, 24, 35, 37, 42, 49, 50, 53, 60);

//Define search function
function confirm_value(& $Array, $Value){
$Value1 =$Value;       //value to check for
$High =count($Array) -1;               //last array index
$Low =0;       //first array index
$Index;       //selected index


while($Low <= $High){
$Index =(int)($High + $Low)/2;

if ($Array[$Index] ==$Value1){
return TRUE;
}
else if ($Array[$Index] <$Value1){
$High =$Index -1;
}
else $High =$Index +1;

}
return FALSE;
}

//try search function
echo confirm_value($MyArray, 49);
Re: Help With This Php Code. by Nobody: 8:39am On Jan 25, 2012
Re: Help With This Php Code. by okeyxyz(m): 9:17am On Jan 25, 2012
thanx man!! i see my mistake now. My search was moving away from $Value1 instead of towards it. cheers.
Re: Help With This Php Code. by dellnet: 10:03am On Jan 25, 2012
Ah arrays, every student's nightmare. I will advice you when dealing with arrays to avoid interfering with the index for example $High = count($Array) - 1;

-1 can lead to logic errors later in your program.
Re: Help With This Php Code. by blackrules1(m): 8:08am On Feb 02, 2012
@ Omo t' o dun.

Kindly advice me on how to get a C++ compiler. I need it pls.
Re: Help With This Php Code. by Nobody: 6:54am On Feb 05, 2012
Ah well, there be many compilers, but i kinda prefer "codeblocks" - u might want to look it up in google.
But wait, i think alhaji omo_to_dun uses a different compiler o.

(1) (Reply)

Simple Text-To-Speech Program using Vbscript. Do It Yourself. / I Need A Blog / MATLAB Enthusiasts Come In.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 11
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.