-3

I have two multi-dimension arrays

num[line][col] = {
        { '\0', '1', '\0', '\0', '\0' },
        { '\0', '\0', '\0', '3', '\0'  },
        };  
solution[line][col] = {
        { '5', '1', '3', '4', '2' },
        { '4', '2', '5', '3', '1'  },};
  1. How do I write in num? 2.How do I compare num and solution? Thank you so much
MD XF
  • 7,860
  • 7
  • 40
  • 71
MBV
  • 3
  • 2

2 Answers2

0

Based on your example, I'll doit like this:

#include <stdio.h>

#define line 2
#define col 5

int main(void){
int num[line][col] = {
        { '1', '1', '1', '1', '1' },
        { '1', '1', '1', '1', '1' },
    };

int solution[line][col] = {
        { '1', '1', '1', '1', '1' },
        { '1', '1', '1', '1', '1' },
    };


    for (size_t i = 0; i < line; ++i)
    {
        for (size_t j = 0; j < col; ++j)
        {
            if (num[i][j] != solution[i][j])
            {
                printf("Not equal\n");
                return 1;
            }
        }
    }
    printf("are equal\n");
}

Output:

are equal
Michi
  • 5,175
  • 7
  • 33
  • 58
0

Try using memcmp to compare num and solution:

char num[line][col] = {
    { '\0', '1', '\0', '\0', '\0' },
    { '\0', '\0', '\0', '3', '\0'  },
    };  
char solution[line][col] = {
    { '5', '1', '3', '4', '2' },
    { '4', '2', '5', '3', '1'  }, };

int main(void)
{
    printf("Comparing num[][] to solution[][]...\n");
    int result = memcmp(num, solution, 10);

    if (result != 0)
        printf("Not equal.\n");
    else
        printf("Equal.\n");

    return 0;
}
MD XF
  • 7,860
  • 7
  • 40
  • 71
  • I wasn't positive about what to put as the 3rd argument in `memcmp`. If 10 is wrong, feel free to correct me. – MD XF Dec 07 '16 at 22:05
  • Down Voter please stop acting like a coward and Explain your reasons. – Michi Dec 07 '16 at 22:06
  • You know, I don't mind the downvote (as I'm not a rep-junkie), but I don't understand what's wrong with my solution. It answers the question, it's clear and concise, and it uses proper programming technique. Explain, please. – MD XF Dec 07 '16 at 22:08
  • `as I'm not a rep-junkie` hmmm.... then you shouldn't have answered this in the first place. BTW: don't hardcode `10` - `sizeof` is much better. – Support Ukraine Dec 07 '16 at 22:30
  • @4386427 Exactly my thoughts, but I figured I'd rather hardcode it than use `sizeof` incorrectly. How could I use it in this instance? – MD XF Dec 07 '16 at 22:30
  • You shouldn't have answered in the first place. However, if you do, you better make a perfect one. Oh, BTW - upvoting each other answers will sooner or later get you in to trouble. – Support Ukraine Dec 07 '16 at 22:35
  • @4386427 I did not ask the other user to upvote my answer. I simply noticed that his answer was a valid, good response, and upvoted it. Whether or not he returned the vote was 100% his choice and doing, and I did not vote his in hopes of getting reputation. – MD XF Dec 07 '16 at 22:35
  • @4386427 I really *don't* care so much about the reputation. I don't *mind* the points, but they're not why I'm here. I simply enjoy sharing my knowledge with the rest of the community. – MD XF Dec 07 '16 at 22:36
  • @4386427 If it makes this much of a difference in your mind, I'll edit the answer and request that Michi reverses his vote. – MD XF Dec 07 '16 at 22:38
  • I don't care what this particular answer is. I care about learning you not to answer this kind of questions. SO is not a "write my code" service. – Support Ukraine Dec 07 '16 at 22:40
  • @4386427 So punish good users for answering bad questions? Is that what you're saying? – MD XF Dec 07 '16 at 22:41
  • Yes - exactly. Don't answer questions which shows not effort from OP at all. That is not welcome at SO. – Support Ukraine Dec 07 '16 at 22:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130052/discussion-between-md-xf-and-4386427). – MD XF Dec 07 '16 at 22:42
  • @4386427 This means that [This Question shows a lot of effort](http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c) ? At least the OP showed 7 lines of Code. – Michi Dec 09 '16 at 11:12