user11919542
user11919542

Reputation: 11

how to scan a 2d array using a function without deleting it's content

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;
}

Upvotes: 1

Views: 103

Answers (1)

Alexey Zavodchenkov
Alexey Zavodchenkov

Reputation: 62

  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].

Upvotes: 3

Related Questions