In C++
1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array.
2. The program should then ask the user for a number to search for in the array:
3. The program should also display the elements of the array on the screen:
4. The program should have a minimum of 3 functions (not including main), possible functions are:
Sample Run
Please enter 10 numbers:
12
23
45
65
78
98
41
21
54
87
The items entered into the array are:
12 23 45 65 78 98 41 21 54 87
The items in the array before the search:
12 21 23 41 45 54 65 78 87 98
What number do you want to search for?
45
the item was found at index: 4
//C++ code for performing binary search after sorting the given array
#include<iostream>
using namespace std;
void fill_array(int array[],int size) //function to fill
array
{
for(int i=0;i<size;i++) //loop to read array elements
cin>>array[i];
}
void print_array(int array[],int size) //function to print array
elements
{
for(int i=0;i<size;i++) //loop to display array elements
cout<<array[i]<<" ";
}
void sort (int array[], int size ) //function to sort the
array
{
int temp;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(array[i]>array[j]) //swap the adjacaet elements
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
}
int binarySearch(int array[],int low,int high,int key)//function to
search given number key
{
if (low<=high)
{
int mid=(low+high)/2;
if (array[mid]==key)
return mid;
else if(array[mid]>key)
return binarySearch(array,low,mid-1,key);
else
return binarySearch(array, mid + 1, high, key);
}
return -1;
}
int main()
{
int array[10],size=10,number,found;//declarations
cout<<"Please enter 10 numbers :"<<endl;
fill_array(array,size);
cout<<"\nThe items entered into the array are
:"<<endl;
print_array(array,size);
cout<<"\nThe items in the array before search :
"<<endl;
sort(array,size);//function to sort array elements to perform
binary search
print_array(array,size);
cout<<"\nWhat number do you want to search for ?
"<<endl; //to read number value
cin>>number;
int index = binarySearch(array,0,size-1,number);//function
call
if(found==-1)
cout<<"The item was not found in the array";
else
cout<<"The item was found at index : "<<index;
return 0;
}
In C++ 1. Write a program that allows a user to enter 10 integer values from...
The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...
In C++ Write a program that lets the user enter at least 10 values into an array. The program then display the largest and smallest values stored in the array.
Write a program that lets the user enter at least 10 values into an array. The program should then display the largest and smallest values stored in the array. Do it in C++
Write a C++ program to allow an user to enter username and password, and then compare the entered values with the values which stored in a file called login.txt. Display an message "Login successful" if the values are matched, otherwise display "Login fail!. This program allow the user to have three times of tries, if exceeded the number of times, display a message "Number of times is exceeded, please contact administrator (Assume, the stored username is "INTI-IU and password is...
IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...
in Python: Write a program that allows the user to enter a number between 1-5 and returns an animal fact based on what the user entered. The program should 1. Continue to run until the user enters “quit” to terminate the program, and 2. Validate the user’s input (the only acceptable input is 1, 2, 3, 4, 5 or “quit”). You can use the following animal facts: An octopus is a highly intelligent invertebrate and a master of disguise. Elephants...
Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...
In C++ Write a program that allows the user to enter eight integer values. Display the values in the reverse order of the order they were entered. Name the file ReverseNumbers.cpp.
C++ Problem #1 Write a program that prompts user to enter an integer up to 1000 and displays back a message about the number of digits in this number and if number is odd or even. For example, if user enters 93 message back should be "93 has 2 digits and is odd".
in C++ You are to define a structure containing the id number, three test scores, and their average, for each student in a class of 20. The file pr2data.txt is included and contains the data. The program is to include the following: A function that reads 20 records of data from the file, finds the average for each student and prints each student’s record. A function that lists the ids and averages for all the students whose average is above...