1

as a homework assignment, i need to scan N matrices and a user input integer, and scan if any of the matrices values contains that number without using pointers.

as soon as i finish scanning the array and exits the function, the content of the array resets to zero, or trash if i dont init the array.

#pragma warning(disable:4996)

#include<stdio.h>

#define N 2

int exist(int matrix[][N], int elem);

void input_matrix(int matrix[][N], int size);


void main()
{
    int matrix_1[][N] = { 0 }, matrix_2[][N] = { 0 }, matrix_3[][N] = { 0 };

    int elem;
    printf("please enter values of squared matrix:\n");
        input_matrix(matrix_1[][N], N);
        //(input_matrix(&matrix_2[N][N]));
    //  (input_matrix(&matrix_3[N][N]));
    printf("please enter number to search for in the matrix:\n");
    scanf("%d", &elem);
    if (exist(matrix_1,elem))
        //printf("exist.");//the next part of h.w to be written when input func works

}

void input_matrix(int matrix[][N], int size)//something here fishy
{
    int i, j;
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            scanf("%d", &matrix[i][j]);
        }
    }
}
int exist(int matrix[][N], int elem)
{
    int i, j;
    int flag = 0;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            if ((matrix[i][j]) == elem)
            {
                flag = 1;
                break;
            }
        }
    }
    return flag;
}
  • Try `for (j = 0; j < size; j++)` --> `for (j = 0; j < N; j++)`. – chux - Reinstate Monica Aug 12 '19 at 22:16
  • 1
    The compiler doesn't know how many rows there are when you initialize your arrays like `matrix_1[][N] = { 0 };`. Please see [this](https://stackoverflow.com/questions/22346426/omitting-sizes-while-initializing-c-c-multidimensional-arrays). The compiler counts the number of subarrays to determine the first dimension of the array. – Quaxton Hale Aug 12 '19 at 22:26
  • You can do it without any variables declared with explicit pointer declaration syntax, which I guess is what you're after, but you cannot do it without using pointers at all. The indexing operator (`[]`) is a *pointer* operator, after all, and you cannot receive an array as a function parameter, even if the declaration syntax disguises that. – John Bollinger Aug 12 '19 at 23:15
  • 1
    The compiler *does* know how many rows, @QuaxtonHale. There are just enough to accommodate all the elements of the initializer, which is 1. That's not what the OP appears to want, but there's no uncertainty. – John Bollinger Aug 12 '19 at 23:26
  • @JohnBollinger is right. I meant to point out OP's intent. – Quaxton Hale Aug 12 '19 at 23:41

1 Answers1

3
  1. Inside the main function, in the call input_matrix(matrix_1[][N], N) you pass the invalid parameter. Instead should pass the whole matrix, like input_matrix(matrix_1, N).
  2. As noted in a comment, it will be better to declare matrix like matrix_1[N][N].