Please Help!

PLease help i dont know why but it keeps saying there is no matching functions to index_of_smallest and i need to finish this program before midnight!
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
#include <iostream>
#include <ctype.h>
using namespace std;

const int MAXSIZE = 20;
void read_array(char d_array[], int& size); // array will be modified
void write_array(const char d_array[], int size); // array will not be modified
int index_of_smallest(const char d_array[], int start_index, int size);
void sort_array(char d_array[], int used_size);

int main(void){
    char digit_array[MAXSIZE];
    int size, i;
    size = 0;
    cout << "Enter an integer with no more than 20 digits: ";        
    read_array(digit_array, size);        
    write_array(digit_array, size);
    sort_array(digit_array, size);
    return 0;
}

void read_array(char d_array[], int& size){ 
    char digit;
    do {
        cin.get(digit);
        if (isdigit(digit)){
            d_array[size] = digit;
            ++size;} 
    } while (size < 20 && isdigit(digit));
} 

void write_array(const char d_array[], int size){ 
    cout << "The integers you entered are: ";
    for (int i = 0; i<size; ++i)
        cout << d_array[i];
    cout << endl;
}

int index_of_smallest(const char d_array[], int start_index, int size){
    int min = d_array[start_index], index_of_min = start_index;
    for(int i = start_index+1; i < size; i++) 
        if(d_array[i] < min ){
            min = d_array[i];                
            index_of_min = i;           
        }           
    return index_of_min;
}

void sort_array(char d_array[], int size){
    int index_of_next_smallest;
    int temp;
    for(int i = 0; i < size-1; i++){
        index_of_next_smallest = index_of_smallest(d_array, index, size);// swap two elements 
        temp = d_array[i];
        d_array[i] = d_array[index_of_next_smallest];
        d_array[index_of_next_smallest] = d_array[i];
    }
}
These are your warnings/errors:
 In function 'int main()':
13:15: warning: unused variable 'i' [-Wunused-variable]
 In function 'void sort_array(char*, int)':
53:61: error: 'index' was not declared in this scope

The error is referring specifically to line 53. On line 53, you are passing a value called "index" to your index-of_smallest function, but this variable does not exist. It is not clear what you meant to pass here. Perhaps you should pass index_of_next_smallest, and initialize it to 0? It's not clear to me.

Is there a particular sorting algorithm you're supposed to be using? There's tons of resources you can search for online if you're having trouble implementing a sort function.
Generally, a simple sort like a bubble sort is relatively easy to program.

When you iterate over an array, if there is ever a point where array[i-1] is larger than array[i], then swap array[i] and array[i-1] and then start over until the array is sorted.

PS:
1
2
3
        temp = d_array[i];
        d_array[i] = d_array[index_of_next_smallest];
        d_array[index_of_next_smallest] = d_array[i];

I presume you are attempting to swap two array elements here, but you aren't doing it correctly. Notice that you are assigning to temp, but never using it for anything else, e.g.
d_array[index_of_next_smallest] = temp;
Last edited on
There is std::swap() to swap two variables See http://www.cplusplus.com/reference/algorithm/swap/

There is also std::sort() unless you have to write your own See http://www.cplusplus.com/reference/algorithm/sort/

Also std::min_element() to determine the smallest value index. See http://www.cplusplus.com/reference/algorithm/min_element/

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
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;

const int MAXSIZE {20};

void read_array(char d_array[], size_t& size);
void write_array(const char d_array[], size_t size);
size_t index_of_smallest(const char d_array[], size_t size);
void sort_array(char d_array[], size_t used_size);

int main(void)
{
	char digit_array[MAXSIZE] {};
	size_t size {};

	cout << "Enter an integer with no more than " << MAXSIZE << " digits (non-digit to terminate): ";

	read_array(digit_array, size);

	cout << "The integers you entered are: ";
	write_array(digit_array, size);

	cout << "An index of smallest number is " << index_of_smallest(digit_array, size) << '\n';
	sort_array(digit_array, size);

	cout << "The numbers sorted are ";
	write_array(digit_array, size);
}

void read_array(char d_array[], size_t& size) {
	for (char digit {}; cin.get(digit) && size < MAXSIZE && isdigit(digit); d_array[size++] = digit);
}

void write_array(const char d_array[], size_t size) {
	for (int i = 0; i < size; ++i)
		cout << d_array[i];

	cout << '\n';
}

size_t index_of_smallest(const char d_array[], size_t size) {

	return std::min_element(d_array, d_array + size) - d_array;
}

void sort_array(char d_array[], size_t size) {
	std::sort(d_array, d_array + size);
}

Topic archived. No new replies allowed.