Question

Write a C program that is uses a function to search for a specific value in...

Write a C program that is uses a function to search for a specific value in a two dimensional array using quick search

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The answer to the question is given below.

For the fast search, the 2-D array has to be sorted over rows and columns wise.

The C code for the problem is given below.


#include<stdio.h>
int search(int row, int column,int arr[][column],int key)
{
int min=arr[0][0]; /* taking starting element as minimum */
int max=arr[row-1][column-1]; /* bottom right element is maximum */
if(key<min || key>max) /* if key is not between min and max then it is not available in array */
{
return -1;  
}
int i=0;
int j=column-1;
while(i<row && column>=0) /* traversing the matrix */
{
    if(arr[i][j]==key)
    {
    printf("Key is found at index %d %d",i,j); /* when found the key, displaying its index */
   return 1;  
   }
   if(arr[i][j]>key) /* if key is larger than any element than we will back to previous column, because matrix is sorted column wise and row wise*/
   {
       j--;
   }
   else /* if key is smaller than any element than we will back to previous row */
   i++;
}
printf("\n Element is not in the array"); /* after traversing the complete matrix if we get nothing, then element is not in the array */
return 0;
}
int main()
{
   int row,column;
   printf("Please Enter The Rows and Columns: ");
   scanf("%d%d",&row,&column);
   int i,j;
   int arr[row][column];
   printf("Please Enter The Elements of 2D array:\n");
   for(i=0;i<row;i++)
   {
       for(j=0;j<column;j++)
       {
       scanf("%d",&arr[i][j]);
       }
   }
   printf("Enter the Element You want to Search: ");
   int key;
   scanf("%d",&key);
   int result=search(row,column,arr,key); /* calling the search function */
   if(result==1)
   {
       printf("\nThank You for using the program");
   }
   else
   {
       printf("\nSearching Element should be in the list");
   }
   return 0;
}



The screenshot of the running code is given below.

1 #include<stdio.h> 2 int search(int row, int column, int arr[][column], int key) 30{ 4 int min=arr[0][0]; int max=arr[row-1]29 30 31 33 int main() { int row, column; printf(Please Enter The Rows and Columns : ); scanf(%d%d,&row,&column); int i,




The screenshot of the sample input-output is given below.


Please Enter The Rows and Columns: 4 4 Please Enter The Elements of 2D array: 10 20 30 40 15 25 35 45 27 29 37 48 32 33 39 50






If the answer helped please upvote it means a lot. For any query please comment.

Add a comment
Know the answer?
Add Answer to:
Write a C program that is uses a function to search for a specific value in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Q 3-) (30 points) Write a C program (MAIN function) and a FUNCTION to create and...

    Q 3-) (30 points) Write a C program (MAIN function) and a FUNCTION to create and print the transpose of a two- dimensional array. Within C program (the MAIN function); Declare a two- dimensional 3X2 integer array A and initialise with the values (1,2), (3,4), (5,6), declare also a two-dimensional 2X3 integer array B and initialise it with zero. Call the FUNCTION and pass arrays A and B to the FUNCTION as arguments. Print the array A and its transpose....

  • In C++ for 2D Array: Write a program that uses a function that manipulates a 2D...

    In C++ for 2D Array: Write a program that uses a function that manipulates a 2D array of integers as follows: 1. Fill the first row and column in the array with random bits 2. Every subsequent cell should be filled as follows: 1. If the cells that are to the left and top of it are equal, then you should store their sum in it b. Otherwise, your program should store the highest among them. Test your function using...

  • Write and test a function in C++ that uses the binary search algorithm to search an...

    Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array: string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",         "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",         "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",         "James, Jean", "Weaver, Jim", "Pore, Bob",...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • Problem: Write a C++ program that does the following : Accepts a positive integer ( n...

    Problem: Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following In the first function: display elements of the array. Display the first 20 elements If the size is > 20 In the second function : Using recursion, Search for a...

  • Write a C program with a function that displays a list of months, the number of...

    Write a C program with a function that displays a list of months, the number of days in each month, and the month with the least amount of days.   Use a two-dimensional character array for the months and a one-dimensional integer array for the number of days in each month. Include another function that returns the index of the month with the least amount of days. Function Prototypes: void DisplayMonths( char months[ROW][COL], int days[ ROW ] ); int min( int...

  • Write a C program (not C++) that calls a function to multiply a matrix (two dimensional...

    Write a C program (not C++) that calls a function to multiply a matrix (two dimensional array). The main program should ask the user to enter an integer that will be used as the matrix “adder” and then call the function. The function should perform a matrix increment (every element of the array is incremented by the same value) and assign the result to another array. The function should be flexible enough to work with any array size and “adder”,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT