Write a program in C++ that input 10 numbers from the user in one dimensional array and search a data (input by user) in the array.
Answers
Kindly mark the answer as brainliest if you find it useful.
The searching technique used here is binary search which is fast technique. So first let's see what binary search really is.
Binary Search in C++
To perform binary search or to search an element using binary search in C++ Programming, you have to ask to the user to enter the array size then ask to enter the array elements.
Now ask to enter an element that is going to be search to start searching that element using binary search technique and display the position of the element on the screen if found as shown here in the following program.
/* C++ Program - Binary Search */ #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle; cout<<"Enter total number of elements :"; cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{ first = middle + 1; }
else
if(arr[middle] == search)
{
cout<<search<<" found at location "<<middle+1<<"\n";
break; }
else
{
last = middle - 1;
}
middle = (first + last)/2; }
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the list.";
}
getch();
}
Answer:
KnowledgeBoat Logo
Computer Applications
Write a program to input 10 integer elements in an array and sort them in descending order using bubble sort technique.
Java
Java Arrays
ICSE
16 Likes
ANSWER
import java.util.Scanner;
public class KboatBubbleSortDsc
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = 10;
int arr[] = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
//Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}