0

I am attempting to call an array in an order specified by the zigzag construct. Any help and insight into this is greatly appreciated. I have tried several things so I think I can be a little helpful. The project has been a handful. I can't picture how to do that. I was lastly trying to call of the struct and use but it never got picked up in the pattern I needed it to read.

I'm trying to print out the in the order of the coordinates that are in the struct. The current output I get is standard order of the array values. 10,4,2 etc. Other edits I've gotten all 1's or two of each number in standard order.
How do I get the array values to print in the order of the array locations in the struct?

#include <stdio.h>
#include <stdlib.h>

#define N 8

typedef struct _Matrix{
    double element [N][N];
} Matrix;

struct zigzag {
    int row, col;
} ZigZag[N*N] = {
    {0,0},
    {0,1},{1,0},
    {2,0},{1,1},{0,2},
    {0,3},{1,2},{2,1},{3,0},
    {4,0},{3,1},{2,2},{1,3},{0,4},
    {0,5},{1,4},{2,3},{3,2},{4,1},{5,0},
    {6,0},{5,1},{4,2},{3,3},{2,4},{1,5},{0,6},
    {0,7},{1,6},{2,5},{3,4},{4,3},{5,2},{6,1},{7,0},
    {7,1},{6,2},{5,3},{4,4},{4,5},{2,6},{1,7},
    {2,7},{3,6},{4,5},{5,4},{6,3},{7,2},
    {7,3},{6,4},{5,5},{4,6},{3,7},
    {4,7},{5,6},{6,5},{7,4},
    {7,5},{6,6},{5,7},
    {6,7},{7,6},
    {7,7}
};

int data[N*N];

int* ZigZagOrdering(Matrix x) // fill data and return data
{
    int i;
    int j;
    struct zigzag zigzag;
    int k;

    for(i=0; i<N; i++)
    {
        for (j=0; j<N; j++)
        {
            for (k=0; k<N; k++)
            {
                 printf ("%g ",x.element[i][j]);
             }
             printf ("%d " , zigzag.row[k]);
        }// innerfor
        printf("\n");
    }//outer for
}//ZZ 



int main(int argc, char **argv)
{
    // special case that allows us to initialize this way
    Matrix C =    {10,  4, 2,  5, 1, 0, 0, 0,
                    3,  9, 1,  2, 1, 0, 0, 0,
                   -7, -5, 1, -2, -1, 0, 0, 0,
                   -3, -5, 0, -1, 0, 0, 0, 0,
                   -2,  1, 0,  0, 0, 0, 0, 0,
                    0,  0, 0,  0, 0, 0, 0, 0,
                    0,  0, 0,  0, 0, 0, 0, 0,
                    0,  0, 0,  0, 0, 0, 0, 0};

    // encoding
    // EncodeRunlength("output.dat", C);
    //decoding
    // DecodeRunlength("output.dat");
    ZigZagOrdering(C);
}//main
Mike
  • 4,041
  • 6
  • 20
  • 37
  • 1
    Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon, and more importantly, about how to create an MCVE ([MCVE]). It isn't clear from your question what output you are getting, or what output you are expecting. Your indentation is a bit random. Leaving comments about code that isn't part of your question isn't helpful (though as it's only 4 lines, it isn't as horrible as some questions). – Jonathan Leffler Sep 17 '18 at 05:37
  • Note that you should not create function, variable, tag or macro names that start with an underscore, in general. [C11 §7.1.3 Reserved identifiers](https://port70.net/~nsz/c/c11/n1570.html#7.1.3) says (in part): — _All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use._ — _All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces._ See also [What does double underscore (`__const`) mean in C?](https://stackoverflow.com/a/1449301) – Jonathan Leffler Sep 17 '18 at 05:38
  • 3
    That ZigZag being formatted that way is probably throwing you off. Also, try putting the struct definition, and variable (array) definition as separate statements to make it clearer. The array is simply a list of coordinates for NxN 2D array, specifying the order in which the elements should be accessed. Nothing more complex about that here, I think. I think it is assumed there are no duplicates in coordinate pairs, and therefore each element in array will get accessed. – hyde Sep 17 '18 at 05:40
  • 2
    I suspect that your `ZigZagOrdering()` function should be using two (not three) nested loops, and you should be printing `printf("%g ", x.element[ZigZag[i].row][ZigZag[i].col]);` or thereabouts (the subscript might need to be calculated in a more complex way than that — may `i * N + j` instead of just `i`). It isn't 100% clear, though (but the array name is definitely `ZigZag` and not `zigzag`). You might even just use a single loop and `for (i = 0; i < N * N; i++)` and then the simple `i` subscript I wrote first. As it stands, you print 512 values, not 64. – Jonathan Leffler Sep 17 '18 at 05:42
  • What do you mean by "call an array"? I only know how to declare, define, initialise and array and how to read or write an entrie of an array. – Yunnosch Sep 17 '18 at 05:52
  • 1
    What is your problem? What is the question? – Yunnosch Sep 17 '18 at 05:54
  • Your question might give a less confusing impression if you delete the information-free "I have no clue." part ("Any help and insight into this is greatly appreciated. I have tried several things so I think I can be a little helpful. The project has been a handful. I can't picture how to do that.") and replace it with some actual explanation. You could e.g. describe what "several things" you have tried and in which way they did not work (instead of just saying you did, imagine the answer of a teacher to "I did my homework, honest.") . What do you mean by " call of the struct and use"? – Yunnosch Sep 17 '18 at 05:59
  • Good morning all, I have updated the question. My apologies when I asked this it was 2 am and I had been up since 3 am the previous day. Basically ZigZag holds the order that the printf is supposed to show the values of the Matrix in. Currently when I run it I have gotten the following output 1) The array values printing in the order you see in Matrix C so 10,4,2,5,1, etc... or all 1's. How do I use the struct ZigZag to have the values of Matrix C print in that order? Thank you – Tyreese Davis Sep 18 '18 at 12:28

1 Answers1

0

I think you are simply supposed to iterate through the Matrix x in the order defined by ZigZag. So your code becomes quite a bit simpler. I have no idea what your actual function is supposed to return, so I changed it to return nothing.

void ZigZagOrdering(Matrix x)
{
    // iterate through every item in ZigZag array
    for(int i=0; i < N*N; i++)
    {
        // temp variables to make code easier to read and less repetitive
        int row =  ZigZag[i].row;
        int col = ZigZag[i].col;
        // print row and column from ZigZag, and value in x at that position
        print("row %d column %d value %g\n", row, col, x[row][col]);
    }
}

I hope this function makes you understand what is needed, so you can write your actual function. If something is unclear about this, just ask in a comment, but tf you need to ask further questions about how to return values etc, please work on your code first, then ask a new question with the new code showing the new problem.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • Thank you for your response it's helped me work on my code. I guess the thing that's kind of frustrating is I did something similar to this before and the for loop wouldn't run. I appreciate the help from you and everyone who took time to respond. I'm still learning and the book I have for this particular course has been less than useless. – Tyreese Davis Sep 18 '18 at 16:40