c++ - Getting Memory error while sorting pairs. Can anybody tell me where am i going wrong in the code -
i trying sort pairs (struct pairs { int i; int j; } ) value of i. after sorting store pairs according has highest number of j's corresponding it. (1,3),(1,4),(2,5),(2,6),(2,7). have structure els(int *i,int *j,int count),els[0] should have 2,5,,2,6,,2,7,,where 2's stored in i,5,6,7 in j.els[1] have 1, correspondings. in structure els when using pairs structure code os running fine,but when going 2 individual arrays i,j getting assert() coredumped error.
#include<iostream> #include<time.h> #include<stdio.h> #include<math.h> #include<stdlib.h> struct pairs { int i; int j; }; struct els { int *i; int *j; int count; }; void sortpairs(struct pairs *p,int count); void sortj(struct pairs *p,int count); void swapp(struct pairs *p,struct pairs *q); int random(int min,int max); int elina(int el,int arr[],int siz); int main() { struct pairs *pp = new struct pairs[10]; srand(time(0)); for(int = 0;i<10;i++) { pp[i].i = random(0,10); pp[i].j = random(0,10); } std::cout <<"before\n"; for(int = 0;i<10;i++) { std::cout << pp[i].i <<" "<<pp[i].j<<"\n"; } sortj(pp,10); std::cout << "done sorting\n"; std::cout <<"after\n"; for(int = 0;i<10;i++) { std::cout << pp[i].i <<" "<<pp[i].j<<"\n"; } sortpairs(pp,10); return 0; } void swapp(struct pairs *p,struct pairs *q) { struct pairs temp; temp = *p; *p = *q; *q = temp; } int random(int min,int max) { int n; n = rand()%(max - min) + min; return n; } int elina(int el,int arr[],int siz) { for(int = 0 ;i < siz;i++) { if(arr[i] == el) return i; } return -9; } void sortj(struct pairs *p,int count) { for(int = 0 ; < count - 1;i++) for(int j = + 1;j < count;j++) { if(p[i].i > p[j].i) swapp(&p[i],&p[j]); } } void sortpairs(struct pairs *p,int count) { int *arr = new int[count]; int unqc = 0,az = 0,t; int i; for(i = 0; i< count ;i++) { arr[i] = p[i].i; if(t = elina(arr[i],arr,i) < 0) unqc++; } az = i; std::cout <<"the unique elements "<<unqc<<"\n"; struct els *e = new struct els[unqc]; int ec = 0; //e[0].i = new struct pairs[10]; e[0].i = new int[10]; e[0].j = new int[10]; e[0].count = 0; for(int j = 0 ; j < count;j++) { if(j > 0) { if(p[j].i == p[j-1].i) { //e[ec].p[count].i = p[j].i; //e[ec].p[count].j = p[j].j; e[ec].i[count] = p[j].i; e[ec].j[count] = p[j].j; e[ec].count++; } else { ec++; //e[ec].p = new struct pairs[10]; e[ec].i = new int[10]; e[ec].j = new int[10]; e[ec].count = 0; //e[ec].p[count].i = p[j].i; //e[ec].p[count].j = p[j].j; e[ec].i[count] = p[j].i; e[ec].j[count] = p[j].j; e[ec].count++; } } else { //e[ec].p[count].i = p[j].i; //e[ec].p[count].j = p[j].j; e[ec].i[count] = p[j].i; e[ec].j[count] = p[j].j; e[ec].count++; } } for(int j = 0 ; j < unqc;j++) { std::cout << e[j].count <<"\n"; } }
in various places
e[ec].i[count] = p[j].i; e[ec].j[count] = p[j].j; should be
e[ec].i[e[ec].count] = p[j].i; e[ec].j[e[ec].count] = p[j].j; if had used std::vector instead of new have found code easier write, , have fewer bugs , limitations.
Comments
Post a Comment