Structure in a Structure and Array - c

I have a few problems with the following assignments:
a)
Define a new data type tpos for storing the position of a solid in the two dimensional plane in
single precision. Afterwards define another new data type tsolid that can store the weight of
a solid in double precision and its position in the two dimensional plane. Use tpos for defining
the position component of tsolid.
b)
Define an array of length two of type tsolid. The positions and weights of both solids (array
elements) shall now be read from the keyboard by using the scanf function. For checking
correctness of your program, display the array content on the screen.
So far i have:
#include <stdio.h>
#include <stdlib.h>
struct tpos
{
float xy
};
struct tsolid
{
struct tpos;
double m;
};
int main()
{
struct tsolid array[2];
return 0;
}
How do I Proceed now? Already tried a few things, but sadly they didnt work out. The text basically says i have to save 1 solid in 1 array element, right? But how can i save pos x , pos y, and the weight m all in 1 array element and then print them all at the same time? Do i have to use a pointer ?

First of all, if you need to store "two dimensional plane", you should use two elements inside struct.
Other thing is that you don't access struct elements by saying struct tpos; - you should use tpos xy.
Repaired code:
#include <stdio.h>
#include <stdlib.h>
struct tpos
{
float x;
float y;
};
struct tsolid
{
tpos xy;
double m;
};
int main()
{
tsolid array[2];
return 0;
}
When it comes to scanf and displaying content you need to decide yourself on the "output layout".
And you access certain parts of the "element" by using
array[i].m;
array[i].xy.x;
array[i].xy.y;

tpos needs two members, one for the each coordinate.
tsolid needs a name for the tpos member.
Then write a loop that asks for each value, puts them in the array, and another loop that prints the values from the array.
#include <stdio.h>
struct tpos
{
float x, y;
};
struct tsolid
{
struct tpos position;
double mass;
};
int main()
{
struct tsolid array[2];
for (int i = 0; i < 2; i++) {
printf("Enter x, y, and mass for object %d:\n", i+1);
scanf("%f %f %lf", &array[i].position.x, &array[i].position.y, &array[i].mass);
}
printf("You entered:\n");
for (int i = 0; i < 2; i++) {
scanf("x = %f, y = %f mass = %lf\n", array[i].position.x, array[i].position.y, array[i].mass);
}
return 0;
}

Related

Understanding part of an algorithm

Below is a part of an algorithm i was given to use for a project, but as it's my first time to use an algorithm i don't understand the following lines. Please will need your help.
For i=1 to n do
t[i] .mark <-- 0
t[i] .num <-- -1
End
This pseudo code can be translated to C
Use struct
struct cm{
int mark;
int num;
};
#define N 10
int main(void)
{
struct cm t[N];
for (int i=0;i<N;i++){
t[i].mark = 0;
t[i].num = -1;
}
//print your struct elements field
for (int i=0;i<N;i++){
printf("%d: %d, %d\n",i ,t[i].mark, t[i].num);
}
}
We have an array of struct because of we need each element of it have two field of data (i.e. mark,num).
struct cm t[N]; define a N length array of structure cm.
In loop we assign to each field of array elements proper values.
For more readability you can use typedef instead of using struct to define your desire data structure in this case.
typedef vs struct
Use typedef
typedef struct typecm{
int mark;
int num;
}typecm;
#define N 10
int main(void)
{
typecm s[N];
for (int i=0;i<N;i++){
s[i].mark = 0;
s[i].num = -1;
}
//print values
for (int i=0;i<N;i++){
printf("%d: %d, %d\n",i ,s[i].mark, s[i].num);
}
}
The "t" seems to be an array of objects, and "mark" and "num" are properties of the object.
This may help you:
From an array of objects, extract value of a property as array

C function signature problems

