0

I am a very beginner at structs and barely understand what they are useful for. I have an assignment to create an array with space for 5 points. Every point will be entered by the user.

I don't understand how to use structs with arrays. I tried this but it doesn't work at all...

        #include <stdio.h>

int main(void)
{

    struct Input
    {
        int x;
        int y;
    };

    struct Input arr[5];


    for(int i=1; i <= 5; i++)
    {
        printf("Enter coordinates for point #%d (x,y): ", i);
        scanf("%d,%d", &arr[i].x, &arr[i].y);
    }

    printf("\n\nYou entered:\n");

    for(int i=1; i <= 5; i++)
    {
        printf("Point #%d: %d, %d\n", i, arr[i].x, arr[i].y);
    }


    getchar();
    getchar();

    return 0;

}

EDIT

I am trying to calculate the average of the x coordinations, but obs.avgX doesn't work like planned, the calculation always gets 0.

#include <stdio.h>

int main(void)
{

    struct Observations
    {
        int x;
        int y;
        double avgX;
        double avgY;
    };

    struct Observations arr[5];
    struct Observations obs;


    for(int i=0; i < 5; i++)
    {
        printf("Enter coordinates for point #%d (x,y): ", i +1);
        scanf("%d, %d", &arr[i].x, &arr[i].y);
    }

    printf("\n\nYou entered:\n");

    for(int i=0; i < 5; i++)
    {
        printf("Point #%d: %d, %d\n", i, arr[i].x, arr[i].y);
    }


    obs.avgX = arr[0].y + arr[1].y + arr[2].y + arr[3].y + arr[4].y / 5;
    printf("Average of X: %d", obs.avgX);


    getchar();
    getchar();

    return 0;

}
0

3 Answers 3

3

If you need to create an array of 5 points, first you have to define what a point is.

For example:

struct Point {
    int x;
    int y;
};

Then you have to define an array of 5 points:

struct Point array_of_points[5];

And you can use it like this:

array_of_points[0].x = 20;
array_of_points[0].y = 10;
// etc...
array_of_points[4].x = 3;
array_of_points[4].y = 8;
Sign up to request clarification or add additional context in comments.

6 Comments

What if I want to store a coordinate in each array? Do I have to use ´array_of_points[5][5]´?
2-dimension arrays make the code more difficult to read and it's harder to understand what you are trying to achieve. struct members usually have meaningful names for this goal.
I updated the OP again and I don't understand why on earth my scanf loop is bugging?! If you only enter "2,2" the loop will be infinite!
Oh, I am overloading the arrays.
Please don't update the question too much, else answers become useless. If you want to update, try to keep the initial text, and add edit 'd sections.
|
1

SirDarius gave a nice explanation about code.

You said

barely understand what they are useful for

In fact , you can consider , that structs are a kind of "data saver" where you can store DIFFERENT or MULTIPLE types of data .

For example , using arrays , you can not store both x and y coordinates at a cell (Multiple types of Data).

Moreover , if you want to store a Student's School Info , you may want to store , his unique ID (integer) ,his name (string) , his grade (integer) and the average of each lesson (float) (Different Data). Using structs you can easily store them that way :

struct Student {
int ID;
char * name;
int grade;
float average;
}

Hope that helped to understand ,the reason why structs are really useful :)

Comments

0

Braces are missing as below when computing the average,

obs.avgX = (arr[0].y + arr[1].y + arr[2].y + arr[3].y + arr[4].y) / 5;

Also to print a double type variable you should be using %f as the format specifier rather than %d.

4 Comments

Okay, but the code does still not work perfectly because it rounds the average value wrong, it's cutting off the decimals. Is it because my array values are integers and not double?
Because your struct members x & y are of type integers, and above division results an int. You can make those integers as double or type cast result of division to float.
Okay, I think it's easier to typecast the division to float. I tried this but it doesn't work: obs.avgX = (float)(arr[0].x + arr[1].x + arr[2].x + arr[3].x + arr[4].x) / 5;
Yep, try replacing division by 5.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.