Need help on c++ homework. Thanks Need help updating code from previous assignme
ID: 3607663 • Letter: N
Question
Need help on c++ homework. Thanks
Need help updating code from previous assignment to this assignment above.
here is the code from previous assignment below
#include <iostream>
#include <stdlib.h>
using namespace std;
void printArray(int array[]) {
for (int i = 0; i < 100; ++i) {
cout << array[i] << " " << array[i + 100] << " "
<< array[i + 200] << " " << array[i + 300] << " "
<< array[i + 400] << " " << array[i + 500] << " "
<< array[i + 600] << " " << array[i + 700] << " "
<< array[i + 800] << " " << array[i + 900] << " ";
cout << endl;
}
cout << endl << endl;
}
void sumOdd(int array[]) {
int oddTotal = 0;
for (int i = 0; i < 1000; ++i) {
if (array[i] % 2 == 1) {
oddTotal += array[i];
}
}
cout << "Sum of all odd number present in array = " << oddTotal << endl << endl;
}
void sumEven(int array[]) {
int evenTotal = 0;
for (int i = 0; i < 1000; ++i) {
if (array[i] % 2 == 0) {
evenTotal += array[i];
}
}
cout << "Sum of all even number present in array = " << evenTotal << endl << endl;
}
void search(int array[]) {
cout << "Enter value to search : ";
int key;
cin >> key;
int found = 0;
for (int i = 0; i < 1000; ++i) {
if (array[i] == key) {
cout << key << " found at index " << i << endl << endl;
++found;
}
}
if (found == 0) {
cout << "Value is not in array ";
}
}
void highest(int array[]) {
int key=-89898;
for (int i = 0; i < 1000; ++i) {
if (array[i] >key) {
key=array[i];
}
}
cout<<"The highest number is = " <<key<<endl;
}
void lowest(int array[]) {
int key=89898;
for (int i = 0; i < 1000; ++i) {
if (array[i] <key) {
key=array[i];
}
}
cout<<"The lowest number is = " <<key<<endl;
}
void swap(int *xp, int *yp){
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void sort(int arr[]){
int i, j;
for (i = 0; i <1000; i++) {
for (j = 0; j < 1000-i-1; j++) {
if (arr[j] < arr[j+1])
swap(&arr[j], &arr[j+1]);}}
printArray(arr);
}
void first(int arr[]){
cout<<"The first element is "<<arr[0] <<endl;
}
void last(int arr[]){
cout<<"The last element is "<<arr[999] <<endl;
}
int main() {
int array[1000];
for (int i = 0; i < 1000; ++i) {
array[i] = 1 + rand() % 1000;
}
int choice = 1;
while (choice) {
cout << endl;
cout << "Enter 1. output all integer values ";
cout << "Enter 2. Sum all odd numbers ";
cout << "Enter 3. Sum all even numbers ";
cout << "Enter 4. Enter a search key ";
cout << "Enter 5. The highest value ";
cout << "Enter 6. The lowest value ";
cout << "Enter 7. Sort Array ";
cout << "Enter 8. Output First Number in the Array ";
cout << "Enter 9. Output Last Number in the Array ";
cout << "Enter10. Exit ";
cin >> choice;
switch (choice){
case 1:
printArray(array);
break;
case 2:
sumOdd(array);
break;
case 3:
sumEven(array);
break;
case 4:
search(array);
break;
case 5:
highest(array);
break;
case 6:
lowest(array);
break;
case 7:
sort(array);
break;
case 8:
first(array);
break;
case 9:
last(array);
break;
case 10:
exit(1);
break;
default:
cout << "Invalid choice !! ";
cout << "Please enter a valid choice .... ";
break;
}
}
return 0;
}
Task 1: (Update Previous Assignment Main Function: Declare and fill an array with 1000 random integers between 1 1000. You will pass the array into functions that will perform operations on the array. Function 1: Create a function that will output all the integers in the array. (Make sure output is clearly formatted. Function 2: Create a Function that will sum and output all the odd numbers in the array. Function 3: Create a Function that will sum and output all the even numbers in the array. Function 4: Create a function that will allow the user to enter an integer value to search. Program will implement a Binary Search algorithm to perform the search. Program will output if integer value is found and the location of the integer in the array or The program will output "value is not in array". (It is ok for now for duplicate if duplicate values exist in the array. The goal is to correct implement the Binary Search and return a singular location of where the value exists in the array.) Function 5: Create a function that will output the highest value in the array Function 6: Create a function that will output the lowest value in the array Function 7: Implement a Bubble Sort algorithm. Sort the elements in the array and output in descending order.Explanation / Answer
You have everything else right and there is need to update only the search function of your code. which is below.
The binary search implementation.The array should be sorted before we can search through binary search algorithm as binary search algorithm works for sorted array only.
void search(int arr[])
{
int low=0;
int high=999;
cout << "Enter value to search : ";
int key;
cin >> key;
int found = 0;
while (low <= high)
{
int mid = low + (high-low)/2;
if (arr[mid] == key)
{
cout << key << " found at index " << mid << endl << endl;
++found;
}
if (arr[mid] > key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
if (found == 0)
{
cout << "Value is not in array ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.