-1

Create a program that finds the elements of the given two-dimensional array that match the input numerical values by linear search. Considering that there may be multiple matches, the number of matches should be displayed after the search is completed. Execution resul I want to do

% ./a.out
     Please enter a number: 1
     a [0] [0] is 1
     There was one 1 in the element of the two-dimensional array a
     % ./a.out
     Please enter a number: 5
     a [1] [1] is 5
     a [2] [0] is 5
     There were two 5s in the element of the two-dimensional array a
     % ./a.out
     Please enter a number: 27
     There are no 27 elements in the 2D array a

Actual execution result

./a.out
Please enter a number: 1
a [0] [0] is 1
There were 32768 1s in the elements of the two-dimensional array a
There is no 1 in the element of the 2D array a

source code

#include <stdio.h>
#define INDI 3
#define INDJ 4

int main ()
{
   int a [INDI] [INDJ] = {
     {1, 8, 11, 3},
     {9, 5, 0, 7},
     {5, 10, 4, 6},
   };
   int n;
   int i;
   int yoko;
   int tate;
   int result;
   int count;
   / * Add variable declaration as needed * /

   printf ("Enter a number:");
   scanf ("% d", & n);

   for (tate = 0; tate <3; tate ++) {
     for (yoko = 0; yoko <4; yoko ++) {
       if (n == a [tate] [yoko]) {
     count ++;
     printf ("a [% d] [% d] is% d \ n", tate, yoko, n);
     printf ("There were% d% d in the element of 2D array a \ n", n, count);
       }


     }}


   printf ("There is no% d in the element of 2D array a \ n", n);





   return 0;

}
MonSorcy
  • 3
  • 2
  • 3
    *undefined behavior* is invoked by using `count` without initializing it. "There is no..." is printed unconditionally. – MikeCAT Oct 16 '20 at 14:24
  • Does this answer your question? [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – underscore_d Oct 16 '20 at 14:31
  • Variables are not implicitly zeroed/cleared upon declaration. If you want them to be zero/clear initially, you need to initialise them. Otherwise C assumes you will assign them later and not read them before then, so it doesn't zero/clear them for you because if you were coding correctly that could be a waste of CPU. If you do, you get undefined behaviour. – underscore_d Oct 16 '20 at 14:33
  • Aside it is more idiomatic to write `printf ("There were %d %d in the element...` etc. – Weather Vane Oct 16 '20 at 14:33
  • It prints "There is no 1" because *you told it to print that*. See, right here, where it says `printf ("There is no% d ...` – user253751 Oct 16 '20 at 16:36

1 Answers1

0

I think there are some problems in your code. Here are some things that I noticed:

  1. While taking variable n as input, you are not providing correct format specifier for integer. Instead of scanf ("% d", & n); You should do scanf ("%d", &n);.
  2. You are not initializing count field to zero and incrementing it directly. I think you should initialize it before using:
int count = 0;
  1. You're not checking if element was actually found or not before printing not found message. I think you should wrap it in an if block like this:
if (count == 0) {
   printf ("There is no %d in the element of 2D array a \n", n);
}
  1. I'm not really sure if you're actually putting spaces or whether it's a side effect of copy paste. But Your printf statements should look like this:
printf ("a [%d] [%d] is %d \n", tate, yoko, n);
printf ("There were %d occurences of %d in the element of 2D array a \n", count, n);

When I applied above mentioned changes, I was able to see expected output:

src : $ ./a.out 
Enter a number:5
a [1] [1] is 5 
There were 1 occurences of 5 in the element of 2D array a 
a [2] [0] is 5 
There were 2 occurences of 5 in the element of 2D array a 
src : $ ./a.out 
Enter a number:46
There is no 46 in the element of 2D array a 
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40