I'm working on a C assignment for school and the assignment asks us to use this specific function signature that is causing errors for my compiler.
I'm getting an error from line 38 (the smallest function signature) in vector_test.c, the error reads ""," expected (got "*")". This is my first time working in C so I think I must be doing something wrong in regards to how I have setup the typedef in types.h or something along those lines, just not exactly sure and thought I'd get some extra opinions. Anything you could point out that I'm doing wrong here would be helpful, thank you!
Here's the code:
vector_test.c
#include "types.h"
int smallest(const Vector3t, int);
void main (int argc, char *argv[]) {
unsigned int N = 0;
printf ("Number of elements ===>");
scanf ("%u", &N);
struct Vector3t points[N];
for (int i = 0; i < N; i++)
{
scanf("%d,%d,%d", &points[i].x, &points[i].y, &points[i].z);
}
for (int p = 0; p < N; p++)
{
printf("%i", points[p].x);
printf("%i", points[p].y);
printf("%i\n\n", points[p].z);
}
int result = smallest(points, N);
printf("%s", "The point closest to the origin is (");
printf("%i", points[result].x);
printf("%s", ", ");
printf("%i", points[result].y);
printf("%s", ", ");
printf("%i", points[result].z);
printf("%s", ")");
}
int smallest(const Vector3t* pts, int n)
{
int shortest = 99999999;
int shortIndex = -1;
for (int i = 0; i < n; i++)
{
int distance = pts[i].x + pts[i].y + pts[i].z;
if (distance < shortest)
{
shortest = distance;
shortIndex = i;
}
}
return shortIndex;
}
types.h
#ifndef types_H
#define types_H
struct vec3 {
int x;
int y;
int z;
};
typedef struct Vector3t {
int x;
int y;
int z;
} vec3;
#endif
Here's the assignment instructions for this specific part so you can see what I'm trying to do:
1. Create a header file called “types.h” (with a macro guard)
a. In this header file, create a struct type called vec3 with int types: x, y, and z
b. Create a typedef to the struct vec3 above and call it Vector3t
2. Create a C source file called “vector_test.c”
a. This file should include “types.h” and any other C system includes you may need
b. Create a main function that does the following:
i. Prompts the user “Number of elements ===> “
ii. Read an unsigned int from the user – this variable will be referred to as N
iii. Create an array of Vector3t of size N and call it points
iv. Read in triples of int (comma separated) from the user to populate the entire array
v. Using the function signature: int smallest(const Vector3t* pts, int n), implement a
function that returns the index of the point that is the closest to the point (0, 0, 0)
vi. Call this function and store the index into an int called result
vii. Print out to the user “The point closest to the origin is (<x>, <y>, <z>)” where <x>, <y>, and <z>
correspond to the values of points[result]
viii. Free all allocated data
Your function forward declaration is:
int smallest(const Vector3t, int);
While your function definition says:
int smallest(const Vector3t* pts, int n)
In your forward declaration you're saying that you're passing in the struct as a parameter, while in your definition you're saying that it's taking a pointer to the struct. These are incompatible signatures.
You get it wrong in the first steps:
Create a header file called "types.h" (with a macro guard)
In this header file, create a struct type called vec3 with int types: x, y, and z
Create a typedef to the struct vec3 above and call it Vector3t
For first,
struct vec3 {
int x;
int y;
int z;
};
is correct. Then to define the typedef, you first give the the actual type, then the type alias:
typedef struct vec3 Vector3t;
Alternatively, these 2 can be combined into one typedef:
typedef struct vec3 {
int x;
int y;
int z;
} Vector3t;
Also the declaration of smallest doesn't match the definition (shouldn't they look alike?) and the return type of main must be int.

i have see a a runtime error in code and it force closes

