Question

Triangles lem Descri A triangle is a geometric shape built from three line segments which form the sides of triangle Not all lengths can be combined to form a triangle. For example, while we can build a triangle with lengths 3, 4 and 5, we cannot build a triangle with lengths 1, 3 and 5 (Why not?) Your program is to get the name of a file from the command line. Open this file and read each line. Each line will contain three integers, separated by spaces You are to print each legal triangle and skip the illegal ones. Lets agree that a triangle with 0 area is still a legal triangle Notes: Turn in only your completed triangles.c file You must pick up the filename from the command line. Do not ask the user for it! A sample triangle data file can be found on canvas in triangle1.txt If your program is invoked with no filename, or without a valid filename (e.g. it wont open), print an error and exit. See the example output below If your program is invoked with multiple items on the command line, just assume the first is the filename and ignore the rest. . . . Required Input: A file name on the command line . That text file will contain lines, each with three integers uire Your output should print each legal triangle. It should look something like the following example It must contain your name pi@raspberrypi:~/projects/c /triangles Triangles: Usage: triangles filename pi@raspberrypi:~/projects/c s./triangles nofile.txt Triangles Fİle pi@raspberrypi:~/projects/c ./triangles trianglel.txt Triangles: 2405, 1581, 871 350, 353, 334 390, 404, 753 723, 1167, 1360 710, 419, 897 286, 322, 176 1510, 831, 1270 174, 192, 219 931, 1135, 1236 456, 277, 320 nofile.txt cannot be opened

Triangle1.txt

2405 1581 871

350 353 334

390 404 753

723 1167 1360

147 496 210

955 1259 2756

549 940 2029

1096 427 473

710 419 897

286 322 176

704 1963 878

1510 831 1270

174 192 219

931 1135 1236

456 277 320

780 1138 2350

295 195 694

1286 738 2483

332 198 103

462 1176 472

1038 625 2565

260 160 452

1080 437 232

165 291 533

911 2738 1546

631 1404 359

677 2932 1264

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include<stdio.h>

int main(int argc, char *argv[]){

printf("Triangles: \n");

if(argc!=2){

printf("Usage: triangles filename\n");

return 0;

}

FILE *fp;

fp = fopen(argv[1], "r");

if(fp==NULL){

printf("File %s cannot be opened.\n", argv[1]);

return 0;

}

int a,b,c;

while(!feof(fp)){

fscanf(fp, "%d %d %d", &a, &b, &c);

if(a+c>b && a+b>c && b+c>a){

printf("%d, %d, %d\n", a, b, c);

}

}

fclose(fp);

return 0;

}

nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/September/13092018$ g++ triangles.c -o triangles nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/September/13092018$ ./triangles Triangles: Usage: triangles filename nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/September/13092018$ ./triangles nofile.txt Triangles: File nofile.txt cannot be opened. nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/September/13092018$ ./triangles triangle1.txt Triangles: 2405, 1581, 871 350, 353, 334 390, 404, 753 723, 1167, 1360 710, 419, 897 286, 322, 176 1510, 831, 1270 174, 192, 219 931, 1135, 1236 456, 277, 320

Add a comment
Know the answer?
Add Answer to:
Triangle1.txt 2405 1581 871 350 353 334 390 404 753 723 1167 1360 147 496 210...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT