Homework in C - Help Needed

Joined
Oct 16, 2024
Messages
1
Reaction score
0
Hey, I was accepted to the University for programming major, even though I basically never programmed anything in my life before, however my first homework here is to make a program in which I write 3 coordinates ([x, y]) and the program should be able to calculate 3 more coordinates so with one of the new coordinates and the 3 I wrote there, it would make a rectangle, or any of its special form (square, rhombus, parallelogram) and then the program should decide whether the newly formed tetragonal is square, etc. However, I need the program to work even when I fill in the coordinates in these forms

[ 5, 0 ]

or when it’s all written in one line only

[10.5, 10.5]
[12.5, 10.5][10.5, 15e+0]

or when I write it like this [3,

4

])

but my program automatically says it’s incorrect input and I can’t figure out how to make that work, I even tried ai, but it didn’t help me neither, so any help from a more experienced programmer would be greatly appreciated. Everything else should work just fine. I hope that my problem is understandable as English isn’t my first language

Here is the part of my program, where I fill in the coordinates:


int main(void) {
double ax, ay, bx, by, cx, cy;
double tolerance = 0.0000001;
char input[100];

printf("Bod A:\n");
fgets(input, sizeof(input), stdin);
if (sscanf(input, "[%lf, %lf]", &ax, &ay) != 2) {
printf("Nespravny vstup.\n");
return 1;
}


printf("Bod B:\n");
fgets(input, sizeof(input), stdin);
if (sscanf(input, "[%lf, %lf]", &bx, &by) != 2) {
printf("Nespravny vstup.\n");
return 1;
}

printf("Bod C:\n");
fgets(input, sizeof(input), stdin);
if (sscanf(input, "[%lf, %lf]", &cx, &cy) != 2) {
printf("Nespravny vstup.\n");
return 1;
}
"Bod A" means coordinate A in my language and so on, and "Nespravny vstup" means incorrect input
 
Joined
Sep 4, 2022
Messages
136
Reaction score
16
Hello,

because of the need for multiple coordinates , you have to use an Array with all values in.
it could be a 1 dimension array , and all your values will be by 'two items' , following two others

float figures[6] = {5,2,4,4,7,3}
// 3 values serie are in.

you can use a 2 dimensions array too :

float figures[3][2] = {{5,2},{4,4},{7,3}}

// square have same sides width
// rectangle have width greater than unequals adjacent sides

one loop can read all values in your Array, or a direct call to values by coordinates:
figures[0] ;
figures[1] ;

or

figures[0][0] ;
figures[0][1] ; // here are the two first figure values
------------------------------------------------------------------------
Lesson about Arrays with C langage :

simple Array in C
n-dimensions Array in C

------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,440
Members
47,101
Latest member
DoloresHol

Latest Threads

Top