Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,155,625 members, 7,827,334 topics. Date: Tuesday, 14 May 2024 at 10:42 AM

Help Me Keep My Brain In My Skull! Solve This Logic! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help Me Keep My Brain In My Skull! Solve This Logic! (1926 Views)

How To Code This Logic With Php... / I Need A Smart Programmer To Solve This Logic? / See The Javascript Code That Is Turning My Brain (2) (3) (4)

(1) (Reply) (Go Down)

Help Me Keep My Brain In My Skull! Solve This Logic! by uvalued(m): 5:26pm On Mar 27, 2016
Happy easter to you all.

I have beeen racking my brain for hours to solve this logic in php but with no succes in site and tried googling it was not clear. Here it goes.

I have this serial values 1,2,3,4,5,6,7,8,9,10 as id in my database. Using SELECT statement to call the values was ok. But here is my problem, I want 1 to be added to 1 from the database, 1 subtracted from 2 from the database, 1 added to 3 from the database, 1 subtracted from 4 from the database etc. Each operation should be added to an array which will be called later using for loop.

I thought of using array, if then else, i just cant rap my fingers around it, can someone help cry cry cry
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by Nobody: 5:39pm On Mar 27, 2016
You rack your brain not crack...... Continue with it... You'll finally get it.... Some of your forefathers in whatever you're doing did not give up.... Me I don't understand what is there tongue..

4 Likes

Re: Help Me Keep My Brain In My Skull! Solve This Logic! by codemarshal08(m): 5:56pm On Mar 27, 2016
uvalued:
Happy easter to you all.

I have beeen cracking my brain for hours to solve this logic in php but with no succes in site and tried googling it was not clear. Here it goes.

I have this serial values 1,2,3,4,5,6,7,8,9,10 as id in my database. Using SELECT statement to call the values was ok. But here is my problem, I want 1 to be added to 1 from the database, 1 subtracted from 2 from the database, 1 added to 3 from the database, 1 subtracted from 4 from the database etc. Each operation should be added to an array which will be called later using for loop.

I thought of using array, if then else, i just cant rap my fingers around it, can someone help cry cry cry

Can you post the codes you have tried here ?
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by codemaniacs: 6:05pm On Mar 27, 2016
Go to phpmyadmin then edit your ID table to enable "auto increment" and click the check box and make the index "primary". No need to code it
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by codemarshal08(m): 6:07pm On Mar 27, 2016
codemaniacs:
Go to phpmyadmin then edit your ID table to "auto increment" and click the check box and make it "primary". No need to code it
This obviously wont take care of the subtraction case.
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by uvalued(m): 6:07pm On Mar 27, 2016
codemarshal08:


Can you post the codes you have tried here ?

