0

The col value automatically got changed to 1048576000 after for loop why is this I got no clue for that. And because of this the index value in final step goes out of the bound. The code is

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

void fill(float k,int row,int col)
{
    float glass[row*(row+1)/2]={0.0};
    int index=0;
    glass[index]=k;
    for(int i=1;i<=row;i++)
    {
        for(int j=1;j<=i;j++)
        {
            k=glass[index];
            glass[index]=(k>=1.0)?1.0:k;
            k=(k>1.0)?(k-1.0):0.0;
            glass[index+i]+=k/2.0;
            glass[index+i+1]+=k/2.0;
            index++;
        }
    }
    cout<<col<<endl;
    index=row*(row-1)/2 +col -1;
    cout<<glass[index]<<endl;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        float k;
        int i,j;
        cin>>k>>i>>j;
        fill(k,i,j);
    }
    return 0;
}

Please help me out.

  • `float glass[row*(row+1)/2]` is not a valid code and you seem to access this array out of bounds which causes undefined behavior. – user7860670 Jun 19 '18 at 06:47
  • 2
    First of all, please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) Then you should know that C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array), but some compilers add it as an extension to the language. It's not portable or something you should relying on existing everywhere. Use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) instead. Lastly, are you sure you will never go out of bounds of the array? – Some programmer dude Jun 19 '18 at 06:48
  • Possible duplicate of [Initialize a float array on construction](https://stackoverflow.com/questions/12694214/initialize-a-float-array-on-construction) – Joseph D. Jun 19 '18 at 06:51
  • 1
    It could be a useful mathematical exercise to determine what the maximum value of `index+i+1` will be. For row and col both 1, it will be 2 at some point, and you only expect one element in `glass`. – Arndt Jonasson Jun 19 '18 at 06:57
  • `for(int i=1;i<=row;i++)` uses 1-based indexing. C++ arrays are zero based. This often leads to bugs. – user4581301 Jun 19 '18 at 07:19
  • What is k, row and col when the function is first called? Are you sure `index+i+1` will not exceed your array? – Goswin von Brederlow Jun 19 '18 at 11:12

1 Answers1

0

I really don't know what you are looking for. First of all, the array you are working on is one dimensional, which means there is always a single row.

Second, when you create the array:

float glass[row*(row+1)/2]={0.0};

its maximum size is size row x (row + 1) / 2) − 1, but at the end of the function you used the formula:

index = row * (row - 1) / 2 + col - 1;

What if the value of col is greater than row? That means the line with the code:

cout<<glass[index]<<endl;

is trying to access a value beyond the size of glass array, which results to segmentation fault.

  • But before that point col is already changed. The more likely culprid is ` glass[index+i+1]+=k/2.0;` before that. – Goswin von Brederlow Jun 19 '18 at 11:08
  • No every variable is under bound but the value col automatically goes out of bound. and only when i pass parameters as float,int ,int . If I pass parameter int,int ,float it gives the write answer . – Pratyush Sharma Jun 20 '18 at 11:13