Home > Back-end >  Storing the right value in a two dimensional array in c. But when I tried to print the value it show
Storing the right value in a two dimensional array in c. But when I tried to print the value it show

Time:06-19

Problem: In this question I tried to solve a bitwise operator problem. Giving two number as input Input will be two number. Here is some input:

n = 3
k = 3

Then I need to construct "a" and "b" in some way(for instance when "a = 1" then b will be one past a to <= n. The results of the comparisons are below:

a b   and or xor
1 2   0   3  3
1 3   1   3  2
2 3   2   3  1

Here is my code:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.


void calculate_the_maximum(int n, int k) 
{
    // Two dimentional array for storing the value of and or xor
    int rowOfArray = 3;
    
    int sets[rowOfArray][k];
    
    
    //For loop for a
    for (int i = 0; i < k; i  )
    {
        int a = i   1;
        //For loop for b
        for (int j = a; j < n; j  )
        {
            int b = j;
            b  ;
            printf("{%i, %i}\n", a, b);
            //Storing and operation result to the array
            sets[0][j - 1] = a&b;
            printf("And: %i\n", sets[0][j - 1]);
            //Storing or operation result to the array
            sets[1][j] = a|b;
            printf("Or: %i\n", sets[1][j]);
            //Storing xor opertaion result to the array
            sets[2][j   1] = a^b;
            printf("Xor: %i\n", sets[2][j   1]);
        } 
      
    }
   
    
    //Find the biggest number in array 
    for (int i = 0; i < rowOfArray; i  )
    {
        int big;
        
        for (int j = 0; j < k; j  )
        {
            big = 0;
            printf("Big1: %i\n", big);
            
            if (big < sets[i][j])
            {
                big = sets[i][j];

                printf("Big2: %i\n", big);
            }
        }
        
        printf("big3: %i\n", big);
        if (big < k)
        {
            printf("%i\n", big);
        }
        else
        {
            printf("%i\n", 0);
        }
    }
}

int main() {
    int n, k;
  
    scanf("%d %d", &n, &k);
    calculate_the_maximum(n, k);
 
    return 0;
}

I used too many printf function to show that what have I done wrong. I stored the value as I expected but later in the for loop when I tried to print the specific position integer I didn't get the correct result even sometimes I got bigger and negative number too.

Here is the output:

 3 3
{1, 2}
And: 0
Or: 3
Xor: 3
{1, 3}
And: 1
Or: 3
Xor: 2
{2, 3}
And: 2
Or: 3
Xor: 1
Big1: 0
Big1: 0
Big2: 2
Big1: 0
Big2: 120329728
big3: 120329728
0
Big1: 0
Big2: 1986993953
Big1: 0
Big2: 3
Big1: 0
Big2: 3
big3: 3
0
Big1: 0
Big1: 0
Big2: 32765
Big1: 0
Big2: 3
big3: 3
0

CodePudding user response:

If you run the code, you'll get stack corruption error which is due to indexing the array beyond its allocated size. Accessing memory that's not for your program is undefined behavior. Anything might happen. The program may crash or it may not. For my specific compiler and the fact that I ran the code in debug mode and not release mode, the program crashed with the error I mentioned.

Now to fix the error, from what you explained, you have three columns for and, or and xor. So you need to reverse the dimensions of set (set[k][rowOfArray], better change the name to rowSize or n_columns or so). Also reverse the indexing, e.g. change set[0][j-1] to set[j-1][0] and so on. I'm not sure what you're trying to do in the second part, though.

CodePudding user response:

As is, for the input 3 3, j holds the values 1, 2, and 2, so clearly sets[2][j 1] is Undefined Behaviour since it accesses the subarray at index 3, when valid indices are [0, 2].


Given the source problem, the issues are more clear.

k is simply a limit on output, and should not be used as a bound for iteration, or for calculating the storage required for the number of k-combinations.

It is unnecessary to store all the results at once. For each combination, each value of the operation a ? b (where ? is a bitwise operator) can be tested against the value k and the currently stored maximum value for the given operator.

Here is a quick solution. Compile with -DDEBUG to see additional output.

#include <stdio.h>

void setmax(int *dest, int value, int limit)
{
    if (value < limit && value > *dest)
        *dest = value;
}

void calc(int n, int k)
{
    int and = 0;
    int  or = 0;
    int xor = 0;

#ifdef DEBUG
    printf("INFO: a   b     and  or   xor\n");
#endif

    for (int a = 1; a < n; a  ) {
        for (int b = a   1; b <= n; b  ) {
            setmax(&and, a & b, k);
            setmax(&or,  a | b, k);
            setmax(&xor, a ^ b, k);

#ifdef DEBUG
            printf("INFO: %-3d %-3d   %-3d  %-3d  %-3d\n",
                    a, b, a & b, a | b, a ^ b);
#endif
        }
    }

    printf("%d\n%d\n%d\n", and, or, xor);
}

int main(void)
{
    int n, k;

    if (2 != scanf("%d%d", &n, &k))
        return 1;

    calc(n, k);
}

Sample runs:

./a.out <<< "3 3"
2
0
2
./a.out <<< "5 4"
2
3
3
  • Related