#Help with code to pass 3 exercise tests

94 messages · Page 1 of 1 (latest)

candid burrow
#

Hi guys, I need some help.

Im doing a project for class, im new to C++, and I cant quite figure out how to pass all fo the texts for the project.

This is my code:

#include <iostream>
#include <math.h>
#include<iomanip>
using namespace std;

double calculateDistance(int x1, int y1, int z1, int x2, int y2, int z2)
{
return sqrt((x2-x1)(x2-x1)+(y2-y1)(y2-y1)+(z2-z1)*(z2-z1));
}

int main() {

int x1,y1,z1,x2,y2,z2;
cout<<"Please enter the coordinates of point-01:"<<endl;
cin>>x1>>y1>>z1;
cout<<"Please enter the coordinates of point-02:"<<endl;
cin>>x2>>y2>>z2;
cout<<"The distance between points (1,1,1) and (0,0,0) is:"<<endl;
cout<<setprecision(3)<<calculateDistance(0,0,0,x1,y1,z1)<<endl;
cout<<"The distance between points (3,4,5) and (0,0,0) is:"<<endl;
cout<<setprecision(3)<<calculateDistance(0,0,0,x2,y2,z2)<<endl;
cout<<"The distance between points (1,1,1) and (3,4,5) is:"<<endl;
cout<<setprecision(3)<<calculateDistance(x1,y1,z1,x2,y2,z2)<<endl;

}

I also attached it to the post.

The problem im facing is that there are 3 tests that I need to pass simultaneously. I have passed the first test, but for the second test, the values for "1,1,1" need to be "2,4,6" and the values for "3,4,5" need to be "7,8,9". For the third test, the values for "3,4,5" need to be "3,3,3".

Keep in mind I have to pass all 3 tests at the same time. I cant just do 1 test then go change the number value.

Could someone please help me adjust my code accordingly?

In the post images, you can see the explanation for my failure of test 2. The problem im facing is that I cant just flat out change the number values or I fail test 1.

fallow skiffBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

old mountain
#

you're simply missing a couple *

#

and when you print you need to use the values you read in, not the hardcoded ones

candid burrow
#

That's basically what my teacher said but im not too sure what he meant by that or where.

old mountain
#

cout << "The distance between points (1,1,1) and (0,0,0) is:" << endl; this is hardcoded to be 1,1,1

candid burrow
#

mhm

old mountain
#

so, cout << "The distance between points (" << x1 << "," << ...

candid burrow
#

Why do I have to do that? What's the difference, and why do I now need quotations and the less than symbols?

old mountain
#

quotes encase strings

#

<< takes whatever is to the right of it and feed it to cout

#

which prints it to the console

#

