[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
C++
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 71
Thread images: 6
File: Help.png (131 KB, 849x533) Image search: [Google]
Help.png
131 KB, 849x533
Hey guys I am a freshman level engineering student and I was wondering if anyone could help me out. So this is the very first year of teaching for my Iranian Professor. She is a very kind woman and she definitely cares about her job. That being said...she sucks at teaching C++. The last few years all of the students in the class have passed with A's and B's. This year however, everyone failed the very first exam... I am hear to ask if anyone can help me solve this particular assignment that was given to us for the night. (She has not explained what the output should look like. Let alone the fact that we covered constructors for about 10 minutes and that was it.) Oh, and just out of curiosity, how long should it take to pick up on C++ programming? Sometimes I worry that maybe i'm just an idiot but then I realize the whole class is failing. (None of us have ever even worked with programming.) Thanks in advance
>>
Picking up the basics up to classes and shit shouldn't take more than a couple of weeks at most.

Also, what are you having trouble with?
>>
>>54109651
Post what you've got already and we'll guide you through
We won't give you the entire assignment, however
>>
Main is not a function. It's a method.
>>
>>54109651

I will not help Iranian students until Iran recognizes and respects Israel as the land of the Jewish people. I know that you may not hold the same insane position as the mullahs that run your country, but it is a matter of principle. If Iranians want the benefits of Stanford and other institutions in the US, they have to respect the values we hold in the US, including freedom of religion and respect for human rights.
>>
>>54109651
Just look up online tutorials on how classes work, and remember any functions and constructors will need to be called from main() to demonstrate that the class works. This sort of stuff is fairly easy to teach yourself.
>>
>>54109928
>>>/java/
>>
>>54109651
Do it in Object Pascal just for shits and giggles.
>>
>>54109928
t. pajeet
>>
>>54109928
Incorrect. A method is just a function called on an object, and in C++ main() isn't part of a class; it works the same as it did in C. In Java you could say main() is a method, since the whole program is defined as a class.
>>
>>54109928
Main is a function
>>
>>54109928
POO
>>
>>54109941
main is a function in Java as well.
>>
type Piece = (Int, Int)

up d (x, y) = (x, y + d)
down d (x, y) = (x, y - d)
left d (x, y) = (x - d, y)
right d (x, y) = (x + d, y)

distance (x1, y1) (x2, y2) = abs (x1- x2) + abs (y1 - y2)


Why on earth would you use OOP for such a simple task?
>>
>>54109651
This assignment is very easy if you know what the words mean Dirty Mike. Did you at least attempt it?
>>
>>54110016
because they want to teach OOP not because they actually need the program written you braindead retard
>>
>>54109651

Maybe something like this?

class GamePiece {
private:
int x, y;

public:
GamePiece::GamePiece() {
x=y=0;
}

int GetX() {
return x;
}

int GetY() {
return y;
}

GamePiece::GamePiece(int ax, int ay) {
x=ax;
y=ay;
}

void up() {
y++;
}

void down() {
y--;
}

void right() {
x++;
}

void left() {
x--;
}

float DistanceToPiece(GamePiece otherPiece) {
float difX = (float)(x-otherPiece.GetX());
float difY = (float)(y-otherPiece.GetY());
return sqrt(difX*difX+difY*difY);
}

}

int main() {
GamePiece piece1;
GamePiece piece2=new GamePiece(5, 5);

piece1.right();
piece1.up();
piece2.left();
piece2.down();

cout<<"Piece1 x is "<<piece1.GetX()<<", y is "<<piece1.GetY()<<".\n";
cout<<"Piece2 x is "<<piece2.GetX()<<", y is "<<piece2.GetY()<<".\n";

float pieceDistance = piece1.DistanceToPiece(piece2);
cout<<"Distance between pieces is "<<pieceDistance<<"\n";

return 0;
}


I haven't tested it, so it probably has some syntax error that will prevent it from compiling.
>>
File: Help 1.png (79 KB, 1366x768) Image search: [Google]
Help 1.png
79 KB, 1366x768
>>
>>54110115
Oh yeah, that piece2=new GamePiece(5, 5) should probably just be GamePiece piece2(5, 5);
>>
File: Help 2.png (16 KB, 909x600) Image search: [Google]
Help 2.png
16 KB, 909x600
This is as far as my tutor and I have gotten so far
>>
>>54110153
So what are you having problems with? What are you going to do next?
Your distance function is supposed to accept another piece as an argument, and calculate the distance from it.
See my full example in >>54110115
>>
To be honest, it's refreshing seeing some of you actually helping OP out.

You surprise me sometimes /g/
>>
>>54110198
We usually help people willing to do shit, the problem is people who just come and want us to do their homework for them
>>
>>54110141
>>54110153
You've already done almost everything except the distance bit like >>54110178 said so not sure what the difficulty is. Although your choice of calling the game piece class gameBoard is a bit misleading, and it's not clear why you've put the constructor definitions outside of the class below the main function. Although both those are formatting complaints, but bad formatting can make things seem more complicated than they are.

You're also missing the testing of the default constructor, the x and y get methods, the movement methods, which will probably lose you points since they're specifically requested.
>>
we reditors solve your homework here post em!
>>
>>54110153
Also you can't use ^ for powers in c++, you have to call pow
>>
>>54110198
are you telling me you didn't report him? gtfo
>>
>>54110297
>Calling pow to square a variable
>>
>>54110334
Welcome to /g/
>>
>>54110334
>>54110354
Yep, /g/ proving their ability by getting hung up on trivialities because they don't know enough to discuss real problems. Sure he could multiply the variable by itself if he wants to save those sweet sweet microseconds on an introductory homework assignment, but his current method won't work.

Don't you have some piss-easy interview question to "optimize"?
>>
>tutor in intro to cpp

confirmed for not gonna make it
>>
File: 1436499512876.png (28 KB, 243x243) Image search: [Google]
1436499512876.png
28 KB, 243x243
>>54110443
prekesh pls
>>
>>54110443
I finished those earlier.
>>
>>54110461
>>54110462
Post your work you smug fucks
>>
File: 0e1.png (229 KB, 500x357) Image search: [Google]
0e1.png
229 KB, 500x357
>>54110115
>capitalized class and method names
>x = ax instead of this.x = x
>post-increment
>casting to float before it's required

j/k i'm just being a nitpicky faggot good job
except the capitalizing of the method/class names, that brings out the REEEEE in me
>>
>>54110492
what did capitalized class names do to you?
>>
>>54110492
Thanks for the feedback, actually, despite the triviality of the program.
Good point about the float casts, I realized after I posted that they were unnecessary.
>>
>>54110525
Got me some minus points on an intro midterm for C++ after I spent a year learning Java.

Almost brought me down a letter grade

But they don't even capitalize method names in Java if I remember correctly
>>
>>54110547
Really? You lost points on a midterm for capitalizing class method names? I've never heard of there being a widespread convention against doing so.
Did the teacher give out an explicit style guide?
>>
>>54109928
Congratulations, you failed.
>>
>>54109928
wew lad
>>
>>54110569
Yeah, but at the time I was still actively messing around with Java (and I still do occasionally). Then I learned when I got a job not months afterwards that generally nobody gives a fuck. The code I see that some of my peers write there triggers me in ways beyond rationality or comprehension; thank God I learned C++ instead of C so I don't have to go through all that shit.
>>
Well /g/ I've gotten it to compile for the most part. Can't say the replies were very encouraging to my future. I've always been a straight A student and I've never struggled with a class more than I have with this class. She just assigns shit, spend maybe 5 minutes going over it, no examples, just explaining (in a barely audible, yet incoherent accent). Doesn't show you what the output should look like. I mean, I don't think i'm an idiot....at least I hope not. Almost 90% of the class was failing at the beginning of the semester so i'm really hoping it's just her poor teaching skills. She's a first year teacher
>>
>>54110652
I had a first year CS professor too once anon, don't get disheartened. If you're an engineering student I'm sure you have a penchant for math, so focus on that. Multivariable/vector calc gets pretty fucking hairy. Feel free to ask for help here again, I personally like helping anons with their programming homework.
>>
>>54110652
I hope you addressed the facts that:
1. Your distance function needs to accept another game piece as an argument, and calculate the distance from that.
2. You need to test all the functions in main, including both constructors.
>>
>>54110153
>Those variable names

You're in the advanced course aren't you?
>>
>>54110652
The specifications don't mention any kind of output at all, so I don't see why you would need to worry about it.
You could even do all the tests with a few asserts, and have the program run without any user input or output.
>>
>>54110652
Engineering student here, my first year programming professor was a complete dumbass too.

It got to the point that I got a higher level than her just from learning by myself in the library.
>>
>>54110719
>>54110677
Just out of curiosity, do you guys have any idea what the average IQ of engineering students is?
>>
>>54110153
Few things anon

1) cin >> x1,y1 does not work. You'll have to do
cin >> x1; cin >> y1;
2) name non-constant variables with the first word always uncapitalized, capitalize from the second word onward. (so x instead of X, or find something else. Also getY getX instead of gety).
3) You need to pass x1 and y1 through the gameBoard class. The method should be like double getDistance(gameBoard game2). You can get x1 and y1 by game2.getY() and game2.getX().
4) You need to define sqrt or use the one in math.h
5) ^ does not work for exponents. Use either pow or (preferably) just multiply the number by itself.
6) cin >> x >> y is generally not good practice.
7) You're going to need two gameBoard objects in main, one called through the no-arg constructor.
8) distance should be a method of the objects. You should be able to do game.distance(game2), game2 being your second gameBoard object.
9) small and autistic, but << '\n' is faster than endl.
10) You can use keyword "this" instead of using confusing variables like v and z. For example, resignature the method gameBoard::gameBoard(int x, int y) and use this.x = x and this.y = y;

