0

I have a homework that wants us to make an array of (x,y) coordinates that the user enters but we don't know how many will be entered, it will end when (0,0) is entered, the last one will not be added to the array and then we sort them and print them.So I used pointers to make array but I can't insert all the elements that I want to insert it only recognizes the last entered one. When I try to print them it will print only the last one I entered correctly the others mostly comes (0,0) or some random numbers.

int main()
{
  int x,y,*xp,*yp;
  int a = 0,s,m=12;

  etiket:
  scanf("%d %d",&x,&y);

  printf("\n");
  while (x != 0 || y != 0)
    {   
    a=a+1;
    xp = (int*) malloc(sizeof(int)*m);
    yp = (int*) malloc(sizeof(int)*m);

    //printf("%d %d\n",x,y);
    if( a%10==0)
      {
        xp = (int*) realloc(xp,sizeof(int)*m+10);
        yp = (int*) realloc(yp,sizeof(int)*m+10);
      }
  xp[a]=x;
  yp[a]=y;

  printf("%d %d\n",*(xp+a),*(yp+a));
  goto etiket;


//SortPoints((xp+a),(yp+a),a);
}
//printf("siralama:\n");
//for(s=0; s<=a; s++)
//{
//  printf("%d %d\n",*(xp+s),*(yp+s));

//}
}

So this is my work-in-progress code. I don't even know if it's possible I would appreciate any help. Thank you in advance.

Lithellion
  • 11
  • 3
  • 4
    1. Please indent your code so that it is more readable. 2. Why are you using `goto` 3. [using cast for malloc is bad](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Ed Heal Mar 12 '17 at 09:48
  • 4
    That `goto` is messing your code up majorly - it jumps out of the `while` loop to the `scanf` causing all kinds of bad things to happen (including memory leaks) - there's a good reason why `goto` is often considered to be bad – UnholySheep Mar 12 '17 at 09:56
  • I had to go back to get more elements. And I couldn't think of another method to do it. What should I use? – Lithellion Mar 12 '17 at 09:56
  • Sorry forgot to add them to the text. The condition is when (0,0) point is entered the program stops and prints out the sorted array. I tried to print them without sorting them because I thought my sorting algorithm was wrong but apparently it wasn't or atleast it wasn't the only wrong thing. – Lithellion Mar 12 '17 at 10:04
  • 1
    You are allocating a new array after every `goto` because it jumps out of the `while` loop (which also creates a memory leak) - your logic is just flawed (and the `goto` is just the wrong tool for this problem) – UnholySheep Mar 12 '17 at 10:05
  • 2
    Personally I would throw away this code and have a rethink. – Ed Heal Mar 12 '17 at 10:07
  • You have to read coordinates and insert them to your array *wlile* thy are different from (0,0). Looks like a job for a *while* loop. – n. m. could be an AI Mar 12 '17 at 10:27
  • Could you use C++ ?? Then it will be lot easier. Simply Define a class named point and get input and after that produce sorted array simply using a function ? If not, then you should work with structure similarly. Let me know which way I could help you. – S.Rakin Mar 12 '17 at 10:29
  • It should be `while` I think but I need to go back to get more elements and I can't go with `goto` and I can't think of anything else to use in this. – Lithellion Mar 12 '17 at 10:29
  • I can't use C++ it's an assignment I have to use C. – Lithellion Mar 12 '17 at 10:30
  • Couple of pointers. 1. remove the goto 2. change the while to `while (scanf("%d %d",&x,&y) == 2 && (x != 0 || y != 0))` 3. Do the realloc each time in the loop, using `i` – Ed Heal Mar 12 '17 at 10:36
  • I'm sorry I don't really understand with what you meant in Do the realloc each time in the loop, using `i` – Lithellion Mar 12 '17 at 10:48
  • Use `relloc(xp, sizeof(int) * i)` where `i` is the number of items that are required in the array. – Ed Heal Mar 12 '17 at 11:03
  • Thank you very much for the help it's finally done. – Lithellion Mar 12 '17 at 11:22

1 Answers1

0

As you have been restricted to use C it's lot easier to use structure for dynamically allocated memory for arrays. Here's the code:-

#include<stdio.h>
#include<malloc.h>

#define SIZE 20  // SIZE OF YOUR ARRAY FOR DYNAMIC MEMORY ALLOCATION

typedef struct  {
              int x;
              int y;     // That's the point with x and y coordinate

    } point ;   // Similar as int n, float a .... now point is a 
                         // type which holds the value of x and y

int main(void){

       point *p;    // pointer decleration for point type

       p= (point *)malloc(SIZE* sizeof(point)); // Memory Allocation

       int i=0;

       do{
             p++;  // increment of pointer

             i++;

             printf("Enter values for point %d: (x,y)= ",i);    

             scanf("%d", &p->x);     // input x and y 
             scanf("%d", &p->y);

       }while((p->x) !=0 || (p->y) !=0 ); // loop breaks when inputs 0,0


   p--;    // decrement pointer as 0,0 value will have no use
   i--;

   printf("\n\n\n");

   for(;i>0;i--,p--)    
          printf("Point %d: (x,y)=(%d,%d)\n", i,p->x, p->y); 
                                   // displaying point values user given

   printf("\n\n\n");


 return 0;
}

Here p holds the hole array of points. Now, for sorting you just have to pass p in a function as a agrument like that and write the regular sorting algorithm inside the body.

  point* SortingArray(point *p)

This function will return another arrays of points which is sorted. Hope you could do this yourself.

Furthermore, probably very soon You will learn C++ where this kind of works are so easy using class. In fact, in C++ there have a lot of features in the STL(Standard Template Library) like lists,vectors... for these kind of works which support dynamic memory allocation very soundly.

Let me know if You find any problem to grasp the concept. Thanks.

S.Rakin
  • 812
  • 1
  • 11
  • 24
  • 1
    [Don't cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) - also this code will invoke UB if a user tries to enter more than 20 value pairs – UnholySheep Mar 13 '17 at 07:36
  • @UnholySheep ... yes if you want more value you just change the SIZE.. '#define SIZE 20' – S.Rakin Mar 13 '17 at 07:37
  • From the question: *"we don't know how many will be entered"* - so no, you don't just change a (compile time) definition, this has to be handled at runtime – UnholySheep Mar 13 '17 at 07:38
  • @UnholySheep .... how ?? is it possible in C ? In C++ it runtime handling is possible.. – S.Rakin Mar 13 '17 at 07:39
  • ... What do you think a `std::vector` does? Or why we even bother using `malloc` if we could just create a static array? – UnholySheep Mar 13 '17 at 07:40
  • @UnholySheep ... yes .. you're right. I could try it later. – S.Rakin Mar 13 '17 at 07:41
  • I didn't check your code so I don't know if it works but we still haven't covered structures so I don't think they would have let me use them. And thanks to Ed Heal the problem is solved and I submitted my assignment. Thanks anyway for taking the time. – Lithellion Mar 13 '17 at 12:04