so you are printing The distance between points (

#

then you print x1, which is whatever user inputs

#

for example 2

#

then your console looks like The distance between points (2

#

so you just keep printing the rest

#

to make the whole message

candid burrow
#

Okay, Ill try to see if my editing of my code is properly understanding what you're saying.

I understand all the questions must get a little annoying by the way. Im new to all this and having hard time understanding some fundamentals. Thank you for being so patient.

#

oh

#

I got it

#

sweet

#

Thank you so much

#

Would you mind quickly explaining why I need to make the specific changes I did though?

#

and also why I need to have a return here

old mountain
#

The cout changes?

#

It’s hard coded as a placeholder, you’re supposed to show what the user inputted

boreal root
#

which textbook were you given?

old mountain
#

The return returns that value from the function back to wherever it was called

candid burrow
#

My teacher just reads over notes that he typed out before the course for us

#

and then speeds through examples

#

so if I look down to try to practice something on my own with his guidance I essentially miss something

#

and its left me in this state of being very confused about what we've learned over the past few weeks. I've tried to talk to him about it quite a few times but he keeps missing the point and just assumed I need help on a couple assignments.

#

Its really quite frustrating but that's why Im here now. So I can get help from all of you guys.

brazen stirrupBOT
#
How to Learn C++ Programming

Everyone learns differently, but the best way is to learn by doing: Working on personal projects, solving programming challenges, etc. Here are some resources to accompany you:

C++ Tutorials
Practice

• [Programming Challenges](#archived-resources message)
CodinGame
LeetCode
HackerRank

See Also

An Introduction to Modern CMake
cdecl+ - converts C/C++ declarations to prose
• Our #archived-resources channel. It contains many useful utilities.

old mountain
#

Online sources will probably be more organized than whatever your teacher is doing

#

And at your own pace

candid burrow
#

Yep. College.

#

First year

boreal root
#

then it would be extremely unlikely that there's not a textbook

#

it would be in your syllabus

#

it might not be mandated, but certainly it will have "recommendations"

old mountain
#

There’s lots of $$$$$ to be made off of you and your textbook purchases

candid burrow
#

Well yes, it's there, but it's incredibly expensive.

old mountain
#

Lol

boreal root
#

those recommendations are not as optional as the word "recommendation" might make it sound

boreal root
old mountain
#

But yeah just use those online sources

boreal root
#

I'm saying, going without a textbook is not optional

old mountain
#

Just as good and free

boreal root
#

nor does your professor think it's optional

old mountain
#

I didn’t buy a single textbook through uni lol

boreal root
#

but still, like, the cost of textbooks should be something factored in by basically anyone/everyone who goes to uni

candid burrow
boreal root
#

if you have enough for uni but not enough for books, well... then you don't really have enough for uni

brazen stirrupBOT
#
How to Learn C++ Programming

Everyone learns differently, but the best way is to learn by doing: Working on personal projects, solving programming challenges, etc. Here are some resources to accompany you:

C++ Tutorials
Practice

• [Programming Challenges](#archived-resources message)
CodinGame
LeetCode
HackerRank

See Also

An Introduction to Modern CMake
cdecl+ - converts C/C++ declarations to prose
• Our #archived-resources channel. It contains many useful utilities.

boreal root
#

sorry mistyped, try again

old mountain
#

First link

candid burrow
#

Yep got it

#

What do you think the textbook could provide me with? Part of the reason I decided not to get it other than price is because my prof told me that we wouldn't be using it for class, but that it would be there for knowledge.

#

But I dont see the point of spending 250 for that

#

when it's probably online too?

boreal root
boreal root
#

if you can't afford it, the assumption would be that you made a really serious financial error, and you probably should have attended a less expensive school so you can actually afford your books

#

but, there are usually ways to get your books cheaper

#

used copies, go to the library, etc

old mountain
#

If you go through the site diligently you will know all you need for whatever c++ you’ll learn in college

#

and libgen exists

boreal root
#

this is a case where there is at least one reasonable free online resource

#

not all your courses will probably be like this

candid burrow
#

Actually, I just checked. I was wrong.

There is no textbook for the course in the syllabus and there isnt one under textbook suggestions for my program in our schools bookstore online.

boreal root
#

beyond first year second year you really can't rely on there being easily available free books

boreal root
candid burrow
#

My tech kit for my other class

boreal root
#

do you go to your labs?

candid burrow
#

I do

boreal root
#

then ask your TAs which book you're supposed to get

#

I have a really hard time believing there's not a book for the course

#

or at least, idk, 3-4 recommendations

candid burrow
#

We dont have a TA for that class, but I'll ask my teacher. There are no suggestions anywhere. Not in the bookstore site for the school and not in our course info.

old mountain
#

my man just tryna ask a question here, the online source is good enough for "learning to learn"

#

going through that source will give you the fundamentals needed to write programs and know the keywords to look stuff up for anything beyond

#

it's what I used and the info is much more complete and delivered faster than any lecture I went to

candid burrow
#

Yes I think that's what Ill do

old mountain
#

profs make a dozen mistakes and forget a dozen key insights per hour

candid burrow
#

Thank you for all your help 🙏

old mountain
#

@civic citrus

civic citrus
#

why double post

candid burrow
#

I'm new here, didn't exactly know how to get help, so I tried both. My apologies.

fallow skiffBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.