Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

05-05-2015 , 08:39 PM
Quote:
Originally Posted by Somnius
Why is this recursive calls true determination changing to 'undefined' before returning to the parent comparison?

Code:
function deepEqual(a, b) {
  // value comparison
  if (a === b) { return true };
  
  var propaCount = 0;
  var propbCount = 0;
  
  // object comparison
  if (typeof(a) == 'object' && typeof(b) == 'object' && a != null && b != null) {
    // count the properties in object a
    for (var prop in a) {
      propaCount += 1;
    }
    // starting comparing properties of b in a and recursively comparing the values
    for (var prop in b) {
      propbCount += 1;    
      if (!(prop in a) || !deepEqual(a[prop], b[prop])) {
        return false;  
      }
    }
  // if the object test fails
  } else {
    return false;
  }
  // to get it to work I have to include return true; here however don't understand why the recursive determination of true using the comparison at top of function changes to 'undefined' when returning back to the comparison initiating the call (using pythontutor)
}
any help appreciated please.
You have two objects with subproperties, and all are equal. It won't return true at the top because they are not identical (===). So it falls through. No problem so far. So you take all the cases where you can rule out them being equal, and return false. So far so good. Now it falls all the way to the bottom, and there is no return value. But you already checked for all the bad cases, so you must return true.

Walk through it with a simple case of a = {id:1}, b = {id:1}.

This is also why javascript is a ****ty language - you can have functions that don't always return values, and it actually runs.
Programming homework and newbie help thread Quote
05-05-2015 , 08:42 PM
Quote:
Originally Posted by plx
Thanks guys. I have another question. How do i make a BinarySearch function to search min number in array? I have the code below. I know its buggy with iMin but i've been messing around and i can't find an efficient way to solve it. How would you guys do it?

Code:
#include <algorithm>

int MinBinarySearch(int Elem, int v[], int start, int end)
{
    // Calls to this function should do this inside an exception handler 
    // because this could definitely throw an exception. Of course your
    // solution can throw an exception too.
    return *std::min_element((v+start), (v+end+1));
}
I think this will work. I suppose your assignment is to write a function to do a binary search so you probably can't use this.
Programming homework and newbie help thread Quote
05-05-2015 , 09:45 PM
Quote:
Originally Posted by plx
Thanks guys. I have another question. How do i make a BinarySearch function to search min number in array? I have the code below. I know its buggy with iMin but i've been messing around and i can't find an efficient way to solve it. How would you guys do it?

Code:
int MinBinarySearch(int Elem, int v[], int start, int end)
{
    int middle, iMin=-1;
    if((start>end) && iMin==-1)
        return -1;
    middle=(start+end)/2;
    if(Elem==v[meio])
    {
        iMin=middle;
        return MinBinarySearch(Elem,v,start,middle-1);
    }
    if(Elem<v[middle])
        return MinBinarySearch(Elem,v, start, middle-1);
    else
        return MinBinarySearch(Elem,v,middle+1,end);
    return iMin;
}
What is the point of a binary search when you need to cover every value? Are you missing some requirement, like the list being sorted or something?

What does Elem represent here? What is meio?

I'm not sure what this code is doing or what the goal is.
Programming homework and newbie help thread Quote
05-05-2015 , 09:46 PM
Quote:
Originally Posted by TomCollins
You have two objects with subproperties, and all are equal. It won't return true at the top because they are not identical (===). So it falls through. No problem so far. So you take all the cases where you can rule out them being equal, and return false. So far so good. Now it falls all the way to the bottom, and there is no return value. But you already checked for all the bad cases, so you must return true.

Walk through it with a simple case of a = {id:1}, b = {id:1}.

This is also why javascript is a ****ty language - you can have functions that don't always return values, and it actually runs.
Yes I see it now thanks - for whatever reason was thinking the recursive return of true should carry through however it's just returning to the comparison that called it - passing all the tests of not being false and ofc we need to return something at the end.
Programming homework and newbie help thread Quote
05-06-2015 , 06:21 AM
Quote:
Originally Posted by TomCollins
What is the point of a binary search when you need to cover every value? Are you missing some requirement, like the list being sorted or something?

What does Elem represent here? What is meio?

I'm not sure what this code is doing or what the goal is.
Sorry meio was a typo i was converting from my language but i forgot it in there. The goal of the function is to search a number but return only the first one in the array. Regular binary search returns first number that it finds.

I did this modification and i thought it would work but strangely it keeps giving me -1 even though it's not meeting the condition start >end && iMin==-1 or at least by my thought process. Is iMin still being overwritten with this code? What is it that i'am missing here?

