I need to write a code that receive from the user numbers (must use with EOF) and store them into a matrix.
There are some more requirements:

1. Matrix must be only N*N size, while N will be defined at top.
2. If user is trying to enter more than a N*N numbers, system will throw him out with an appropriate message.

3. If user provide less than a N*N numbers, system will throw him out with an appropriate message.

4. If one of the values (or more) is not an integer, system will throw him out with an appropriate message.

5. Must add a function that will check if the matrix is a magic matrix or not.

6. The program needs to be split into a several functions (Input, Output, Printing, Testings, etc...).


As the base of the program I tried this:

#include <stdio.h>
#include <stdlib.h>
#define N 4 
 
int main()
{
  int matrix[N][N];
  static int i=0, j=0, counter=0;
  int num;

while ( (scanf("%d",&num)) != (EOF) )
  {
    
    if ( (i>N) && (j>N) )
	{
	 printf("Sliding Error !");
	 exit(0);	
	}

    else if  ( (i!=N) && (j==N) ) 
	{
	 matrix[i][j]=num;
	 i++;
         counter++;
	}
    else if ( (i==N) && (j!=N) )
	{
	matrix[i][j]=num;	
	j++;
        counter++;	
	}
  }

Next step, will be taking the whole block under the while:

if ( (i>N) && (j>N) )
	{
	 printf("Sliding Error !");
	 exit(0);	
	}

    else if  ( (i!=N) && (j==N) ) 
	{
	 matrix[i][j]=num;
	 i++;
         counter++;
	}
    else if ( (i==N) && (j!=N) )
	{
	matrix[i][j]=num;	
	j++;	
        counter++;
	}

and store it in a checking function...

But for now I just want it to work as well, because when I'll hold the matrix fill as well, I'll be able to manipulate everything I'd like to...

Please help...

Thanks !!

To satisfy these requirements

2. If user is trying to enter more than a N*N numbers, system will throw him out with an appropriate message.

3. If user provide less than a N*N numbers, system will throw him out with an appropriate message.

you need to loop simply waiting for an EOF. You don't want to do your input based on N.

4. If one of the values (or more) is not an integer, system will throw him out with an appropriate message.

Two ways to handle this.
1) Test the return value from your scanf() to see if an error occurred
2) Read all input as a string and test for digits only (use fgets() for this, do not use scanf() )

To satisfy these requirements

you need to loop simply waiting for an EOF. You don't want to do your input based on N.


Two ways to handle this.
1) Test the return value from your scanf() to see if an error occurred
2) Read all input as a string and test for digits only (use fgets() for this, do not use scanf() )

Will it work if I'll write:

while (!EOF)
{
 scanf("%d",&ch);
...
...
...
}

And then handle with other conditions by writing down calls to a testing functions (inside the while, after the scanf())? or am I missing something here ?

If not, can someone show me an example how to practically use the return value from the scanf() function? an example similar to my state...

Thank you all !!
Looking forward for an answers...

Adam.

you can use

while ( scanf( "%d", &ch) == 1 )
{
 
  ....
  ....
}

The vast majority of functions will give you some indication of success or failure.
Especially with input it's best to make sure that the call succeeded before
proceeding as if it did. In the case of scanf,
a successful call will return the number of items converted:

if (scanf("%d",&ch) == 1)
{
  /* Success */
}
else 
{
  /* Failure */
}

Will it work if I'll write:

while (!EOF)
{
 scanf("%d",&ch);
...
...
...
}

And then handle with other conditions by writing down calls to a testing functions (inside the while, after the scanf())? or am I missing something here ?

If not, can someone show me an example how to practically use the return value from the scanf() function? an example similar to my state...

Thank you all !!
Looking forward for an answers...

Adam.

Please help....

you can use

while ( scanf( "%d", &ch) == 1 )
{
 
  ....
  ....
}

The vast majority of functions will give you some indication of success or failure.
Especially with input it's best to make sure that the call succeeded before
proceeding as if it did. In the case of scanf,
a successful call will return the number of items converted:

if (scanf("%d",&ch) == 1)
{
  /* Success */
}
else 
{
  /* Failure */
}

But... If I'll use if condition, it will do it only once... I need to get an input from the user as long as he didn't hit the CTRL-D sequence, so maybe you meant:

while (!EOF)
{
  if (scanf("%d",&ch) == 1)
  {
    /* Success */
  }
  else 
  {
    /* Failure */
  }

}

??

thnx

Maybe...

What happened when you tried it?

I dint mean that buddy, I want to say this

while ( scanf( "%d", &ch) == 1 )
{
  /* on success */
  ....
  ....

}

As long as you keep inputing digits (0-9) this loop continues.

I dint mean that buddy, I want to say this

while ( scanf( "%d", &ch) == 1 )
{
  /* on success */
  ....
  ....

}

As long as you keep inputing digits (0-9) this loop continues.

But how do I need to insert the values into my matrix ?
Is it supposed to be like this:

while ( (scanf("%d",&num)) ==1 )
  {
    
    if (counter > N*N)
	{
	 printf("Sliding Error !");
	 exit(0);	
	}

    else if  ( (i!=N) && (j==N) ) 
	{
	 matrix[i][j]=num;
	 i++;
	counter++;
	}
    else if ( (i==N) && (j!=N) )
	{
	matrix[i][j]=num;	
	j++;	
	counter++;
	}
  }

??

Thanks !!

I dint mean that buddy, I want to say this

As long as you keep inputing digits (0-9) this loop continues.

I tried this:

#include <stdio.h>
#include <stdlib.h>
#define N 4 
 
int main()
{
  int matrix[N][N];
  static int i=0, j=0, counter=0;
  int num;
 
/*Receive matrix values and store them in it
according to the organ we point at the moment*/
/*===========================================================*/
while ( (scanf("%d",&num)) ==1 )
  {
    
    if (counter > N*N)
	{
	 printf("Sliding Error !");
	 exit(0);	
	}

    else if  (i!=N) 
	{
	 matrix[i][j]=num;
	 i++;
	counter++;
	}
    else if ( (i==N) && (j!=N) ) 
	{
	matrix[i][j]=num;	
	i=0;	
	j++;	
	counter++;
	}

    else if ( (i==N) && (j==N) ) 
	{
	matrix[i][j]=num;
	counter++;
	}
  }



/*Runs all over the matrix values and print them*/
/*==========================================================*/
  for(i = 0; i < N; i++)
  {
    for(j = 0; j < N; j++)
      printf("%d\t", matrix[i][j]);  
 
    printf("\n");
  }  
  

return 0;
}

But this aint working and I can't find why ?
What seems to be wrong with this code ?

Please answer because this is a very important exercise...

Thank you ! :'(

commented: NOTHING IS URGENT -- AND DON"T SHOUT!!! -3

Please answer me...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.