Array of 10000, display only 5 problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

const int size = 10000;
int NUM[size];
void insertVAL(int [], const int);
void sort(int [], const int);
void binarysearch(int [], const int);
void sequentialsearch(int [], const int);
void menu();

int main()
{
	char choice;
	insertVAL(NUM, size);
	menu();
	do
	{
		cout << "\nEnter choice: ";
		cin >> choice;
		if(choice == 's')
		{
			sort(NUM, size);
		}
		if(choice == 'c')
		{
			sequentialsearch(NUM, size);
		}
	}
	while(choice != 'q');
	return 0;
}

void menu()
{
	cout << "Binary and Sequential Search --"
		 << "\nSort - s"
		 << "\nBinarySearch - b"
		 << "\nSequentialSearch - c"
		 << "\nExit - q";
}
void insertVAL(int number[], const int length)
{
	unsigned seed = time(0);
	srand(seed);
	for(int i = 0; i < length; i++)
	{
		number[i] = 0 + rand() % 1000001;
	}
}
void sort(int number[], const int length)
{
	bool check;
	int temp;
	do
	{
		check = true;
		for(int i = 0; i < (length - 1); i++)
		{
			if(number[i] > number[i+1])
			{
				temp = number[i];
				number[i] = number[i+1];
				number[i+1] = temp;
				check = false;
			}
		}
		
	}
	while(check == false);
}
void sequentialsearch(int number[], const int length)
{
	int data;
	cout << "Enter number to find position: " << "(" << number[5000] << ") ";
	cin >> data;
	for(int i = 0; i < length; i++)
	{
		if(number[i] == data)
		{
			cout << "Position: " << i << endl;
			for(int j = (i-2); j < i; j++)
			{
				cout << j << ".  " << number[j] << endl;
			}
			for(int k = i; k < (k+2); k++)
			{
				cout << k << ".  " << number[k] << endl;
			}
			break;
		}
	}
	cout << "\nNumber not found, Try again ";
}


When program is running, after I input key number I provide, it doesnt display only 5 positions but rather displays an infinite loop of some sort PASSED the array. HELP!
for(int k = i; k < (k+2); k++)

Your problem is here. The value k will always be less than k + 2.

Try
for (int k = i + 1; k < (i + 3); k++)
Topic archived. No new replies allowed.