Code:
#include <stdio.h>

int MinBinarySearch(int Elem, int v[], int start, int end)
{
    int iMin=-1;
    do
    {
    if((start>end)&&(iMin==-1))
        return -1;
    int middle=(start+end)/2;
    if(Elem==v[middle])
    {
        iMin=middle;
        return MinBinarySearch(Elem,v,start,middle-1);
    }
    if(Elem<v[middle])
        return MinBinarySearch(Elem,v, start, middle-1);
    else
        return MinBinarySearch(Elem,v,middle+1,end);
        
    }while(start<=end);
    return iMin;
}


int main()
{
    int A[7]={2,4,10,10,10,18,20};
    int res= MinBinarySearch(10, A, 0, 6);
    printf("%d\n", res);




}
Programming homework and newbie help thread Quote
05-06-2015 , 09:17 AM
Quote:
Originally Posted by plx
Sorry meio was a typo i was converting from my language but i forgot it in there. The goal of the function is to search a number but return only the first one in the array. Regular binary search returns first number that it finds.

I did this modification and i thought it would work but strangely it keeps giving me -1 even though it's not meeting the condition start >end && iMin==-1 or at least by my thought process. Is iMin still being overwritten with this code? What is it that i'am missing here?

Code:
#include <stdio.h>I

int MinBinarySearch(int Elem, int v[], int start, int end)
{
    int iMin=-1;
    do
    {
    if((start>end)&&(iMin==-1))
        return -1;
    int middle=(start+end)/2;
    if(Elem==v[middle])
    {
        iMin=middle;
        return MinBinarySearch(Elem,v,start,middle-1);
    }
    if(Elem<v[middle])
        return MinBinarySearch(Elem,v, start, middle-1);
    else
        return MinBinarySearch(Elem,v,middle+1,end);
        
    }while(start<=end);
    return iMin;
}


int main()
{
    int A[7]={2,4,10,10,10,18,20};
    int res= MinBinarySearch(10, A, 0, 6);
    printf("%d\n", res);




}
A little different problem than you stated originally. Obviously "unwinding" the stack is not doing what you think it does. Just step through the code with a debugger and you will see where the problem is. If I was going to tell you what is wrong with the code that is what I would do. Is this program running on Linux or Windows? If Linux I would just make it an Eclipse project and use the source level debugging interface, if Windows VS source level debugging works fine.
Programming homework and newbie help thread Quote
05-06-2015 , 09:33 AM
I run Dev C++. how do i debug? I never did that before. Just hit debug? It just gives me this:

->->pre-prompt
(gdb)
->->prompt

->->post-prompt
Reading symbols from C:\Users\PC\Documents\Untitled1.exe...done.

->->pre-prompt
(gdb)
->->prompt
Programming homework and newbie help thread Quote
05-06-2015 , 09:50 AM
Quote:
Originally Posted by plx
I run Dev C++. how do i debug? I never did that before. Just hit debug? It just gives me this:

->->pre-prompt
(gdb)
->->prompt

->->post-prompt
Reading symbols from C:\Users\PC\Documents\Untitled1.exe...done.

->->pre-prompt
(gdb)
->->prompt
You will have to learn how to use GDB commands if you go that route which is fine. I suggest that you install Eclipse-CDT (Eclipse with C/C++ plugin) , create a project and use the source level debugging interface. There may be others source level debugging options. Also you could just do this in Windows and use one of the free versions of Visual Studio, create a project, and run your program.

You can put printf statements everywhere too to trace your recursions.


You didn't expect to write C/C++ programs and not use a debugger did you?
Programming homework and newbie help thread Quote
05-06-2015 , 09:59 AM
Yes i did. I hoped everything would easily work without much hassle. Just tell computer to do something and it does it! :P
Programming homework and newbie help thread Quote
05-06-2015 , 10:02 AM
Quote:
Originally Posted by plx
Yes i did. I hoped everything would easily work without much hassle. Just tell computer to do something and it does it! :P
Unrealistic expectation when writing C/C++ programs as you are finding out

Edit: you could also just use std::min_element and then start at beginning of the array to find the first occurrence of it. The iterator returned by min_element points to the first occurance I believe too.

