I have declared a structure st below with a struct variable arr[], an array of structs. Im trying to assign the value 1 to the 'num' variable, and values 1 to 10 to 'val' variable of the first 10 locations of array arr[]. And value 2 to 'num' and values 11 to 20 to 'val' of the next 10 locations. But when i traced the code, it won't assign values to the respective num and val of the same array location. If i wanted to assign num=1 and val=4 to the 4th structure it would assign num=1 to val of 3rd structure and val=4 to num of 4th structure. I have no idea why this is happening.
#include<iostream.h>
#include<conio.h>
class abc
{
public:
struct st
{
int num;
int val;
};
st arr[21];
void funct();
};
void abc::funct()
{
int i,j,k=1;
for(i=1;i<=2;i++)
{
for(j=1;j<=10;j++)
{
arr[k].num=i;
arr[k].val=j;
k++;
}
}
}
int main()
{
abc z;
z.funct();
return 0;
}