0
votes

NOTE: I had two variables with the same name... Big thanks to Stefan Birladeanu and Henrik for noticing that!*

Recently I started writing code that helps me input the values of bool function to Veitch (Karnaugh) diagram with 4 variables. The code should write elements to matrix size 4x4 but with these indexes:

  1. element - index 3,3
  2. element - index 2,3
  3. element - index 3,2
  4. element - index 2,2
  5. element - index 0,3
  6. element - index 1,3
  7. element - index 0,2
  8. element - index 1,2
  9. element - index 3,0
  10. element - index 2,0
  11. element - index 3,1
  12. element - index 2,1
  13. element - index 0,0
  14. element - index 1,0
  15. element - index 0,1
  16. element - index 1,1 This is the code of main():

        void main()
        {
            int n;
    
        n=4;
    
        int **VeitchDiagram;
    
        //allocate memory for Veitch diagram
        VeitchDiagram = new int *[n];
        for(int i=0; i<n; i++)
            VeitchDiagram[i]=new int [n];
    
        //enter the elements
        for(int i=0; i<n; i++)
        {
            int j, k;
            if(i%2==1)
            {
                k=0;
                if(i<2)
                    j=4;
                else
                    j=-1;
                for(int k=0; k<2; k++)
                {
                    if(i<2)
                        j--;
                    else
                        j++;
                    cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
                    k++;                            //0,3     1,3     0,2     1,2     if i%2==1 and i<2
                    cin >> VeitchDiagram[k][j];     //0,0     1,0     0,1     1,1     if i%2==1 and i>=2
                    k--;
                }
            }
            else
            {
                k=3;
                if(i<2)
                    j=4;
                else
                    j=-1;
                for(int k=0; k<2; k++)
                {
                    if(i<2)
                        j--;
                    else
                        j++;
                    cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
                    k--;                            //3,3     2,3     3,2     2,2    if i%2==0 and i<2
                    cin >> VeitchDiagram[k][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                    k++;
                }
            }
        }
    
        //free memory allocated for VeitchDiagram
        for(int i=0; i<n; i++)
            delete [] VeitchDiagram[i];
        delete [] VeitchDiagram;
    }
    
3
Have you stepped through it with a debugger?Luchian Grigore
Yes... Multiple times. It lets me to input first two elements and then gives me access violation.zkristic

3 Answers

2
votes
        for(int k=0; k<2; k++)
        {
            if(i<2)
                j--;
            else
                j++;
            cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
            k--;                            //3,3     2,3     3,2     2,2    if i%2==0 and i<2
            cin >> VeitchDiagram[k][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                                 ^ k == -1

But you really should learn how to use a debugger.

2
votes

for i = 0 you reach this branch

else
            {
                k=3;
                if(i<2)
                    j=4;
                else
                    j=-1;
                for(int k=0; k<2; k++)
                {
                    if(i<2)
                        j--;
                    else
                        j++;
                    cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
                    k--;                            //3,3     2,3     3,2     2,2    if i%2==0 and i<2
                    cin >> VeitchDiagram[k][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                    k++;
                }
            }

when k =0

cin >> VeitchDiagram[k /* = 0  OK */][j];     //this part writes the input to elements with index (at least it should do that):
                    k--; //decrease it                            //3,3     2,3     3,2     2,2     if i%2==0 and i<2
                    cin >> VeitchDiagram[k /* here k = -1 BAD!!! */][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                    k++;
1
votes

As noted elsewhere, you're indexing ouside the array.
Just as a suggestion, a table-based version can be less tricky to get right:

const size_t k_index[] = {3,2,3,2,0,1,0,1,3,2,3,2,0,1,0,1};
const size_t j_index[] = {3,3,2,2,3,3,2,2,0,0,1,1,0,0,1,1};

int main()
{
    const int n = 4;
    int VeitchDiagram[n][n]; // No need for dynamic allocation here.

    //enter the elements
    for(int i = 0; i < n * n; i++)
    {
        cin >> VeitchDiagram[k_index[i]][j_index[i]];
    }
}

It's a couple of lines shorter as well.