Advertisement

is c++ really a must?

Started by June 19, 2013 07:36 PM
51 comments, last by Amadeus H 11 years, 7 months ago

If you want to be a good programmer you should know more than one language, here is why:

If we take Java for example, can one of the C++ programmers here write a code snippet in Java to create a string containing the numbers 1 to 10.000 separated by commas ?

Whenever some one who knows C++ tries another language, they always seem to create overcomplicated spaghetti code, which I end up having to rewrite.


#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void main(){
    string xx = "";
    string yy = "";
    for( int x = 0; x < 10001; x+= 1 ){
    stringstream x1;
    x1 << x;
    xx+= x1.str() + ", ";
    }
    cout << xx;
    cin >> yy;
}

public class Main {
	public static void main(String[] args){
	String xx = "";
	for (int x = 0; x < 10001;x++){
		xx+= x + ", ";
	}
	System.out.println(xx);
}}

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Advertisement

I don't know much Java, but I do know you're starting at 0 instead of 1, and you should be using StringBuilder because every time you append to the String it makes a new copy. ;)

EDIT: Not happy about the trailing comma at the end either. At least it prints the same incorrect thing in both implementations. main returns an int in C++ as well (although you don't actually need to return one if you don't want).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley


I don't know much Java, but I do know you're starting at 0 instead of 1, and you should be using StringBuilder because every time you append to the String it makes a new copy. ;)

This is one of the reasons why I think seasoned C++ programmers are better engineers. Managed languages like Java allow this to happen. It's better to use StringBuilder, but there is no penalty for using the plus signs. Novice programmers therefore think it's okay to do so, and continue to do so throughout their career. I have seen this happening not just while back in college, but in professional workplaces.

The consequences in C++ for doing things wrong are much dire. While there are good Java programmers, the incentive to become one is much lower due to how accommodating these languages can be.

If we take Java for example, can one of the C++ programmers here write a code snippet in Java to create a string containing the numbers 1 to 10.000 separated by commas ?

Whenever some one who knows C++ tries another language, they always seem to create overcomplicated spaghetti code, which I end up having to rewrite.


#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void main(){
    string xx = "";
    string yy = "";
    for( int x = 0; x < 10001; x+= 1 ){
    stringstream x1;
    x1 << x;
    xx+= x1.str() + ", ";
    }
    cout << xx;
    cin >> yy;
}

public class Main {
	public static void main(String[] args){
	String xx = "";
	for (int x = 0; x < 10001;x++){
		xx+= x + ", ";
	}
	System.out.println(xx);
}}

As i expected :)

That works, but it is extremely inefficient(slow), Strings in Java are immutable so each time you try to modify the string (using += for example) it creates a new string object containing the concatenated string and replaces the old reference leaving the old string as garbage for the GC to clean up (not good).

The proper way to do it would be (a bit closer to the C++ way really since i was silly enough to use integers in my example).

StringBuilder sb = new StringBuilder(50000); //preallocate enough room for 10k 5 character string (its only a few hundred bytes more than we need and removes any need for further allocations)

for(int i=1;i<10000;i++) {

sb.append(i);

sb.append(",");

}

String s = sb.toString();

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

You got your own problem wrong as well :(

I take it your StringBuilder stuff is correct


int i; // I assume you can do this without an initialisation in Java. Needs to stay alive after the loop has finished anyway

for(i=1;i<10000;i++) {
    sb.append(i);
    sb.append(",");
}

sb.append(i); // remember to add 10000 to the end, and no comma afterwards

String s = sb.toString();

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Advertisement

You got your own problem wrong as well sad.png

I take it your StringBuilder stuff is correct


int i; // I assume you can do this without an initialisation in Java. Needs to stay alive after the loop has finished anyway

for(i=1;i<10000;i++) {
    sb.append(i);
    sb.append(",");
}

sb.append(i); // remember to add 10000 to the end, and no comma afterwards

String s = sb.toString();

Oops. :P

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

If we take Java for example, can one of the C++ programmers here write a code snippet in Java to create a string containing the numbers 1 to 10.000 separated by commas ?

Whenever some one who knows C++ tries another language, they always seem to create overcomplicated spaghetti code, which I end up having to rewrite.


#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(){
    stringstream ss;
    int i = 0;
    for(i = 0; i < 10000; ++i){
    ss << i << ", ";
    }
    ss << i;
    cout << ss.str();
}

That's how I would have done it. Not sure if that's the optimal way in c++, since I'm self educated.

Well I guess none of you get a callback if that was a pre-interview question.

* Always read the question

* Check your loop boundary conditions, it's a very common mistake to be off by one (both programs had a different off by one error!)

* Watch out for special cases that need to be done before entering or after exiting the loop. And make sure your loop variable is still in scope if you need to do something with it afterwards.

* Always read the question. I can't emphasise this one enough ;)

It doesn't matter if you know C++ or Java inside out if you can't write a program that fulfills the actual (i.e. not imagined) requirements.

EDIT: Attempt 3 was the best I guess. Shame that 0 isn't 1, even for small values of 1.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
I find it interesting that in these situations programmers almost always make the special case last rather than making the special case first. That is here they think "output a comma behind every element but the last" rather than "output a comma before every element but the first".

This topic is closed to new replies.

Advertisement