0

The code below enters the number of arrays and the number of queries based on the arrays. The query consists of the array number and the desired element from that particular array.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct array
{
    int arr[100000];
    int n;
};

int main()
{
    int i, j, b, d, e, f;
    struct array a[100000];
    cout << "Enter the number of arrays and the number of queries:";
    cin >> b;
    cin >> e;
    cout << "Enter the value array size and enter the array elements:";
    for(j = 0; j < b; j++)
    {
        cin >> a[j].n;
        int c = a[j].n;
        for(i = 0; i < c; i++)
        {
            cin>>a[j].arr[i];
        }
    }

    for(i=0;i<e;i++)
    {
        cout << "Enter the query that consists of array number and the index of the desired element:";
        cin >> d;
        cin >> f;
        cout << a[d].arr[f] << "\n";
    }
    return 0;
}
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
neha rout
  • 1
  • 1
  • 1
    I'm gonna bet on a stackoverflow, increase your stack size and see if it fixes the problem. Yeah .. you are trying to allocate about 40GB of stack. – nick Mar 24 '20 at 07:08
  • Or simply don't create a bunch of huge 100'000 element arrays on the stack. Looking at the code, it seems they should only be length `b` or `c`. So just use an `std::vector>` and use `resize()` before setting the element values. – AVH Mar 24 '20 at 07:10
  • Also see https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice. – AVH Mar 24 '20 at 07:10
  • Those variable names are all of them bad. – sweenish Mar 24 '20 at 07:11
  • Also see https://stackoverflow.com/questions/301586/difference-between-using-includefilename-and-includefilename-h-in-c. – AVH Mar 24 '20 at 07:11
  • @Dmitry Kuzminov I usually chalk it up to copy pasting here and try not to go too hard on formatting. Looks like they might be mixing tabs and spaces. But those variable names have no excuse. – sweenish Mar 24 '20 at 07:20
  • @sweenish the code is mostly needed not to tell the computer what to do but to communicate other engineers your ideas. I advice you to study any reasonable coding style, otherwise you may experience strong rejection from the colleagues. – Dmitry Kuzminov Mar 24 '20 at 07:38
  • @DmitryKuzminov Why would you imply that I don't practice consistent style? That's not what I said. At all. Please don't try and put words into my mouth. – sweenish Mar 24 '20 at 09:18

0 Answers0