It helps to write code as clearly as humanly possible the first time through. It's a better teacher than anything. Name variables after what they are, name methods after what they do, and try not to use weird things like ints x, X, and x_1.

It's rough having a bad teacher. Feel free to come back to /g/ for help, some of us are always eager to help.
>>
>>54110652
hey no problem bro just ask in reddit next time so I can get a thousand upvotes
>>
>>54110739
What are you getting at anon, i'm a CE student
>>
>>54110884
Why camelCase? Who advocates this?
>>
>>54110913
>CaMeLcAsE
LOL
>>
>>54110913
That's just how I was taught at UCI. The point isn't what you capitalize exactly, it's just that you keep things uniform, and that's the easiest way for me.
>>
>>54110913
What's wrong with camelCase?
>>
>>54111258
It's alright, but I'm just curious why anon is suggesting its use. The recommendation stands out in a comment with mostly objective advice.
>>
>>54111300
Not that guy, but it's good to teach newbies some proper styling so they don't write shit code later on.
>>
#include <iostream>
#include <math.h>

class Piece {
int x, y;

public:
Piece(): x(0), y(0) { };
Piece(int, int);
int getX() { return x; }
int getY() { return y; }
void moveUp() { ++y; }
void moveDown() { --y; }
void moveLeft() { --x; }
void moveRight() { ++x; }
int getDistance(Piece);
};