What wrong with this code i see no compilation error. I want to make an array of structures of type pointer to structure. Then i want to assign x and y coordinates and at the end i want to print all the coordinates. I did (*pp).x at places and pp->x at other places. Coz both are one and the same things whats wrong. Please let me know.
#include <stdio.h>
#include <conio.h>
#define MAX_STRUCT 10
struct point{
int x;
int y;
};
void PrintIt(int x, int y);
main()
{
struct point *pp[MAX_STRUCT];
printf("Enter how many coordinates you want to input: ");
int count=0, n;
scanf("%d", &n);
while(count<n){
printf("\nEnter the X & Y coordinate of the point: ");
int i,j;
scanf("%d%d", &i, &j);
(*pp[count]).x=i; /* or we can do this : pp[count] -> x same thing
(*pp[count]).y=j; // or we can do this : pp[count] -> y same thing
count++;
}
PrintIt(pp[count]->x, pp[count]->y);
return 0;
}
void PrintIt(int x, int y)
{
printf("\n(%d,%d)", x, y);
}
You need to dynamically allocate memory for pp:
#include <stdlib.h>
struct point *pp = malloc(MAX_STRUCT * sizeof(*pp)); // sizeof(struct point) would work too
Or, don't make them pointers:
struct point pp[MAX_STRUCT];

read data from struct into 2d array c

I'm trying to plot coordinates to a map using a 2d Array. The data for the coordinates has been entered by the user and held in a structure. This is a snippet of code taken out of my main program.
#include <stdio.h>
#include <stdlib.h>
char map[5][10]={
"..........",
"..........",
"..........",
"..........",
".........."
};
struct coord{
int x;
int y;
};
int loop, n, i;
struct coord mg[3];
int main(){
for(loop=0;loop<3;loop++){
printf("\n\nEnter MAGENTA X coordinate 0:\n");
scanf("%d",&mg[loop].x);
printf("\nEnter MAGENTA Y coordinate:\n");
scanf("%d",&mg[loop].y);
}
printf("Struct contains:\n");
for(loop=0;loop<3;loop++){
printf("\tx %d,%d y\n",mg[loop].x,mg[loop].y);
}
/*AS SUGGESTED IN ANSWER BELOW (PAUL92),I HAVE DONE THIS BUT GET AN ERROR*/
n=3;
i=0;
for (i = 0; i < n; i++) {
map[mg.y][mg.x] = 'x';
}
getchar();
return(0);
}
The error I get is
testing.c:35:13: error: member reference base type 'struct coord [3]' is not a
structure or union map[mg.y][mg.x] = 'x';
~~^~
What I'm trying to achieve is getting the coordinates held in struct to then be assigned to the correct element in the array i.e. if the user enters 3(x),5(y) then the element map[4][2] will hold this an display and 'x'.
I am not sure I have understood exactly what you want, but if you want just to mark the points on the map with an 'x' for example, you can to iterate over the points:
for (int i = 0; i < n; i++) {
map[mg.y][mg.x] = 'x';
}
where n is the number of points.
Of course, this is after you initialize the map.

How do you make an array of structs in C?

I'm trying to make an array of structs where each struct represents a celestial body.
I don't have that much experience with structs, which is why I decided to try to use them instead of a whole bunch of arrays. However, I keep on running into numerous different errors. I've tried to implement the techniques that I've seen on various threads and on StackOverflow (such as Array of structs in C and C - initialize array of structs), however not all of them were applicable.
Further information for those who have read this far: I don't need any of this to be dynamic, I know/define the size of everything beforehand. I also need this to be a global array as I'm accessing this in several different methods which have defined arguments (i.e. GLUT methods).
This is how I'm defining the struct in my header:
struct body
{
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double mass;
};
I have a list of other global variables that I'm defining before I define the interior of the struct, and one of those is the array of this struct (basically, if I'm being too unclear in my fogged speak, the line below is above the stuff above):
struct body bodies[n];
Just so you know, n is something that I've legitimately defined (i.e. #define n 1).
I use this array in several different methods, but the easiest and least space consuming one is a simplified form of my main. Here I initialize all of the variables in each of the structs, just to set the variables for certain before I modify them in some way:
int a, b;
for(a = 0; a < n; a++)
{
for(b = 0; b < 3; b++)
{
bodies[a].p[b] = 0;
bodies[a].v[b] = 0;
bodies[a].a[b] = 0;
}
bodies[a].mass = 0;
bodies[a].radius = 1.0;
}
The current error that I'm facing is nbody.c:32:13: error: array type has incomplete element type where line 32 is where I'm making the array of the structs.
One last clarification, by header I mean the space above int main(void) but in the same *.c file.
#include<stdio.h>
#define n 3
struct body
{
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double mass;
};
struct body bodies[n];
int main()
{
int a, b;
for(a = 0; a < n; a++)
{
for(b = 0; b < 3; b++)
{
bodies[a].p[b] = 0;
bodies[a].v[b] = 0;
bodies[a].a[b] = 0;
}
bodies[a].mass = 0;
bodies[a].radius = 1.0;
}
return 0;
}
this works fine. your question was not very clear by the way, so match the layout of your source code with the above.
Another way of initializing an array of structs is to initialize the array members explicitly. This approach is useful and simple if there aren't too many struct and array members.
Use the typedef specifier to avoid re-using the struct statement everytime you declare a struct variable:
typedef struct
{
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double mass;
}Body;
Then declare your array of structs. Initialization of each element goes along with the declaration:
Body bodies[n] = {{{0,0,0}, {0,0,0}, {0,0,0}, 0, 1.0},
{{0,0,0}, {0,0,0}, {0,0,0}, 0, 1.0},
{{0,0,0}, {0,0,0}, {0,0,0}, 0, 1.0}};
To repeat, this is a rather simple and straightforward solution if you don't have too many array elements and large struct members and if you, as you stated, are not interested in a more dynamic approach. This approach can also be useful if the struct members are initialized with named enum-variables (and not just numbers like the example above) whereby it gives the code-reader a better overview of the purpose and function of a structure and its members in certain applications.
So to put it all together by using malloc():
int main(int argc, char** argv) {
typedef struct{
char* firstName;
char* lastName;
int day;
int month;
int year;
}STUDENT;
int numStudents=3;
int x;
STUDENT* students = malloc(numStudents * sizeof *students);
for (x = 0; x < numStudents; x++){
students[x].firstName=(char*)malloc(sizeof(char*));
scanf("%s",students[x].firstName);
students[x].lastName=(char*)malloc(sizeof(char*));
scanf("%s",students[x].lastName);
scanf("%d",&students[x].day);
scanf("%d",&students[x].month);
scanf("%d",&students[x].year);
}
for (x = 0; x < numStudents; x++)
printf("first name: %s, surname: %s, day: %d, month: %d, year: %d\n",students[x].firstName,students[x].lastName,students[x].day,students[x].month,students[x].year);
return (EXIT_SUCCESS);
}
I think you could write it that way too. I am also a student so I understand your struggle. A bit late response but ok .
#include<stdio.h>
#define n 3
struct {
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double mass;
}bodies[n];
move
struct body bodies[n];
to after
struct body
{
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double mass;
};
Rest all looks fine.
Solution using pointers:
#include<stdio.h>
#include<stdlib.h>
#define n 3
struct body
{
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double *mass;
};
int main()
{
struct body *bodies = (struct body*)malloc(n*sizeof(struct body));
int a, b;
for(a = 0; a < n; a++)
{
for(b = 0; b < 3; b++)
{
bodies[a].p[b] = 0;
bodies[a].v[b] = 0;
bodies[a].a[b] = 0;
}
bodies[a].mass = 0;
bodies[a].radius = 1.0;
}
return 0;
}
That error means that the compiler is not able to find the definition of the type of your struct before the declaration of the array of structs, since you're saying you have the definition of the struct in a header file and the error is in nbody.c then you should check if you're including correctly the header file.
Check your #include's and make sure the definition of the struct is done before declaring any variable of that type.
You can do it in a same manner as you create the array of numbers but wrap the element's values in braces like this ->
struct Wrestler studs[count] = {
{"John", "Cena"},
{"The", "Undertaker"},
{"The", "Big Show"},
{"The", "Rock"},
{"Triple", "H"},
{"Scott", "Hall"},
{"Roman", "Reings"},
{"Dean", "Ambrose"}};
Here is full code
#include <stdio.h>
struct Wrestler
{
char firstName[20];
char secondName[20];
};
void pIntro(struct Wrestler *s)
{
printf("Hi, I am %s %s.\n", s->firstName, s->secondName);
};
int main(int argc, char const *argv[])
{
#define count 8
struct Wrestler studs[count] = {
{"John", "Cena"},
{"The", "Undertaker"},
{"The", "Big Show"},
{"The", "Rock"},
{"Triple", "H"},
{"Scott", "Hall"},
{"Roman", "Reings"},
{"Dean", "Ambrose"}};
for (int i = 0; i < count; i++)
{
pIntro(&(studs[i]));
}
return 0;
}

Resources