Last edited by adios; 05-06-2015 at 10:32 AM.
Programming homework and newbie help thread Quote
05-06-2015 , 10:45 AM
lol C(++)
Programming homework and newbie help thread Quote
05-06-2015 , 11:01 AM
Quote:
Originally Posted by ChrisV
lol C(++)
When Milleseconds Make Millions
Quote:
My previous article noted that an HFT company, Teza Technologies, offered $1.2 million to a C++ developer named Sergey Aleynikov to leave his $400,000-per-year position at Goldman Sachs and work on its own trading system.
C++ Expertise Pays Great, like hundreds of thousands of $

Thanks for the helpful comments in a thread devoted to helping people with their homework too.
Programming homework and newbie help thread Quote
05-06-2015 , 02:52 PM
Quote:
Originally Posted by adios
Fixed your link. I'll add that expertise in pretty much any technology is worth lots of money and this has nothing do with C++.
Programming homework and newbie help thread Quote
05-06-2015 , 03:11 PM
Isn't sergey the guy going to jail for stealing code?

http://www.bloombergview.com/article...erge-aleynikov
Programming homework and newbie help thread Quote
05-06-2015 , 05:50 PM
Quote:
Originally Posted by candybar
Fixed your link. I'll add that expertise in pretty much any technology is worth lots of money and this has nothing do with C++.
Yep but just pointing out that there is serious money involved. LOL(c++) just struck me as mockery of someone trying to learn it. Could be convinced otherwise.
Programming homework and newbie help thread Quote
05-06-2015 , 08:18 PM
Quote:
Originally Posted by adios
Yep but just pointing out that there is serious money involved. LOL(c++) just struck me as mockery of someone trying to learn it. Could be convinced otherwise.
Agreed - how do they think we got to the high-level languages and infrastructure we have anyway? Every major browser and rendering engine is written in C++, every major Javascript engine is written in C++, most Java runtimes and CLR are written in C++, Windows is written in C++, etc, etc.
Programming homework and newbie help thread Quote
05-06-2015 , 08:28 PM
Quote:
Originally Posted by candybar
Agreed - how do they think we got to the high-level languages and infrastructure we have anyway? Every major browser and rendering engine is written in C++, every major Javascript engine is written in C++, most Java runtimes and CLR are written in C++, Windows is written in C++, etc, etc.
If you are writing an operating system, C++ is a great language. When you need a lot of low level control, it's a great language. Otherwise, not so much. Sure, it helped get where we are, but it has a lot lacking.

Very few applications require that level of control, and readability and maintainability and test-ability of code is often worth the slight performance hit. When milliseconds matter, sure, C++. When developer-hours matter, you'd be a fool to use it.
Programming homework and newbie help thread Quote
05-06-2015 , 08:36 PM
For the foreseeable future, at least like one person has to still know machine & assembly language and all that.

At least, until we tell robots how to do that crap.
Programming homework and newbie help thread Quote
05-06-2015 , 11:46 PM
Quote:
Originally Posted by Anais
For the foreseeable future, at least like one person has to still know machine & assembly language and all that.

At least, until we tell robots how to do that crap.
On assembly language, most of the time to get from the reset vector to finishing setting up the C runtime environment is pretty much it. That is basically all the assembly language you will see in UEFI for instance. Windows has some thunks for interrupt processing written in assembly language. Some small embedded type processor applications are written in assembly language. Compiler global optimization does a better job in large scale OS type work. I've read the compilers that ARM sells do a much better job at optimizing code than GNU does for ARM processors.

