your program cpp file of Task1 You can upload file several t

your program (.cpp file) of Task1. You can upload file several times for each question. But we mark only the latest attempt. If you have questions on the coursework, please raise in the next lab session. Task1 [6 points] Create a program that reads coordinates of four points (A, B, C an D) in 2D from the file (week8triangle.txt) and reports the user the followings: 1: Each length of sides of the triangle of points A, B and C 2: Total length of sides of the triangle

Solution

#include <iostream>
#include <fstream>
#include <Math.h>
using namespace std;
//make sure input file format and path of the file are clear
//below is example file content
/*
x y
a 1.2 3.6
b 1.8 2.9
c 3.6 5.7
d 9.9 3.2
*/
int main()
{
float cords[4][2],total=0,dist;
char c,x,y;
ifstream inData(\"week8triangle.txt\");
inData >>x>>y; //read the first line to x and y chars
for (int i=0; i<4; i++) {      
inData >>c>> cords[i][0]>> cords[i][1];//Read x and y coordinates at position i into the array cords
}
inData.close();
for (int i=1; i<4; i++) {      
//caluclating sides and total

dist=sqrt(pow((cords[i-1][0]-cords[i][0]),2)+pow((cords[i-1][1]-cords[1][0]),2));
cout<<\"side \"<<i<<\":\"<<dist<<endl;
total+=dist;
}
cout<<\"Total distance:\"<<total<<endl;
return 0;
}

//example output:-

side 1:1.89737
side 2:2.1095
side 3:7.40945
Total distance:11.4163

 your program (.cpp file) of Task1. You can upload file several times for each question. But we mark only the latest attempt. If you have questions on the cours

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site