Jump to content

Could I improve this program? (C++)

ohJey

You input 4 integers and it gives you the slope, midpoint, tells you if the slope is defined and closes if it isn't, then you input an x and it gives you the y value that lies on the original line

Any tips, tricks, knowledge would be appreciated xD

#include <iostream>
#include <cmath>

using namespace std;
int main ()
{
    int x1,y1,x2,y2; ///initialize integers
    cout << "\nPlease input 4 integer values for (x1,y1) and (x2,y2) respectively.\n"; ///prompt user for input
    cin >> x1 >> y1 >> x2 >> y2;
    cout << "\nYou entered x1= " << x1;
    cout << "\nYou entered y1= " << y1;
    cout << "\nYou entered x2= " << x2;
    cout << "\nYou entered y2= " << y2;
    double m1 = static_cast<double>(x1+x2)/2; /// m1 is "midpoint 1"
    double m2 = static_cast<double>(y1+y2)/2; /// m2 is "midpoint 2"
    if ((x2-x1)==0)  ///check whether slope is defined
    {
        cout << "\nThe slope is not defined, program abort\n";
        return 0;
    }
    else
    {
        cout << "\nSlope is defined";
    }
    double slope = (y2-y1)/(x2-x1); ///slope is y/x, stored slope in type double named slope
    cout << "\nSlope:" << slope;
    cout << "\nMidpoint:" << m1 << "," << m2; ///midpoint in x,y format m1=x m2=y
    cout << "\nInput an x value:"; ///prompt user for x value and store in variable type double
    double x3;
    cin >> x3; /// compute the corresponding y- value on the line through (x1 ,y1) and (x2 ,y2) and report that
    double y3,m; ///initialize y and m as double
    slope = m; ///set slope to m
    y3 = m*(x1-x3)+y1; ///solve for y
    cout << "The corresponding value for y: " << y3;
}

 

Linux "nerd".  If I helped you please like my post and maybe add me as a friend :)  ^_^!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×