Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,081 members, 7,957,044 topics. Date: Tuesday, 24 September 2024 at 05:44 AM |
Nairaland Forum / Science/Technology / Programming / Fix This C++ Code If You can (1819 Views)
Who Can Fix This Error In My Blog / Please Guys Help Me With This C++ Program Exam Question. It's Urgent. / Help Fix This Android Studio Error Dhtml18 And Otherd (2) (3) (4)
Fix This C++ Code If You can by Olyboy16(m): 11:29pm On Jul 03, 2017 |
Hi guys, so i was just playing around with c++, trying to get back on track after a long long time away into Generic GC langs. As a polyglot programmer, its my duty to stay up to date with all my languages. So , here's the dig. I was playing with algorithms...binary search to be exact. so, i encountered some issues along the way. which actually gave me some serious debug time! So i thought, why not share the fun....i'm throwing out this challenge to anyone who thinks he's as good a C++ programmer as he/she thinks, to fix this code snippet...don't write your implementation...we all can do that...just /fix?optimize/
Please note that i intentionally designed it to be a recursive solution...its just to pass time really. |
Re: Fix This C++ Code If You can by WhiZTiM(m): 10:50pm On Jul 04, 2017 |
Olyboy16: ... Unfortunately, I don't think Your code is not salvageable... By any streak of luck :-( ... It would require a significant change hence rewrite Comments reserved on the code below...
Template Argument Deduction will not work here because none of the arguments depends on the template parameters. Even without Template Argument Deduction, I don't see any use of type `T` in the function-body below template<typename T> Umm, I don't see any #include <string> up there... Seriously? This function returns a `std::string` Oh boy....int size = hay->size(); Whaaaat?? For what? uninitialized pointer?? Even if `hay` was valid, the code below would invoke Undefined Behavior wanted to use std::copy_backward instead?
Below, doesn't make any sense... We are supposedly recursing with the exact parameters this function was called with?
Below, We are dealing with built in types here, they have bool operator == (T x, T y) defined for them by the compiler!! BTW `std::find` takes a ValueComparable type as the third argument... since you are using some sort of Unary Predicate, wanted to use `std::find_if` instead?
What?
What kind of nonsense is the declaration of arr? std:: containers should be used as value types... A pointer to any std:: container is more like code smell... ..... Really? Any need to invent your functor here? std::sort uses std::less<T> by default, if you want in descending, why not simply use std::greater<int>? ..sort(arr->begin(), arr->end(), [](int& i, int& j){ 1 Like |
Re: Fix This C++ Code If You can by WhiZTiM(m): 11:14pm On Jul 04, 2017 |
Olyboy16:Is that not Dr. Andrei Alexandrescu on your profile? ...The template goon... I picked up D lang cause of that guy.... |
Re: Fix This C++ Code If You can by Olyboy16(m): 12:56am On Jul 05, 2017 |
WhiZTiM: Hey, calm down big guy! I wrote that code after over 2 years away from c++. Also, that's not the actual working code, it was intentionally left buggy and dirty. Obviously carelessly written too. Now down to the comments. Clang and Mingw both support implicit <string> headers in their <iostream> header; and. ..just ignore those issues. it wasn't even a working code anyway, just part of a c++ rehab process(playing around with containers and lambdas). Now just FTR, here is the actual code i wrote.
just for PoC really. the result's half baked |
Re: Fix This C++ Code If You can by Olyboy16(m): 2:22am On Jul 05, 2017 |
WhiZTiM: Yes sir, that's the man! He's one of the guys i really wish to work with. D is just mehn.. I wonder why folks aren't picking it up much...such a great language |
Re: Fix This C++ Code If You can by WhiZTiM(m): 10:55pm On Jul 07, 2017 |
Hey, calm down big guy! I wrote that code after over 2 years away from c++.I am calm... very calm... But look at the title of your post, "Fix This C++ Code If You Can (dare)!". Even if it worked, it would have been an Implementation Defined Behavior, according to ISO C++ standards... ...lol on the rehab process... Goodluck with it... Now just FTR, here is the actual code i wrote. The code is very very buggy... And the interface is ... comments reserved Bug spotted...int main(){
...lol. I know, like I said, I am only responding based on the title of your post. Maybe the title of your post betrays your original intent... I dunno... 1 Like |
Re: Fix This C++ Code If You can by WhiZTiM(m): 11:15pm On Jul 07, 2017 |
Olyboy16:Yea... you should check the work he has been pushing out... "Design by Introspection". BTW, I like the design of your blog. Spent sometime on it. Glad to learn about the Python library "ShutIt". Great job man! |
Re: Fix This C++ Code If You can by sharrp: 4:29am On Jul 08, 2017 |
Olyboy16:What happens when the pin is lesser than than the lenght of the array/2 . And why did you resort to use an ordinary sort rather than implementing the binary search on the second half |
Re: Fix This C++ Code If You can by Olyboy16(m): 8:52am On Jul 08, 2017 |
WhiZTiM:wao thanks man! i've been discouraged recently to make post cos i dont get any feedbacks, but you just gave me new inspiration! More Posts On The Way. andrei's design by introspection is great, gave me some think time. though i feel like people should have known these law before hand. BTW, pls dont reserve your comments on the bugs on my code, i love being corrected, makes me smarter. i have more codes to post if you dont mind, i deal in AI and i'm trying to create a custom AI-ML. so i've got some pretty interesting code snippets we can brainstorm on. |
Re: Fix This C++ Code If You can by Olyboy16(m): 8:54am On Jul 08, 2017 |
sharrp:Hi Sir, please find the correct code on the thread. that code is intentionally buggy. |
Re: Fix This C++ Code If You can by WhiZTiM(m): 4:20pm On Jul 12, 2017 |
wao thanks man! i've been discouraged recently to make post cos i dont get any feedbacks, but you just gave me new inspiration! More Posts On The Way.Ok... I will be checking...! BTW, pls dont reserve your comments on the bugs on my code, i love being corrected, makes me smarter. i have more codes to post if you dont mind, i deal in AI and i'm trying to create a custom AI-ML. so i've got some pretty interesting code snippets we can brainstorm on Ok.... Its a bit Mathematical... C++ treats its STL comparators with the concept of Strict Weak Ordering. This makes it possible to use a single comparator for A variety of things. For example. Using the strict Weak Order of < (less than operator). We can establish the following: - A < B; A is less than B - B < A; B is less than A - !(A < B); A is not less than B. Hence, A is greater than or equal to B - !(B < A); B is not less than A. Hence, B is greater than or equal to A - !(A < B) && !(B < A); A is neither less than B nor vice-versa, Hence A is equall to B -------- See how we were able to use one single operator to Cater for others? - A < B; equivalent to: A < B ; B > A - B < A; equivalent to: B < A ; A > B - !(A < B); equivalent to: A >= B ; B <= A - !(B < A); equivalent to: B >= A ; A <= B - !(A < B) && !(B < A); equivalent to: A == B With that knowledge, we can write a generic version of out Binary Search Algorithm. That takes a functor and makes comparison. Recall that the C++'s STL iterator concept uses the notion of One element past the end. Basically, the iterator produced for a range starts out with a closed interval and ends with an Open Interval. Hence, a range is defined as: [A, B) - The first element, A is inclusive; the last element, B is exclusive Armed with the above, we could write an iterative version of `binary_search`. Which has similar API with STL's version, but we return an iterator instead of a bool as per C++ STL version.: namespace tim{ Example Usage: int main(){ I haven't run serious tests for the above code though, so, I am still paranoid that there may be a bug lurking somewhere... ...lol Don't mind me... I write libraries, so I am very keen about performance, API intuitiveness and "being generic"... I also answer questions on Stackoverflow... |
Re: Fix This C++ Code If You can by Olyboy16(m): 11:17pm On Jul 12, 2017 |
@whiztim , great job on the code! i see you are a regular c++ customer. . . . you still dint point out any bug in my own snippet! your code only leverages on mathematical grounds as opposed to my artistic approach. anyway, its great to know i can still find someone that actually writes standard c++ codes on nairaland; i guess i'm a bit rusty on the C++ STL(temporarily). . oh and tim, i've checked your github repo. man the 'Value' class is really great! i tried something like that with a mix of c & c++ few years ago, the results were awesome, but not even close to what you've done! good job man, i'll try nd find the header code later to analyze the concept! 1 Like |
Re: Fix This C++ Code If You can by KazukiIto(m): 10:08pm On Jul 13, 2017 |
*picks up C++ textbook* |
(1) (Reply)
Today is good / Browse For Free Without Limit / My Funny Reality How I Became A Programmer and Started Earning
(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. 71 |