0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N_MAX 10
#define N_MIN 2
#define TEMPSIZE 1024

float RowSum(float **matrix, int sizearray,int row) {
    float sum;
    for (int j=0 ; j <= row; j++) {
        sum = 0;
        for (int i=0 ; i < sizearray; i++) {
            sum = sum + matrix[j][i];
        }
    }
    return sum;
}

float ColSum(float **matrix, int sizearray, int col) {
    float sum;
    for (int j = 0; j <= col; j++) {
        sum = 0;
        for (int i = 0; i < sizearray; i++) {
            sum = sum + matrix[i][j];
        }
    }
    return sum;
}

int RepNum(int arraysize, float **matrix) {
    int i, j, counter = 0;
    int temparray[N_MAX*N_MAX];
    for (i = 0; i < N_MAX*N_MAX; i++) {
        temparray[i] = 0;
    }
    for (i = 0; i < arraysize; i++) {
        for (j = 0; j < arraysize; j++) {
            temparray[(int)matrix[i][j]]++;
        }
    }
    for (i = 1; i < arraysize*arraysize; i++) {
        if (temparray[i] > 1)
            counter++;
    }
    return counter;
}


void PrintArray(float **matrix, int arraysize) {
    for (int i = 0; i<arraysize; i++)
    {
        for (int j = 0; j<arraysize; j++)
            printf("%3d ", (int)matrix[i][j]);
        printf("\n");
    }
}

int CheckInt(float **matrix, int arraysize) {
    int counter = 0;
    for (int i = 0; i < arraysize; i++) {
        for (int j = 0; j < arraysize; j++) {
            if (((int)matrix[i][j]) != matrix[i][j])
                counter++;
        }
    }
    return counter;
}

void main() {
    printf("Hello! this program will help you to find out if you have a magic square!\nPlease enter your matrix order and following it the numbers you'd like to check: \n");
    float **matrix;
    char input[TEMPSIZE];
    int sizear = 0;
    float correctsum = 0.0;
    int counter = 0, row, column;
    fgets(input, TEMPSIZE, stdin);
    char* str = strstr(input, " ");
    if (str == 0)
        return;
    str[0] = 0;
    str++;  
        sizear = atof(input);
        if (sizear > N_MAX || sizear < N_MIN)
        {
            printf("ERROR: The matrix size cannot be %d size. \n", sizear);
            exit(0);
        }
        matrix = (float**)calloc(1, sizear * sizeof(float*));
        for (column = 0; column < sizear; column++)
        {
            matrix[column] = (float*)calloc(1, sizear * sizeof(float));
        }
        for (row = 0; row < sizear; row++)
        {
            for (column = 0; column < sizear; column++)
            {
                char* temp = strstr(str, " ");
                if (temp == 0) /*gets the last number*/
                {
                    ++counter;
                    matrix[row][column] = atof(str);
                    goto end;
                }
                if (atof(temp) <= sizear*sizear && atof(temp) > 0) { /*puts all numbers in matrix*/
                    temp[0] = 0;
                    matrix[row][column] = atof(str);
                    str = temp + 1;
                    ++counter;
                }
                else {
                    printf("you cannot enter the number %.3f \n", atof(temp));
                    exit(0);
                }
            }
        }

    end:
        if (counter > sizear*sizear) {
            printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
        }
        else if (counter < sizear*sizear) {
            printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
        }
        else if (counter == sizear*sizear) {
            correctsum = (float)((sizear*(sizear*sizear + 1)) / 2);
            float row = RowSum(matrix, sizear, 0);
            float coul = ColSum(matrix, sizear, 0);
            if (row == coul && row== correctsum && coul==correctsum && RepNum(sizear, matrix) ==0 && CheckInt(matrix,sizear)==0) {
                printf("It's a magic matrix!\n");
                PrintArray(matrix, sizear);
            }
            else {
                printf("Not a magic square:\n");
                if (row != coul && row != correctsum && coul != correctsum) {
                    printf("* Coloums and rows sum do not match.\n");
                }
                if (RepNum(sizear, matrix) != 0) {
                    printf("* There are repeating numbers.\n");
                }
                if (CheckInt(matrix, sizear) != 0) {
                    printf("* One of the numbers or more you've entered isn't integer.\n");
                }
            }
        }
        for (column = 0; column < sizear; column++)
        {
            free(matrix[column]);
        }
        free(matrix);
    }

When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it? The exercise asked to check if the input is a magic square (sum rows=columns=diagonals), making sure there are no duplicates, only int numbers and that I don't enter more or less than sizearrayXsizearray numbers. The input from the user should look like 3 1 2 3 4 5 6 7 8 9 where the first number (here it is 3) is the array size and the rest are the numbers that would be checked as a magic square.

Nadia
  • 3
  • 3
  • I got as far as `sizear = atof(input);` But `int sizear` is taking result of `double atof()`. How does a magic square need to use `float` and `double` types? – Weather Vane Dec 05 '17 at 21:04
  • They asked us to make sure that all the numbers are only int, not float or double etc... so the way to make sure is to convert the float number into int and compare them. – Nadia Dec 05 '17 at 21:08
  • Yes, but why is the matrix of `float` types? Work with `int`. Later, if you want to check the user input in case a`float` was entered then it's a separate exercise, nothing to do with the magic square. Such as [Check if input is integer type in C](https://stackoverflow.com/questions/4072190/check-if-input-is-integer-type-in-c). – Weather Vane Dec 05 '17 at 21:12
  • Your `...Sum` functions calculate the sum of each row/col, discards it and returns the sum of the last row/col. Is this intended? – mch Dec 05 '17 at 21:17
  • @mch I've figured that comparing one row and column to the mathematical calculation of a number square base on the size: correctsum = (float)((sizear*(sizear*sizear + 1)) / 2); is enough. – Nadia Dec 05 '17 at 21:26
  • @WeatherVane ok, got it. May I ask why is it important if my matrix is int and not float? – Nadia Dec 05 '17 at 21:31
  • Use the appropriate type for the situation. If a magic square contains integers, then use an integer type. [Reductio ad absurdum](https://en.wikipedia.org/wiki/Reductio_ad_absurdum) – don't use a `double` where you need `bool`. – Weather Vane Dec 05 '17 at 21:40
  • @WeatherVane changed it to int. But my counter still doesn't reach beyond arraysaizeXarraysize – Nadia Dec 06 '17 at 00:29

1 Answers1

0

When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?

Sure, it's simple: the counter is incremented in the loops

        for (row = 0; row < sizear; row++)
        {
            for (column = 0; column < sizear; column++)
            {
                …
            }
        }

- the inner statements are run through (at most) sizear × sizear times, so the counter, starting from 0, cannot reach a higher value.

Armali
  • 18,255
  • 14
  • 57
  • 171