4 Replies - 1052 Views - Last Post: 08 May 2007 - 05:00 PM Rate Topic: -----

#1 Th3Gam3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-May 07

Array Search

Posted 08 May 2007 - 10:10 AM

The problem i have to do is :
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;
		}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Array Search

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Array Search

Posted 08 May 2007 - 10:24 AM

You are not incrementing i in the while loop...you are consistently checking the same value.

It also does not appear that you have closed the while loop...are you missing a brace?
Was This Post Helpful? 0
  • +
  • -

#3 Th3Gam3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-May 07

Re: Array Search

Posted 08 May 2007 - 10:34 AM

View PostAmadeus, on 8 May, 2007 - 10:24 AM, said:

You are not incrementing i in the while loop...you are consistently checking the same value.

It also does not appear that you have closed the while loop...are you missing a brace?

i incremented the i in the while loop as you said...and closed the loop...but the problem i'm still getting is when the number is not in the array my cout "Not Present" keeps on getting printed.. its doesn't stop.
Was This Post Helpful? 0
  • +
  • -

#4 sondurak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-May 07

Re: Array Search

Posted 08 May 2007 - 02:12 PM

Many mistakes that i realize; that
you are not doing binary search,u are sequantially looking all elements in the array from zero index to its size.
numSearch(..) function has a integer return type but why do u need that type? i couldnt understand.
if you insist to use that numSearch function, u can modify it
after correcting the return type to void and just like;
..
while(i < aSize)
 {  
		if(a[i]==n)
		 {
			 cout << "index is"<< i << endl; 
			 return; 
		 }
  i++;
 } // while loops ends if no match found
	 
   cout << "NOT PRESENT" << endl;
  return;




And also you can cancel the line in the main function ;
	 cout << "The number " << n << " is in index: " << endl;


Good luck.
Was This Post Helpful? 0
  • +
  • -

#5 Th3Gam3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-May 07

Re: Array Search

Posted 08 May 2007 - 05:00 PM

View Postsondurak, on 8 May, 2007 - 02:12 PM, said:

Many mistakes that i realize; that
you are not doing binary search,u are sequantially looking all elements in the array from zero index to its size.
numSearch(..) function has a integer return type but why do u need that type? i couldnt understand.
if you insist to use that numSearch function, u can modify it
after correcting the return type to void and just like;


How would i write this in binary search?don't understand...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1