Whether or not the problem domains that C++ should be used for is shrinking is hard to say. Microsoft seems to be re-embracing C++ for Windows 10 for instance. Graphics and games utilize C++ a lot. Many people believe that multithreading in applications will become more prevalent because the gains in processor speeds have maxed out for the time being. If you need speed getting "closer to the machine" would seem to indicate C++ has a future. I opined a while back that somehow processors would scale up to accommodate higher level languages. I'm thinking that one way that processors could "scale up" is to have a boatload more registers. More registers means less use of memory needs to be utilized by compilers.
Programming homework and newbie help thread Quote
05-07-2015 , 06:29 AM
Quote:
Originally Posted by adios
Yep but just pointing out that there is serious money involved. LOL(c++) just struck me as mockery of someone trying to learn it. Could be convinced otherwise.
It was like 70% that I was drunk when posting and 30% lol C++ still being taught in university courses (though I don't even know plx is in that category).

C++ has its place for sure though I don't see what salary has to do with it. As candyman pointed out, expertise always pays. COBOL paid a ton around the Y2K era. The highest paid guy at the last job I was at was paid to maintain a system written in some godawful language called 4th Dimension.

Quote:
These days, one of the popular applications of 4D is as a web server. Having a web server and a compiled application running as one gives developers the ability to create efficient, dynamic web sites with a high level of adaptability, and security.
A web server written in a relational database language! That does sound awesome!

Anyway my points are as follows:

1. C++ certainly has its place. In certain niches it's the best choice.
2. The fact that you can make mad bank writing it isn't really relevant.
3. It should not be being taught in introductory courses. I've soapboxed at length before about out of date university courses and won't do it again.
4. Relatedly, I facepalm a bit when I see learners attempt to wrestle needlessly with its shortcomings. But maybe plx knows other languages and is learning C++. More of a general comment than this specific instance.
Programming homework and newbie help thread Quote
05-07-2015 , 08:45 AM
Quote:
Originally Posted by adios
Yep but just pointing out that there is serious money involved. LOL(c++) just struck me as mockery of someone trying to learn it. Could be convinced otherwise.
removing the mockery, his point is valid.

having used many programming languages over many years, it's entirely fair to think that teaching new programmers C++ is pretty insane (and it was my first language). it's just going to teach you so many wrong things, and prevent you from focusing on so many important things. (EDIT: unless your goal is to do low level systems programmings)

it's not "lol" in the sense that it's useless or that there are no jobs for it (although the high paying jobs would be looking for experts, i'd think), it's just like.... teaching a college creative writing course by having students write a technical manual for a specific model of a microwave.
Programming homework and newbie help thread Quote
05-07-2015 , 09:01 AM
As one who is leaving c++ for Java, there's absolutely levels of control that I will miss. But I think I'd ultimately rather start my career out with a language that's a bit more OOP/beginner friendly and worry about learning the advanced stuff once I've got more experience.

I don't need to know how to tear down an engine and rebuild a transmission to be able to get a driver's license, after all.
Programming homework and newbie help thread Quote
05-07-2015 , 09:04 AM
Quote:
Originally Posted by ChrisV
It was like 70% that I was drunk when posting and 30% lol C++ still being taught in university courses (though I don't even know plx is in that category).

C++ has its place for sure though I don't see what salary has to do with it. As candyman pointed out, expertise always pays. COBOL paid a ton around the Y2K era. The highest paid guy at the last job I was at was paid to maintain a system written in some godawful language called 4th Dimension.



A web server written in a relational database language! That does sound awesome!

Anyway my points are as follows:

1. C++ certainly has its place. In certain niches it's the best choice.
2. The fact that you can make mad bank writing it isn't really relevant.
3. It should not be being taught in introductory courses. I've soapboxed at length before about out of date university courses and won't do it again.
4. Relatedly, I facepalm a bit when I see learners attempt to wrestle needlessly with its shortcomings. But maybe plx knows other languages and is learning C++. More of a general comment than this specific instance.
I do study CS and learn C not C++. The compiler i use is dev c++ but works fine with C too. But yeah maybe its not the best language to start i do find my self stuck with technical things alot of the time, maybe learning a softer language would be better to introduce?
Here we start with C and Assembly(much worse ) then we go to java and get deeper on C with data structs and stuff. They are going to replace Assembly with python which is good but they should already have done that!
Programming homework and newbie help thread Quote
05-07-2015 , 09:07 AM
Quote:
Originally Posted by Anais

I don't need to know how to tear down an engine and rebuild a transmission to be able to get a driver's license, after all.
it's even more than that. if you want to be a racecar driver, the skills needed are just completely different than those needed to be a mechanic. so spending a year learning about engines, when your goal is to race, is actually harmful.

what kind of programming do you want to do?
Programming homework and newbie help thread Quote
05-07-2015 , 09:21 AM
Quote:
Originally Posted by gaming_mouse
what kind of programming do you want to do?
not sure I even fully know how to answer that question.

Giz just finished her intro to programming class last night and we had a talk about how she was feeling about programming. Said she sort of had a love/hate relationship with it. Like, she was really glad to be done and to have turned in the final project and all that, but at the same time, she went back immediately and started adding functionality to the project just because she thought of more interesting things to do.

That's exactly what I did at the end of that class as well.

And I think it's sort of a good description of what attracts me to programming. Yeah, there's some projects I've worked on that were really irritating, but if it means I get to sit there and try different ways to accomplish some goal or achieve some functionality, I can hardly think of anything else I'd rather be doing.

But to try to answer your question more directly, I think maybe more business logic/back end-type stuff, or straight up application development. While going through odin project, my least favorite bits have definitely been the front end work. Can do it if it's necessary, but would rather be doing something else.

tl;dr - dunno
Programming homework and newbie help thread Quote

      
m