Getting Different output everytime I open the serial monitor

After uploading the code once on my Mega 2560, I am getting different output every time I close the Serial Monitor and open again. Can anybody point out the error

What code?

Yes it is in line 23, you are using a random function and it is giving you a diffrent value every time.

This is the code

# include <stdio.h>

unsigned char A[21] = {0X36,0X3E,0X34,0XE8,0X37,0X46,0X99,0XD4,0X68,0X11,0X99,0XA0,0X7E,0XD5,0XA2,0X86,0X62,0XA1,0XEB,0XF0,0X03};
unsigned char B[21] = {0XF1,0X24,0X73,0X79,0X0C,0X5C,0X1C,0XB1,0X45,0XD5,0XCD,0XA2,0X4F,0X09,0XA0,0X71,0X6C,0XBC,0X1F,0XD5,0X00};
unsigned char C[21] = {0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00};
unsigned char T1[21];
unsigned char T2[21];
unsigned char D[21];
unsigned char E[21];
unsigned char F[21];

void add (unsigned char* number1, unsigned char* number2)
{
  int i;
  for (i=0; i<21; i++)
  {
    number2[i] = number1[i] ^ number2[i];
  }
}

void sqr(unsigned char* number, unsigned char* output)
{
  int i,m,k,t1,t2;
  unsigned char pr[41];
        for (i=0; i<41; i++)
	{
                k=4;
		for (m=0; m<8; m+=2)
		{	
                        if ((i%2) != 0)
                        {
                        t2 = bitRead(number[(i-1)/2],k);
                        pr[i] = bitWrite(pr[i],m,t2);
                        k++;
                        }
                        else
                        {
                         t1 = bitRead(number[i/2],(m/2));
                         pr[i] = bitWrite(pr[i],m,t1);
                        }
			
		}
	}
        

 for(i=0; i<21; i++)
        {
          output[i] = pr[i];
        }
	
}


void mul(unsigned char *a, unsigned char *b, unsigned char *output)
{
        int i,j,m,t,bi;
	int x=0, y=0, z = 0;
        unsigned char pr[41];

// first nested loop generating the first 4 columns

	for (i=0; i<21; i++)
	{
		for (j=0; j<=i; j++)
		{
			for (m=0; m<8; m++)
			{
                                bi = bitRead(b[i-j],m);
                                if (bi == 0)
                                {
                                  t = 0;
                                }
                                else 
                                  t = a[j];
				z = z ^ (t<<m);
			}
                        x = x ^ z;
			z = 0;
		}
		y = x>>8; 
		pr[i] = x&0XFF; 
		x = y; 
	}

// second nested loop generating the last 3 columns
	for (i=21; i<41; i++)
	{
		for (j=i-20; j<21; j++)
		{
			for (m=0; m<8; m++)
			{
                                bi = bitRead(b[i-j],m);
                                if (bi == 0)
                                {
                                  t = 0;
                                }
                                else 
                                  t = a[j];
				z = z ^ (t<<m);
			}
                        x = x ^ z;
			z = 0;
		}
		y = x>>8; 
		pr[i] = x&0XFF; 
		x = y; 
	}
	pr[41] = y;

        for(i=0; i<21; i++)
        {
          output[i] = pr[i];
        }
        
	
}

void dble(unsigned char* number1, unsigned char* number2, unsigned char* number3,unsigned char* number4, unsigned char* number5, unsigned char* number6) 
{  
  int i;
 
  sqr(number3, T1); 
  sqr(number1, T2);
  mul(T1, T2, number6);
  sqr(T2, number4);
  sqr(T1, T1);
  add(T2,number4); 
  sqr(number2, T1);
  add(number6,T1); 
  add(T2,T1); 
  add(T1, number5);
  
}

void setup()
{
  Serial.begin(115200);
  int i;
  dble(A, B, C, D, E, F);
  
  for (i=20; i>=0; i--)
  {
   Serial.print(D[i], HEX);
   Serial.print(" ");
  }
  Serial.print("\n");
  for (i=20; i>=0; i--)
  {
   Serial.print(E[i], HEX);
   Serial.print(" ");
  }
  Serial.print("\n");
  for (i=20; i>=0; i--)
  {
   Serial.print(F[i], HEX);
   Serial.print(" ");
  }
}
void loop() { 
}
pr[i] = bitWrite(pr[i],m,t2);

What is pr before this command is executed?

pr[] is a blank array initially

pr[] is a blank array initially

Really?

void sqr(unsigned char* number, unsigned char* output)
{
  int i,m,k,t1,t2;
  unsigned char pr[41];

sohini25: pr[] is a blank array initially

There is no such thing as a blank array. pr[] is an array of bytes with the space for it allocated on the stack. What will be in pr[] ?

There is no such thing as a blank array

unsigned char T1[21];

{ cough }

AWOL:

There is no such thing as a blank array

unsigned char T1[21];

{ cough }

What value is “blank”? It’s not 0, because 0 has the value 0. Blank means no values, and every byte of RAM and FLASH has a value. There is no such thing as “blank” or “empty” values.

sorry if i meant it wrong, pr[] is an array that is supposed to store certain values within the functions. Is the error coming because of this array?

sohini25:
sorry if i meant it wrong, pr is an array that is supposed to store certain values within the functions. Is the error coming because of this array?

Yes. So what exactly is in that array when you start the function? Just defining an array in local scope like that doesn’t actually place anything in the array - it just moves the stack pointer down far enough to accommodate the array, and then place it in the newly created space. That space will contain whatever happened to be in that space from the last time it was allocated. Who knows what that is? It’s certainly not all 0 as you assume it is. The first time you call the function it might be, but it might not - it might contain the contents of variables that were used in previously called functions.

The second time you call the function, if you haven’t called any other functions in between, it may well contain whatever it finished as the last time you called the function.

You have to manually set the contents of the array to a known state at the start of the function. The simplest way is to use the memset() function:

memset(pr, 0, 41 * sizeof(unsigned char));

That would set the whole array to be value 0. Yes, in this instance you could miss out the “* sizeof(unsigned char)” as the size of an unsigned char is 1, but if you were to do it on an int array and just use the number of elements you’d only zero half of it - it’s a good habit to get into to specify the number of elements and the size of each element in that way.

You also need to check for other arrays that are similarly uninitialized.

ok i will try initialising all arrays in the manner you said. But do I need to do it only for the local arrays or also for the global ones ?

sohini25: ok i will try initialising all arrays in the manner you said. But do I need to do it only for the local arrays or also for the global ones ?

Only local ones. Global ones are initialized to 0 at startup by the CRT (C RunTime). Any uninitialized global variables are known as "BSS" variables, and are automatically set to all 0 as one of the first things that happens when a C program starts up - even before main() is run.

So is the uninitialized local arrays causing me to get different outputs from the serial monitor every time I reopen it

sohini25: So is the uninitialized local arrays causing me to get different outputs from the serial monitor every time I reopen it

Yes.