(Newb stuff) Variable changing value for no apparent reason?

Posts 1–4 of 4 · Page 1 of 1
(Newb stuff) Variable changing value for no apparent reason?
Programming teacher couldn't figure out what's wrong. Link to program: https://*****************/file/d/0By1k...1BMkkzdzg/edit
In file: https://*****************/file/d/0By1k...xHUUNvdm8/edit
So, the problem is at While(k!=0)
From the in file, k takes the value of 4. I noticed that after the command a[2]=prop[linie][2]; k becomes 1. Before that command, k is still 4. Why does it change? I tried changing the position of a[2]=prop[linie][2]; before and after other commands, k was still 4 before it, k becomes 1 after it. Why?
 
code from link above

Code:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream in("elicop.in");
    ofstream out("elicop.out");
    int m,n,k,a[2],b[2],sj,linie=1,nr,nru,cateta,punct[2],aux,auxx,i,j,c,l,nrelicop=0,cr;
    in>>m>>n;
    int teren[m+1][n+1];
    for(aux=1; aux<=m; aux++)
    {
        for(auxx=1; auxx<=n; auxx++)
        {
            in>>teren[aux][auxx];
        }
    }
    in>>k;
    int prop[k][5];
    for(aux=1; aux<=k; aux++)
    {
        for(auxx=1; auxx<=5; auxx++)
        {
            in>>prop[aux][auxx];
        }
    }
    while(k!=0)
    {
        a[2]=prop[linie][2];
        a[1]=prop[linie][1];
        b[1]=prop[linie][3];
        b[2]=prop[linie][4];
        sj=prop[linie][5];
        linie=linie+1;
        if(sj==-1)
        {
            if(b[1]>a[1])
            {
                punct[1]=b[1];
                punct[2]=a[2];
            }
            else
            {
                punct[1]=a[1];
                punct[2]=b[2];
            }
            l=punct[1];
            if(punct[1]-a[1]<0)
            {
                cateta=-(punct[1]-a[1]-1);
            }
            else
            {
                cateta=punct[1]-a[1]+1;
            }
            c=cateta;
            nru=0;
            nr=0;
            while(l!=0)
            {
                for(i=1; i<=l; i++)
                {
                    for(j=1; j<=c; j++)
                    {
                        cr=teren[i][j];
                        if(cr==0)
                        {
                            nru=nru+1;
                        }
                        nr=nr+1;
                    }
                    c=c-1;
                }
                l=l-1;
            }
            if(nru==nr)
            {
                nrelicop=nrelicop+1;
            }
            else
            {
                if(nru>nr/2)
                {
                    out<<k<<" ";
                }
                else
                {
                    nrelicop=nrelicop+1;
                }
            }
        }
        else
        {
            if(a[1]>b[1])
            {
                punct[1]=a[1];
                punct[2]=b[2];
            }
            else
            {
                punct[1]=b[1];
                punct[2]=a[2];
            }
            l=punct[1];
            if(punct[1]-a[1]<0)
            {
                cateta=-(punct[1]-a[1]-1);
            }
            else
            {
                cateta=punct[1]-a[1]+1;
            }
            c=cateta;
            nru=0;
            nr=0;
            while(l!=0)
            {
                for(i=1; i<=l; i++)
                {
                    for(j=1; j<=c; j++)
                    {
                        cr=teren[i][j];
                        if(cr==0)
                        {
                            nru=nru+1;
                        }
                        nr=nr+1;
                    }
                    c=c-1;
                }
                l=l-1;
            }
            if(nru==nr)
            {
                nrelicop=nrelicop+1;
            }
            else
            {
                if(nru>nr/2)
                {
                    out<<k<<" ";
                }
                else
                {
                    nrelicop=nrelicop+1;
                }
            }
        }
        k=k-1;
    }
    out<<endl<<nrelicop;
    return 0;
}

int m,n,k,a[2],b[2] ........;

while(k!=0)
{
a[2]=prop[linie][2];
How many items does a[] have?



It's because of how you declared a[2]: which creates an array of 2 items: a[0] and a[1].
And then tried to access index 3, ie. a[2].


I don't know, and might depend on the compiler used, but your variables (in the order you declared them) will be laid out in ram in one of two ways:
from left-to-right, or right-to-left.


See how in the 2nd example (variables created in reverse order), a[2] would actually give the memory location of 'k'.
If you wrote to a[3] (index 4), I believe it would also over-write 'n'.

C++ doesn't have index-checking. That is, if you refer to a[10000], it will calculate the address where that item WOULD be, and continue as normal. It doesn't enforce the fact that you declared a[] to only have 2 items.

edit: int may not be 4 bytes on your platform..same concept still applies.
Endianness is usually a property of the hardware and thus results in different compiler behavior for different architectures. Therefore, it is dictated by the platform you are coding for, not the compiler you choose.

C++ most definitely has bounds checking (its up to you if you want to use them), you should be using containers like std::array.
Quote Originally Posted by Fovea View Post
Endianness is usually a property of the hardware and thus results in different compiler behavior for different architectures. Therefore, it is dictated by the platform you are coding for, not the compiler you choose.

C++ most definitely has bounds checking (its up to you if you want to use them), you should be using containers like std::array.
I am not talking about endianness, but the order in which the variables are stored.

Should his code not compile then? He says it does...?
int m,n,k,a[2],b[2] ........;
...

while(k!=0)
{
a[2]=prop[linie][2]; // <-- suspect index

In vs2012 I get a compile error, in vs2010 I do not.

Code:
#include <iostream>
using namespace std;

int main()
{
	int x, y, z, arr[2];

	x = 111;
	y = 222;
	z = 333;
	cout <<"&x = 0x" << &x << "\n" <<
		   "&y = 0x" << &y << "\n" <<
		   "&z = 0x" << &z << "\n" <<
		   "&arr 0x" << arr << "\n" << endl;

	cout << "(x,y,z)-> " << x << "," << y << "," << z << endl;
	//testing
	arr[2]= 14;
	arr[3] = 15;
	arr[4] = 16;
	cout << "(x,y,z)-> " << x << "," << y << "," << z << endl;

	system("PAUSE");
	return 0; 
}
vs2010


vs2012


His other variable is changing because he's writing to index [2], but that isn't actually part of the array.

edit: Update
(for vs 2012) I was using the Debug config...when switching to 'Release', the code compiles and runs fine.
 
slightly diff. code

Code:
#include <iostream>
using namespace std;

int main()
{
	int x, y, z, arr[2], j = 4, k = 5, l = 6;

	x = 111;
	y = 222;
	z = 333;
	cout <<"&x = 0x" << &x << "\n" <<
		   "&y = 0x" << &y << "\n" <<
		   "&z = 0x" << &z << "\n" <<
		   "&arr 0x" << arr << "\n" <<
		   "&j = 0x" << &j << "\n" <<
		   "&k = 0x" << &k << "\n" <<
		   "&l = 0x" << &l << endl;


	cout << "(x,y,z)-> " << x << "," << y << "," << z << endl;
	//testing
	arr[2]= 14;
	arr[3]= 15;
	arr[4]= 16;
	cout << "(x,y,z)-> " << x << "," << y << "," << z << endl;
	cout << "(j,k,l)-> " << j << "," << k << "," << l << endl;
	system("PAUSE");
	return 0; 
}




Note the order the variables are stored in.(!)

@OP Which compiler are you using?
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us