-2

Getting error or garbage while Sorting members of structure in cplusplus.

I want to sort the array based on first value of 2d array using structure in c/c++. I'm getting garbage or runtime error. I can't sort the array using bubble sort, I don't know how to handle it.

Here is my code

#include <bits/stdc++.h>
using namespace std;

struct x {
    int l[100][3];
};

int main()
{   
    int n, k, i, j;
    cin>>n>>k;
    struct x o[n], p;
    
    for(i=0;i<n;++i) {
        cin>>o[i].l[i][0]>>o[i].l[i][1];
    }
    
    for(i=0;i<n;++i) {
        for(j=0;j<n-i-1;++j) {
            
            if((o[j].l[j][0])>(o[j+1].l[j+1][0])) {
            //cout<<o[j].l[j][0]<<" "<<o[j+1].l[j+1][0]<<"\n";
                p = o[j];
                o[j] = o[j+1];
                o[j+1] = p;
            }
        }
    }
    
    for(i=0;i<n;++i) {
        for(j=0;j<2;++j) {
            cout<<o[i].l[i][j]<<" ";
        }
        cout<<"\n";
    }
    
    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    I can't compile your program. Please read [Why should I not `#include `?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) and [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Ted Lyngmo Dec 06 '20 at 12:12
  • Your data structure looks strange. You are only using such `o[j].l[j][0])`, i.e. index of `o` = index of `l`. So, what is the role of `l[100]` in the struct `x`? – Damien Dec 06 '20 at 12:22
  • Since the index of 'o' is alway eqaul to it's member 'l', I guess you don't need an array in 'o'. Also, the input data 'k' wasn't used in the program. – ytlu Dec 06 '20 at 13:26

1 Answers1

0

I will give you a trial version according my understanding. Please do pronounce if I took your logic wrongly.

First I build a struct of `int3` to contain 3 integers with some utilities to simplify the coding, then a array of `int3` size 100 in the structure `xxx` (your `x`). Note that the `operator[i]` returns the `xxx[i].p[0]` of ith element, and `operator(i)` returns the `int3 p[3]` for exchange purpose. Both operators have `rvalue` and `lvalue` versions.

The structures: `int3` and `xxx`

#include <iostream>
struct int3
 { int p[3];
   int3() = default;
   int3(const int3&a) {for (int i=0; i<3; i++) p[i]=a.p[i]; }
   int3& operator=(const int3&a) = default;
   void print() { std::cout <<"("<< p[0] <<", "<<p[1] <<") "; }
};
struct xxx {
    int3 s[100];
    int operator[](const int i) const {return s[i].p[0];}
    int3 operator()(const int i) const {return s[i];}
    int&operator[](const int i) {return s[i].p[0];}
    int3&operator()(const int i) {return s[i];}
    void print(const int n) {
       for(int i=0; i<n; i++) {
          std::cout << "No " << i << " = ";
          s[i].print();
          std::cout << std::endl;
        }
    }
};

In the test `main()`, I removed the input of `k`. It is not used.

int main()
{
    int n, k, i, j;
    xxx ox;
    int3 q;
    std::cout << "input n = ";
    std::cin >> n;
    while (n>100) {
           std::cerr << "Array out of range!\n";
           std::cout << "input n = ";
           std::cin >> n;
          }
   for(i=0;i<n;++i) {
           std::cout << "a[" << i << "].p[0] ,p[1] = ";
           std::cin >> ox.s[i].p[0] >> ox.s[i].p[1];
          }
 std::cout << "****input list:\n"; ox.print(n);
 for(i=0;i<n;++i) {
    for(j=0;j<n-i-1;++j) {
         if( ox[j] > ox[j+1] ) {
              q = ox(j);
              ox(j) = ox(j+1);
              ox(j+1) = q;
            }
       }
   }
 std::cout << "****sorted list:\n"; ox.print(n);
 return 0;
}

A test run with `n = 5` in my MSYS2 system

$ ./a.exe
input n = 5
a[0].p[0] ,p[1] = 5 5
a[1].p[0] ,p[1] = 7 7
a[2].p[0] ,p[1] = 3 3
a[3].p[0] ,p[1] = 8 8
a[4].p[0] ,p[1] = 4 4
****input list:
No 0 = (5, 5)
No 1 = (7, 7)
No 2 = (3, 3)
No 3 = (8, 8)
No 4 = (4, 4)
****sorted list:
No 0 = (3, 3)
No 1 = (4, 4)
No 2 = (5, 5)
No 3 = (7, 7)
No 4 = (8, 8)

Good luck!

ytlu
  • 412
  • 4
  • 9