Writing a Function with an Array

Hello everyone.
I was attempting to make a code for a problem that has 2 parts.

A: Write a function, smallestIndex, that takes as parameters an int array & it's size & returns the index of the first occurrence of the smallest element in the array.
B: Write a function, lastLargestIndex, that takes as parameters an int array & it's size & returns the index of the last occurrence of the largest element in the array.

I had them working separately, but when I tried to combine them, I ended up getting a bunch of error codes (literally like 100). I was also using a different program than I had originally written it in when I got the codes, if that would make any difference. Here is what my code looks like when I tried to combine them.

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
#include <iostream>

using namespace std;

int smallestIndex(int array[], int arraySize);
int lastLargestIndex(int array[]); int arraySize);

int main()
{
    const int MAX_ARRAY_SIZE = 15;
    int test[MAX_ARRAY_SIZE] {16, 22, 4, 17, 21, 84, 43, 62, 53, 24, 35, 13,
        29, 16, 29};
    int smallestPosition = smallestIndex(test, MAX_ARRAY_SIZE);
    cout << "The smallest index position is: " << smallestPosition << ".";
    cout << "The value of the smallest index is: " << test[smallestPosition] << ".";
    int largestPosition = lastLargestIndex(test, MAX_ARRAY_SIZE);
    cout << "The largest index position is: " << largestPosition << ".";
    cout << "The value of the largest index is: " << test[largestPosition];
    return 0;
}

/*int main()
{
    const int MAX_ARRAY_SIZE = 15;
    int test[MAX_ARRAY_SIZE] {16, 22, 4, 17, 21, 84, 43, 62, 53, 24, 35, 13,
        29, 16, 29};
    int largestPosition = lastLargestIndex(test, MAX_ARRAY_SIZE);
    cout << "The largest index position is: " << largestPosition << ".";
    cout << "The value of the largest index is: " << test[largestPosition];
    return 0;
}
(this is what it looked like before I tried to combine them)*/

int smallestIndex(int array[], int arraySize)
{
    int smallest = 0;
    for (int i = 0; i < arraySize - 1; i++)
    {
        if (array[i] < array[smallest]) smallest = i;
    }
    return smallest;
}

int lastLargestIndex(int array [], int arraySize)
{
    int largest = 0;
    for (int i = 0, i < arraySize; i++)
    {
        if (numberArray[i] >= largest) largest = i;
    }
    return largest;
}


Any help is appreciated. Thanks!
What is the very first error? (Hint: line 6.)
Oh shoot. I changed that on the actual program that gave me the errors, I forgot to fix it before I posted it, sorry. Most of the error codes said something about the "{" "}" symbols, but I checked and they are all matched and there's semicolons at the ends of all the lines.
Ok, show the real code and the exact first error.


Note, in your lastLargestIndex(), line 49, what do you compare?
Okay, I fixed some stuff, and I don't see any errors showing up. How does this look?

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
#include <iostream>

using namespace std;

int smallestIndex(int arrayS[], int arraySizeS);
int lastLargestIndex(int arrayL[], int arraySizeL);

int main()
{
    const int MAX_ARRAY_SIZE = 15;
    int test[MAX_ARRAY_SIZE] {16, 22, 4, 17, 21, 84, 43, 62, 53, 24, 35, 13,
        29, 16, 29};
    int smallestPosition = smallestIndex(test, MAX_ARRAY_SIZE);
    cout << "The smallest index position is: " << smallestPosition << ".";
    cout << "The value of the smallest index is: " << test[smallestPosition] << ".";
    int largestPosition = lastLargestIndex(test, MAX_ARRAY_SIZE);
    cout << "The largest index position is: " << largestPosition << ".";
    cout << "The value of the largest index is: " << test[largestPosition];
    
    system("pause");
    return 0;
}


int smallestIndex(int arrayS[], int arraySizeS)
{
    int smallest = 0;
    for (int i = 0; i < arraySizeS - 1; i++)
    {
        if (arrayS[i] < arrayS[smallest]) smallest = i;
    }
    return smallest;
}

int lastLargestIndex(int arrayL[], int arraySizeL)
{
    int largest = 0;
    for (int ii = 0; ii < arraySizeL; ii++)
    {
        if (arrayL[ii] >= arrayL[largest]) largest = ii;
    }
    return largest;
}
Last edited on
Nice, except your test data; neither min nor max value repeats in the set, so no proof of finding the first/last.
how would I go about fixing that, besides repeating a couple numbers?
Exactly that, fix some numbers.
Topic archived. No new replies allowed.