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

C++ exam subject

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 38
Thread images: 2

File: C.png (5KB, 550x380px) Image search: [Google]
C.png
5KB, 550x380px
Anyone of you know how to do this? I got stuck at the distance thing.
Its c++
>Define a class called Point with x,y int coordinates, private variables of the class.
>Define 2 constructors, one without parameters that initiates with 0 the variables and another that lets the variables be initiated.
>Define a method called Distance that has the parameter refrenced to a point and returns a double value that is the distance between the parameter and the current object.
>Define an array of n<=5 objects with values different from origin of coordinates. Display the points' coordinates and the distance between the first and the other n-1 points.
>For display use own handlers which align to right in a field of 10 charachters, precision 2 decimals for fractional part.
>>
What about it confused you? Just use the distance formula.
>>
>>61174593
sure, all you have to do is aefine a method called Distance that has the parameter refrenced to a point and returns a double value that is the distance between the parameter and the current object.
>>
>>61174870
Did I note this wrong cause it sounds so fucked up and maybe that's what I don't know what to do.
>>
>>61174869
I know you have to use that but I don't know how to do the parameters and objects thing. I'm still learning and watching tutorials on OOP.
>>
>>61174870
double distance(Point other) {
return sqrt((pow((this.x-other.x),2) + pow((this.y-other.y),2)));
}


this can't work however, because 1) says that x and y are private. they need to be public for this to work
>>
>>61174958
Does it matter if it's happening in the same class?
>>
>>61174958
also "Point other" should be "Point & other" because you need to pass a reference
>>
>>61175008
yes, this is an instance method so you're calculating a distance between the object that has the method (coordinates are its fields -- this.x and this.y) and another object passed as a parameter
>>
>>61175009
What do I put in the header file?
public:
Point *other;

And in the point.cpp

double Point::Distance(Point &other){}

Also how do I make that array and assign to an object? Does it have to be random values?
>>
It's worded like shit, but this is still really easy. You need to write accessor function since the coordinates are private data members.
>>
>>61175216
I'm not sure about the exact syntax because I don't do C++ at all. just google it, I guarantee you'll find the answer

>Does it have to be random values?
no. just make up some simple values so that you'll be able to verify the calculations by hand
>>
>>61175308
I write accessor_x and accessor_y which return x and y right? How do I use those functions?
>>
>>61175406
These are public members of the class that will return the values of the private members when called.
>>
>>61175444
Well I need the x and y for the distance formula. How do i implement those accesors so the formula can use x and y.
>>
>>61175548
You can have a separate function for that. The return values of your accessor functions can be used as parameters for your distance function.

The whole point of OOP is to split things into modular pieces.
>>
Okay let's get back to basics. I don't understand what the distance function does. Do i input 2 points on 2 different objects? ob1(5,2) ob2(6,3)?
>>
>>61174958
No, in C++ a class can access the private members of other classes if they are both the same class.
>>
>>61175700
Point p1(5,2);
Point p2(6,3);
p1.Distance(p2);
>>
>>61174593
>>Define a class called Point with x,y int coordinates, private variables of the class.
class Point {
private:
double x;
double y;
};

>>Define 2 constructors, one without parameters that initiates with 0 the variables and another that lets the variables be initiated.
public:
Point(double xx, double yy) : x(xx), y(yy) {}
Point() : Point(0,0) {}

>>Define a method called Distance that has the parameter refrenced to a point and returns a double value that is the distance between the parameter and the current object.
public:
double Distance(Point &other)
{
double dx = x - other.x, dy = y - other.y;
return sqrt(dx*dx+dy*dy);
}

>>Define an array of n<=5 objects with values different from origin of coordinates. Display the points' coordinates and the distance between the first and the other n-1 points.
I would use a vector for this, but whatever...
int n = 5;
Point points {{0,0},{1,2},{3,4},{5,6}};
for(int i = 1; i < n; ++i)
std::cout << points[0].Distance(points[i]) << std::endl;

>>For display use own handlers which align to right in a field of 10 charachters, precision 2 decimals for fractional part.
I have no idea what this sentence was supposed to be, can you rewrite it in english?
>>
>>61175733
what the fuck, it works like that in java too. either I'm only learning about this now which would be ridiculous or I'm having a serious stroke
>>
>>61175874
that for loop goes out of bounds
>>
>>61175940
yes... I changed the n to 5 before I pressed send....
Also a good reason not to use c style arrays.
>>
>>61175973
What do I put in the header file when initialising the distance method? I've put this but it doesn't work:
>double distance(); .
It give an error in the cpp file:
>prototype for 'double Point::distance(Point&)' does not match any in class 'Point'
>>
>>61176095
Include the parameters type in the paranthesi:
Point&
>>
>>61176095
double Distance(Point&);
>>
>>61176129
Oops i thought i tried that.
How do i create that vector as an object to Point?
>>
>>61176198
std::vector<Point> points {{0,0},{1,2},{3,4},{5,6}};
>>
>>61176095
I wrote public so you could see it should be a public function, as in part of the class.
If you want to split the code, replace the curly brackets with a semicolon in the header and put Point:: in front of the function names in the source file.
What kind of course have you taken which doesn't cover this?

Also what is the last question?
>>
>>61176198
Do I make it like this: http://www.cplusplus.com/forum/general/7950/ ?
It seems overdone
>>
>>61176231
Codeblocks won't let me use this? It says something about c++11 and c++98. I;ve included <vector> too.
>>
>>61176237
I didn't study during the semester and I'm doing it now. What last question? About the alignment? I'll figure that out.
>>
>>61176198
Since we are spoonfeeding you everything anyway....

I think the question is:
How do I create a vector of Points?
It could be like this >>61176231
This will create the points described.
In >>61176248 you create a bunch of identical Points.
Since the task was to create a few, a initializer list is pretty good because you can write it all in a single line.
When you have more, you need better methods.
>>
>>61176282
You need C++11 for { } initialization.
https://stackoverflow.com/a/24398366
>>
>>61176305
But all these things are stuff mentioned in the first lecture, I can see how they would come up during the 2nd lecture as questions, why would they come up in a written exam?
It doesn't make sense.
If you don't understand it by now, do yourself a favor and fail the class.
Hand in a blank piece of paper and go watch netflix or something.
And if you still want to get that education tomorrow, apply yourself and catch up with the work you lost.
If you don't want to get the education, drop out while you haven't invested too much time and do something else.
>>
Well thank you all I wasn't expecting this much spoonfeeding. My tummy is full. I'll end it here.
>>
File: 32143612.png (190KB, 500x281px) Image search: [Google]
32143612.png
190KB, 500x281px
>>61174593
Write it in python and use cython to convert it to C++
>>
>>61176426
Words of wisdom right here and I had a lot of thoughts about this. I've already failed the exam and now I'm taking it again and honestly I don't think I can learn enough in 4 days. I still don't know what I'm gonna do about education. I'm not decided if to apply myself or not yet so I'm doing the minimum to pass the year. I really don't know what to do because I don't enjoy college because it's exactly like highschool, a lot of subjects which very few I enjoy. I would like to apply myself to only one. Honestly I would drop out and go to shit tier college just for degree and get into programming.
Thread posts: 38
Thread images: 2


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

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.