The C++ program attached to this assignment generates 100 random integers in an array sorted in
increasing order. Modify this program to accept a number from a user and perform a binary search for
that number. If the number is present in the array, the program should output its index in the array. If
the number is not present, the program should output “not present."
---i can't get the my function to say "not present",....Help
#include <cstdlib>
#include <time.h>
#include <iostream>
using namespace std;
const int ARRAY_SIZE = 100;
void generateValues (int a [], int aSize);
void printValues (int a [], int aSize);
int numSearch(int a[], int n, int aSize);
int main () {
int vals [ARRAY_SIZE];
int n;
cout << "Generating " << ARRAY_SIZE << " random values...";
generateValues (vals, ARRAY_SIZE);
cout << " done" << endl;
cout << "Please enter a value to search for in the array." << endl;
cin >> n;
cout << "The number " << n << " is in index: " << endl;
numSearch(vals, n, ARRAY_SIZE);
printValues (vals, ARRAY_SIZE);
system("PAUSE");
return 0;
}
void generateValues (int a [], int aSize) {
int i = 0;
int previous = 0;
time_t seconds;
time (&seconds);
srand((unsigned int) seconds);
while (i < aSize) {
a[i] = previous + (rand() % 3 + 1);
previous = a[i];
++i;
}
}
void printValues (int a [], int aSize) {
int i = 0;
while (i < aSize) {
cout << "[" << i << "] = " << a[i] << endl;
++i;
}
}
int numSearch(int a[], int n, int aSize){
int i=0;
int index;
while(i < aSize){
if(a[i]==n){
cout << i << endl;
}
if(a[i]!=n){
cout << "NOT PRESENT" << endl;
}
}

New Topic/Question
Reply




MultiQuote




|