How to search an element in a two-dimensional array and travel to that element using the C language? |
Problem: How to search an element in a two-dimensional array and travel to that element using C language.
How to search an element in two dimensional array?
#include<stdio.h> #include<conio.h> int main(){ int rows, columns, srchElement, count=0; printf("Enter the number of Row and Column:"); scanf("%d %d", &rows, &columns); int array[rows][columns]; printf("Enter %d elements: \n", (rows*columns)); for(int i=0; i<rows; i++){ for(int j=0; j<columns; j++){ scanf("%d", &array[i][j]); } } printf("Enter the element to get the position:"); scanf("%d", &srchElement); for(int i=0; i<rows; i++){ for(int j=0; j<columns; j++){ if(array[i][j] == srchElement){ printf("(%d, %d) \n", i, j); count++; } } } if(count==0) printf("Not found \n"); return 0; } *****OUTPUT***** Enter the number of Row and Column:3 3 Enter 9 elements: 14 16 17 18 14 13 19 18 16 Enter the element to get the position:14 (0, 0) (1, 1) |
How to search an element in two dimensional array?
#include<stdio.h> #include<conio.h> int main() { int marks[10]={37,56,76,54,34,87,98,67,44,66}; int s,m=0; printf("Student Marks are given below\n"); for(int i=0;i<=9;i++) printf("%d ",marks[i]); printf("\nEnter Your Marks Search:"); scanf("%d",&s); for(int j=1;j<=9;j++) { if(s==marks[j]) { printf("student Marks Found at Position:%d",j+1); m=1; } } if(m==0) printf("Student Marks Not Found"); } *****OUTPUT***** Student Marks are given below 37 56 76 54 34 87 98 67 44 66 Enter Your Marks Search:98 student Marks Found at Position:7 |
How to Travel element in two dimensional array?
#include<stdio.h> #include<conio.h> int main() { int arr[3][3]={{10,20,30},{60,70,50},{77,55,44}} printf("The Array has given below\n"); for(int i=0;i<=2;i++) { for(int j=0;j<=2;j++) { printf("%d ",arr[i][j]); } printf("\n"); } printf("Travel of Array is given below\n"); for(int i=0;i<=2;i++) { for(int j=0;j<=2;j++) { printf("%d ",arr[j][i]); } printf("\n"); } } *****OUTPUT***** The array has given below 10 20 30 60 70 50 77 55 44 Travel of Array is given below 10 60 77 20 70 55 30 50 44 |
How to Binary Search element in single dimensional array?
#include<stdio.h> #include<conio.h> int main() { int arr[100]; int n,x,first,last,mid,f=0; printf("Enter Size of Array:"); scanf("%d",&n); printf("Enter %d integer values one by one:\n",n); for(int i=0;i<n;i++) scanf("%d",&arr[i]); printf("Enter the Element to Search:"); scanf("%d",&x); first=0; last=n-1; while(first<=last) { mid=(first+last)/2; if(x==arr[mid]){ f=1; break; } else if(x>arr[mid]) first=mid+1; else last=mid-1; } if(f==1) printf("The Number is found at Position:%d",mid+1); else printf("Number not found"); } *****OUTPUT***** Enter Size of Array:5 Enter 5 integer values one by one: 56 43 76 34 23 Enter the Element to Search:76 The number is found at Position:3 |