Piece::Piece(int X, int Y)
{
this.x = X;
this.y = Y;
}

int Piece::getDistance(Piece other)
{
return sqrt(pow(x - other.getX(), 2) + pow(y - other.getY(), 2));
}

int main(int argc, char *argv[])
{
Piece a, b(4, 4), c(5, 5);

std::cout << "Piece a:\n";
std::cout << "x: " << a.getX() << " y: " << a.getY() << "\n";
std::cout << "Piece b:\n";
std::cout << "x: " << b.getX() << " y: " << b.getY() << "\n";
std::cout << "Piece c:\n";
std::cout << "x: " << c.getX() << " y: " << c.getY() << "\n";

std::cout << "Moving a up and to the right.\n";
a.moveUp();
a.moveRight();

std::cout << "Moving b to the left and down.\n";
b.moveLeft();
b.moveDown();

std::cout << "Moving c to the left and down.\n";
c.moveLeft();
c.moveDown();

std::cout << "Piece a:\n";
std::cout << "x: " << a.getX() << " y: " << a.getY() << "\n";
std::cout << "Piece b:\n";
std::cout << "x: " << b.getX() << " y: " << b.getY() << "\n";
std::cout << "Piece c:\n";
std::cout << "x: " << c.getX() << " y: " << c.getY() << "\n";

std::cout << "Distance between a and b: " << a.getDistance(b) << "\n";
std::cout << "Distance between b and c: " << b.getDistance(c) << "\n";
std::cout << "Distance between a and c: " << c.getDistance(a) << "\n";

return 0;
}
>>
>>54113360
>int X
>capital X
>>
File: 200px-Jidf_logo.png (54 KB, 200x200) Image search: [Google]
200px-Jidf_logo.png
54 KB, 200x200
>>54109932
Right on, brother. He should respect our Democratic, Peaceful Nation of Israel and her people.
>>
>>54109932
According to american philosophy racism against jews doesn't matter

Prejudice + position of power over = racism
Jews are equals and betters to whites and betters to everyone else so nobody can be racist against jews except jews

Antisemitism don't real
>>
>>54111258
I personally don't like it.

I prefer camel_case, or aCamelCase.
>>
Your homework seems rather trivial. Sorry to hear that your professor can't teach, but the topics you're covering shouldn't take much work to implement.

Assuming you already know the basics of classes, you should know that when you instantiate an object, its constructor is called, and that when the object leaves scope. If you're having trouble with syntax, cppreference is your friend.

http://en.cppreference.com/w/cpp/language/default_constructor
http://en.cppreference.com/w/cpp/language/initializer_list

As for your question
>how long should it take to pick up on C++ programming?

A lifetime. The basics aren't that hard, but once you get into template metaprogramming and other nonsense, you will understand why even Bjarne Stroustrup -- who invented the fucking language -- doesn't consider himself to be a master.

>>54109932
1. OP only stated that their professor was Iranian, not that they themselves are Iranian.
2. Most Iranians in the US are actually pretty decent people. They also have no control over the behavior of their government. Power there is split between the president (elected by the people) and the ayatollah (religious leader -- NOT ELECTED). Guess who has all the power? Not the president.
3. Iran is not the only country that doesn't like Israel. Many Americans hate it either because they sympathize with the Palestinians, or perhaps because they intentionally sank the USS Liberty, claiming they though it was Egyptian. Yes, because Egypt flies the American flag on their ships.
>>
>>54109984
then call it! :^)
>>
>>54109651
Fuck off, Pajeet.
>>
>>54109651
I'm pretty sure many poo in the loos, hues, and other monkeys from the third world will be willing to help you Mike.
>>
>>54110334
>>54110354
>2016
>no #include <boost/math/special_functions/pow.hpp> then call boost::math::pow<2>(base)

I bet you write your own leftpad too.
>>
Hey, i don't want to create a new thread for that, but anyone here know how to display some video from multicast packet?
Thanks in advance if any of you knows.
>>
>>54109651
everyone passed before because you weren't in the class.

guess what, i have that professor for another class, at your uni, you dumb shit. i'm going to report you to administration for academic dishonesty.

the problem with your education is you. you spend all this time playing games, and posting here instead of reading books about how to git gud. go fuck yourself, and drop out. we don't want you in the field.
>>
>>54113601
op is in australia
Thread replies: 71
Thread images: 6

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.