Not Sure Why It Always Returns -1..

This program is supposed to find the largest number, then cout the index it's found in an array, along with the unsorted array. We can use a sorted array when we sort, then cout the unsorted array.

But it seems to always return -1. Why??

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
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <math.h>

using namespace std;

int search(int arrray[],int copyarrray[]);
int copysearch(int copyarrray[]);
void fill(int arrray[], int copyarrray[]);
void display(int arrray[]);
void sort(int arrray[]);

	int main()

{
	int window;
	int num;
	int result;
	int arrray[20];
	int copyarrray[20];

	fill(arrray, copyarrray);

	sort(copyarrray);
	
	num = copysearch(copyarrray);

	result = search(arrray,copyarrray);

	if (result != -1)
		cout << "\nThe largest element was found at index " << result << "." << endl;

	display(arrray);

	cin >> window;

return 0;

}

void fill(int arrray[],	int copyarrray[20])
{
	srand(1);
		for(int i = 0; i < 20; i++)
		{
			arrray[i] = rand() % 100;
			copyarrray[i] = rand() % 100;
		}
}

int copysearch(int copyarrray[])
{
	return copyarrray[19];
}

void display(int arrray[])
{
	for(int x = 0; x < 20; x++)
		cout << arrray[x] << "  ";
}

void sort(int copyarrray[])
{
     int i, j, key, numLength = 20;
     for(j = 1; j < numLength; j++)
    {
           key = copyarrray[j];
           for(i = j - 1; (i >= 0) && (copyarrray[i] > key); i--)
          {
                 copyarrray[i+1] = copyarrray[i];
          }
         copyarrray[i+1] = key;
     }
     return;
}

int search(int arrray[],int copyarrray[])
{
	int index;

	for(index = 0; index < 20; index++)
	{
		if (copyarrray[19] == arrray[index])
			return index;
		else
			return -1;
	}
}
I assume you are referring to your search function. What is so special about copyarray[ 19 ]?
It holds the largest element in the sorted array, which would be the largest element in the whole array.
arrray after fill
83  77  93  86  49  62  90  63  40  72  11  67  82  62  67  29  22  69  93  11  
copyarrray after fill
86  15  35  92  21  27  59  26  26  36  68  29  30  23  35  2  58  67  56  42  

arrray after sort
83  77  93  86  49  62  90  63  40  72  11  67  82  62  67  29  22  69  93  11  
copyarrray after sort
2  15  21  23  26  26  27  29  30  35  35  36  42  56  58  59  67  68  86  92  

num after copysearch = 92
copyarrray[19] = 92
arrray[0] = 83


The search doesn't match on the first comparison in the if statement. copyarrray[19] (or 92) != arrray[0] (or 83). So the function goes to the else statement which terminates the function call with a return -1. It's not going to go to the next iteration of the for loop.

1
2
3
4
if (copyarrray[19] == arrray[index])
	return index;
else
	return -1;
Last edited on
Your fill() function puts two different sets of random numbers in array and copyarray. Did you mean for the arrays to start out with the same values?
@dhayden

Yes. It's an easy fix, if thats the only problem. :)
Changed the fill array to

1
2
3
4
5
6
7
8
9
void fill(int arrray[],	int copyarrray[20])
{
	srand(1);
		for(int i = 0; i < 20; i++)
		{
			arrray[i] = rand() % 100;
			copyarrray[i] = arrray[i];
		}
}


And now it stops displaying anything after displaying the filled, unsorted array. :(
Did you intend to reinitialize the random number generator at line 3?
That will cause fill() to return the same random number sequence every time.

Okay, but what is causing it to stop displaying anything after displaying the filled, unsorted array?
Last edited on
Anyone have an idea why?
Why does void sort() have a return floating in it?

Okay, but what is causing it to stop displaying anything after displaying the filled, unsorted array?


You call display only at the end, so it's only displaying the array at the end of everything. If the array it displays is unsorted, your sort function is not working.
Topic archived. No new replies allowed.