0

I want to directly input a 2-D Array in C, separated just by single spaces and newlines. At the same time I also want to verify whether the user is entering a valid single digit integer (either positive or negative). I tried the following.

int A[3][3];
    int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            scanf("%d",&A[i][j]);
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d ",A[i][j]);
        printf("\n");
    }

But I want 2 things in this:-

  1. Input that is first verified whether its a single digit integer or not.
  2. Not works merely for positive integers and 0 but also for negative integers.

eg. input such as the following should be accepted and stored in 2-d array

1 2 -3 4
-5 1 2 -6
1 1 2 3  

I'm sorry if I wasn't clear. An important component is that the input should be confirmed whether its integer or not i.e. the input should be either as char or string and if it is an integer (which can be ascertained by functions such as isdigit() , then it should be converted to an integer value. This following code chunk works for single positive values

char c = getchar(); 
  if (isdigit(c))
     int value = c - '0';

However, I don't know how to enable this functionality for negative integers in a complete 2D array input.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • You could check the result of scanf. It should return 1 upon a successful read. If you need more detailed input validation, then read the whole input as a string with fgets. – Lundin Jun 03 '20 at 08:52
  • So, Input could be any **ASCII** character. Isn't it? – Shubham Jun 03 '20 at 09:43
  • @Lucas yes, any ASCII character including newline and whitespace. The goal is to extract the correctly formatted integer values, positive or negative, from the input. – Mohammad Sally Jun 03 '20 at 10:05
  • @MohammadSally So, you need to know that there isn't any negative value in ```ASCII``` all starts from 0 to 255. Refer [here](http://www.asciitable.com/). – Shubham Jun 03 '20 at 10:27
  • and what you are doing in this line ```int value = c - '0';``` is converting a ```character digit``` into its integer value. It only works with the strings of digits or character digits. It takes the ASCII value of ```c``` and subtracts ASCII value of ```0``` from it. – Shubham Jun 03 '20 at 10:30
  • A negative number if you are assuming it as a character, is not a character. It is a string of two characters ```'-'``` and ```5(say)```. – Shubham Jun 03 '20 at 10:31
  • @Lucas Thanks for the clarification. So, how do I go about this? – Mohammad Sally Jun 03 '20 at 10:34
  • You need to check this post [link](https://stackoverflow.com/a/61144505/12180605) – Shubham Jun 03 '20 at 10:34
  • You can only store +ve values in this array. @MohammadSally – Shubham Jun 03 '20 at 10:36
  • -ve numbers aren't characters. They are stored as signed binary numbers(bits) internally. – Shubham Jun 03 '20 at 10:40
  • https://stackoverflow.com/questions/51872086/how-to-handle-negative-integer-when-converting-them-to-strings Maybe It can help you. – Shubham Jun 03 '20 at 10:43

2 Answers2

1

If you want to verify that a number is a single digit, just check if its between -9 and 9.

As for negative numbers, im pretty sure the %d modifier for the scanf captures that.

Please comment down below if the answer is incorrect so i could fix it :)

Edit: i forgot to note that scanf returns the number of read elements. so comparing this against 1 (since you read one element at a time) will allow you to know if the input was partial or not. something like this:

if (scanf(" %d", &A[i][j]) != 1){ 
   //here goes the code for when the input is partial
}

should do the trick

nir shahar
  • 328
  • 3
  • 13
0

So first, tell the user to enter values row-wise or column-wise. In this case, ask like this

printf("Enter integers row-wise");

Now the user will enter values.

After scanning those values put an if statement to verify that value is greater than or equal to -9 and less than 9. If value is within the range continue; else put a exit(1); statement and tell the user that entered value is incorrect. Like this:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int A[3][3];
    int i, j;
    for( i = 0; i < 3; i++ )
    {
        for( j = 0; j < 3; j++)
        {
            scanf("%d", &A[i][j]);
            if(A[i][j] >= -9 && A[i][j] <= 9)
                continue;
            else
            {
                printf("Enter correct values.\n");
                exit(1);
            }
        }
    }


    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d ",A[i][j]);
        printf("\n");
    }
    return 0;
}

Shubham
  • 1,153
  • 8
  • 20
  • your code will always reach the ```continue``` since every number is either bigger than 0, or equal to 0 or smaller than 0... – nir shahar Jun 03 '20 at 09:09