Reputation: 1
I am a first year student and I am beginning my adventure with programming from learing language C. I was given an assigment that I have no idea how to do.In this program I need to use malloc, void functions,pointers and structures.
This is the assignment:
1.Create a predetermined number of figures.Allocate memory for these figures by using malloc.
2.Add new figure.
3.Remove random figure.
4.Add a point to the random figure.
5.Remove random point from a figure.
6.Show all the figures. If figure have 1 point print "it is a point". If figure have 2 points print "it is a line". If figure have 3 points print "it is a triangle".etc
Programme needs to be composed of following lines(But you can add some things).
struct Point
{
int x, y;
};
struct Figura
{
unsigned short size;
struct Point **p;
};
This is my illogical code with a strange approach.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include <string.h>
#define SIZE 20
struct Point{
int x;
int y;
};
struct figure{
unsigned short size;
struct Point **p;//I have no idea how to work with this.
char figure[SIZE];
};
void create_space_for_points(struct Point ***point,int beginning_quanity);
void create_space_for_figures(struct figure ***figure,int beginning_quanity);
void ceating_base_of_figures(struct Point **point,struct figure **figure,int beginning_quanity,int *quanity,int *sum_of_points);
void adding_figure(struct Point ***point,struct figure ***figure,int *quanity,int *sum_of_points);
void showing_figures(struct figure **figure,int quanity);
int main()
{
struct figure **figure;
struct Point **point;
int beginning_quanity;
int *quanity=0;
int *sum_of_points=0;
printf("How many figures do you want to create?: ");
scanf("%d",&beginning_quanity);
//At this stage I am creating separate space for point and figures.
create_space_for_points(&point,beginning_quanity);
create_space_for_figures(&figure,beginning_quanity);
ceating_base_of_figures(point,figure,beginning_quanity,&quanity,&sum_of_points);//At this stage sometime it doesn't work. Maybe because of the memory.
adding_figure(&point,&figure,&quanity,&sum_of_points);//new figure is added at the end of list.
showing_figures(figure,quanity);
return 0;
}
//I am sure I made a mistake with memory allocation but I don't know where.
void create_space_for_points(struct Point ***point,int beginning_quanity){
struct Point **temp1=(struct Point**)malloc(beginning_quanity*sizeof(struct Point))
for(int i=0;i<beginning_quanity;i++)
temp1[i]=(struct Point*)malloc(beginning_quanity*sizeof(struct Point));
*point=temp1;
}
void create_space_for_figures(struct figure ***figure,int beginning_quanity){
struct figure **temp=(struct figure**)malloc(beginning_quanity*sizeof(struct figure));
for(int i=0;i<beginning_quanity;i++)
temp[i]=(struct figure*)malloc(sizeof(struct figure));
*figure=temp;
}
void ceating_base_of_figures(struct Point **point,struct figure **figure,int beginning_quanity,int *quanity,int *sum_of_points){
int number_of_points=0,i;
srand(time(NULL));
char word[SIZE];
for( i=0;i<beginning_quanity;i++){
char figures[SIZE];
for(int j=0;j<rand()%5+1;j++){
point[j]->x=rand()%10;
point[j]->y=rand()%10;
//printf("%d %d\n",punkt[j]->x,punkt[j]->y);
number_of_points++;
}
printf("-------Number of points-------\n%d",number_of_points);
if(number_of_points==1){
printf("----point----\n");
strcpy(word,"point");
strcpy(figure[i]->figure,word);
}
if(number_of_points==2){
printf("----line----\n");
strcpy(word,"line");
strcpy(figure[i]->figure,word);
}
if(number_of_points==3){
printf("----triangle----\n");
strcpy(word,"triangle");
strcpy(figure[i]->figure,word);
}
if(number_of_points==4){
printf("----square----\n");
strcpy(word,"square");
strcpy(figure[i]->figure,word);
}
if(number_of_points==5){
printf("----pentagon----");
strcpy(word,"pentagon");
strcpy(figure[i]->figure,word);
}
printf("\n");
*quanity=beginning_quanity;
sum_of_points+=number_of_points;
number_of_points=0;
}
printf("Number of figures: %d\n",*quanity);
}
void adding_figure(struct Point ***point,struct figure ***figure,int *quanity,int *sum_of_points){
int number_of_points;
char word[SIZE];
printf("How many points should a new figure have: ");
scanf("%d",&number_of_points);
int new_size=*quanity+1;
struct figure **temp=(struct figure **)malloc((new_size)*sizeof(struct figure*));
for(int i=0;i<*quanity;i++)
temp[i]=(*figure)[i];
temp[*quanity]=(struct figure*)malloc(sizeof(struct figure));
if(number_of_points==1){
strcpy(word,"point");
strcpy(temp[*quanity]->figure,word);
}
if(number_of_points==2){
strcpy(word,"line");
strcpy(temp[*quanity]->figure,word);
}
if(number_of_points==3){
strcpy(word,"triangle");
strcpy(temp[*quanity]->figure,word);
}
if(number_of_points==4){
strcpy(word,"square");
strcpy(temp[*quanity]->figure,word);
}
if(number_of_points==5){
strcpy(word,"pentagon");
strcpy(temp[*quanity]->figure,word);
}
free(*figure);
*figure=temp;
++(*quanity);
printf("\n");
printf("new number of figures: %d\n",*quanity);
number_of_points=0;
}
void showing_figures(struct figure **figure,int quanity){
for(int i=0;i<quanity;i++)
printf("----%s----\n",figure[i]->figure);
}
I am stuck at removing the figures and I am not even sure if I correctly used structures and I have absolutely no idea how to add a random point to the figure or remove one.
Upvotes: 0
Views: 295
Reputation: 30165
If you are sure struct Point **p;
is not a typo and meant to be struct Point*
and that is the entire assignment text, the intention maybe that the Point
objects are not owned by Figura
, and that it just references/points to some separately owned Point*
. I would seek clarification from your teacher.
In which case you just consider a Point*
as your "value", and Figura
as a basic dynamic array.
#include <stdio.h>
#include <stdlib.h>
struct Point
{
int x, y;
};
struct Figura
{
unsigned short size;
struct Point **p;
};
// Note, you should also write a free function so that any Figura you create don't end up leaking memory!
void figura_init(struct Figura *f)
{
f->size = 0;
f->p = NULL;
}
// Point *p must be allocated elsewhere (stack, global, malloc, whatever)
// and must not be deleted before being removed from the Figura (or Figura itself deleted)
void figura_add_point(struct Figura *f, struct Point *p)
{
// Note not checking for duplicates
f->p = realloc(f->p, sizeof(struct Point*) * (f->size + 1)); // Expand array
f->p[f->size] = p; // Store p
++f->size;
}
void figura_remove_point(struct Figura *f, struct Point *p)
{
for (unsigned short i = 0; i < f->size; ++i)
{
if (f->p[i] == p)
{
// Not storing any specific order, so can remove an item by overwriting it with the last
f->p[i] = f->p[f->size - 1];
--f->size;
// Note, could free unused memory
return;
}
}
}
int main()
{
struct Point some_points[] = {{0,0}, {10,5}, {15,20}, {1, 4}, {5, 40}};
struct Figura fig;
figura_init(&fig);
figura_add_point(&fig, &some_points[1]);
figura_add_point(&fig, &some_points[3]);
figura_add_point(&fig, &some_points[4]);
for (unsigned short i = 0; i < fig.size; ++i)
{
printf("%d %d\n", fig.p[i]->x, fig.p[i]->y);
}
figura_remove_point(&fig, &some_points[3]);
printf("After remove.\n");
for (unsigned short i = 0; i < fig.size; ++i)
{
printf("%d %d\n", fig.p[i]->x, fig.p[i]->y);
}
}
Upvotes: 1