Answer :
C++ Code:
#include <iostream>
#include <fstream>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int findMin(int numbers[], int i, int minx, int n){
for (int j = i+1; j < n; j++)
if (numbers[j] < numbers[minx])
minx = j;
return minx;
}
void selectionSort(int numbers[], int n)
{
int i, j, minx;
for (i = 0; i < n-1; i++)
{
minx = i;
minx = findMin(numbers, i, minx, n);
swap(&numbers[minx], &numbers[i]);
}
}
int binarySearch(int numbers[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (numbers[mid] == x)
return mid;
if (numbers[mid] > x)
return binarySearch(numbers, l, mid-1, x);
return binarySearch(numbers, mid+1, r, x);
}
return -1;
}
int median(int numbers[], int n){
int m = 0;
if ((n % 2) == 0)
m = (numbers[n/2] + numbers[(n/2) - 1])/2.0;
else
m = numbers[n/2];
return m;
}
void showArray(int numbers[], int n){
for(int i = 0; i < n;i++)
cout << numbers[i] << " ";
}
int main()
{
int n;
fstream myfile("data.dat", ios_base::in);
int size = 50;
int a[size], i = 0;
while (myfile >> n)
{
a[i] = n;
i++;
}
selectionSort(a, size);
int result = binarySearch(a, 0, size-1, 7);
if(result == -1)
cout << "Number not found\n";
else
cout << "Number found at " << result <<
endl;
cout << "Median of the array is " << median(a,
size)<< endl;
showArray(a, size);
return 0;
}
The name of the C++ file must be search.cpp Write a program that will read data...
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...
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...
Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...
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: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...
JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...
C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...
This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...
Basic C program Problem: In this assignment, you have to read a text file (in.txt) that contains a set of point coordinates (x,y). The first line of the file contains the number of points (N) and then each line of the file contains x and y values that are separated by space. The value of x and y are an integer. They can be both negative and positive numbers. You have to sort those points in x-axis major order and...
In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...
You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...