$QuerySelects = "SELECT * FROM entry";
$array = array();
$Query = mysql_query($QuerySelects) or die (mysql_errno() . ": " . mysql_error(). "\n"wink;
while($result=mysql_fetch_assoc($Query)) {

$k = $k +1;
$array[] = $result["id"];
$i = $i +1;
if($i == 2)
{
print_r($array);

}
}
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by uvalued(m): 6:09pm On Mar 27, 2016
codemaniacs:
Go to phpmyadmin then edit your ID table to enable "auto increment" and click the check box and make the index "primary". No need to code it

It already auto_incremented ie the ID
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by uvalued(m): 6:11pm On Mar 27, 2016
airmirthd1:
You rack your brain not crack...... Continue with it... You'll finally get it.... Some of your forefathers in whatever you're doing did not give up.... Me I don't understand what is there tongue..

Thanks at least your comment was a relief by putting a smile on me sha
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by codemarshal08(m): 6:14pm On Mar 27, 2016
uvalued:


$QuerySelects = "SELECT * FROM entry";
$array = array();
$Query = mysql_query($QuerySelects) or die (mysql_errno() . ": " . mysql_error(). "\n"wink;
while($result=mysql_fetch_assoc($Query)) {

$k = $k +1;
$array[] = $result["id"];
$i = $i +1;
if($i == 2)
{
print_r($array);

}
}

Check the below tip:
--------------------------
From the problem statement, i could recognize a pattern
values ==> 1,2,3,4,5,6,7,8,9,10
even numbers ==> 2, 4, 6, 8, 10
odd numbers ==> 1, 3, 5, 7, 9

//make a function to determine if a number is even or odd
// then make the below comparison
if the value i received from DB is an even number
add 1 to it
else
subtract 1 from it

1 Like

Re: Help Me Keep My Brain In My Skull! Solve This Logic! by uvalued(m): 6:36pm On Mar 27, 2016
codemarshal08:


Check the below tip:
--------------------------
From the problem statement, i could recognize a pattern
values ==> 1,2,3,4,5,6,7,8,9,10
even numbers ==> 2, 4, 6, 8, 10
odd numbers ==> 1, 3, 5, 7, 9

//make a function to determine if a number is even or odd
// then make the below comparison
if the value i received from DB is an even number
add 1 to it
else
subtract 1 from it

Yes you are right, l will add that to my codes. By the way, someone suggested this code and it worked and tot i share it here for others to benefit.

$val + (($val & 1)<<1) -1 adds 1 to odd numbers and substracts 1 from even numbers.
https://stackoverflow.com/users/3741589/quasimodos-clone
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by bigdrey(m): 12:53am On Mar 29, 2016
I don't really get u very well, but with the little i got, here's is aw u can go abt it:

If($k % 2 != 0){
$newK = $k +1:
}else{
$newK = $k -1;
}

You shld get the concept and if that doesn't solve all you need, try clarifying ursef more.

2 Likes

Re: Help Me Keep My Brain In My Skull! Solve This Logic! by paranorman(m): 5:31pm On Mar 29, 2016
Oga codemarshall08, how do one check if a number is odd or even or multiple of a particular number or if a number is a factor or so?
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by codemarshal08(m): 8:59pm On Mar 29, 2016
paranorman:
Oga codemarshall08, how do one check if a number is odd or even or multiple of a particular number or if a number is a factor or so?

Are you requesting logic or codes ?
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by paranorman(m): 9:56pm On Mar 29, 2016
codemarshal08:


Are you requesting logic or codes ?
both, if possible. do it in PHP
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by mexzony: 12:30am On Mar 30, 2016
Are u a beginning programmer.if so why don't you try to solve it all on ur own even if it takes you a bit of time.
This is because it eventually helps you to develop ability to come up with solutions from scratch and you become smarter in the long run.
Just my opinion though.
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by omicpet(m): 12:15am On Apr 01, 2016
Very well here's a rough idea ,
Do a for loop where if value of array mod 2== 0 subtract 1
And another where array mod 2 == 1 plus 1..

Try get d idea ..
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by mexzony: 8:02am On Apr 01, 2016
omicpet:
Very well here's a rough idea ,
Do a for loop where if value of array mod 2== 0 subtract 1
And another where array mod 2 == 1 plus 1..

Try get d idea ..
Hello is this omic from BCC.
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by omicpet(m): 5:02pm On Apr 01, 2016
mexzony:

Hello is this omic from BCC.

Yeah BCC & BCPS
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by mexzony: 5:09pm On Apr 01, 2016
omicpet:


Yeah BCC & BCPS
Guy how far na.the Boi who refused to deliver my periscope to me.
If you know my identity no reveal am o.
Science class mates.
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by omicpet(m): 5:15pm On Apr 01, 2016
mexzony:

Guy how far na.the Boi who refused to deliver my periscope to me.
If you know my identity no reveal am o.
Science class mates.


Lol oh damn . how ya
Re: Help Me Keep My Brain In My Skull! Solve This Logic! by mexzony: 5:24pm On Apr 01, 2016
omicpet:



Lol oh damn . how ya


Dude send me ur contact at mexzony@gmail.com

(1) (Reply)

Anybody Know How To Install Video Maker Fx? / Learn How To Programme Using Arduino And Raspberry Pi / Kindly Download My First Android Mobile Application

(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. 42
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.