### Array programming problem.

The instructions are "Write a function, smallestIndex, that takes as parameters an int array and its size, and returns the index of the first occurrence of the smallest element value in the array.

Write a function, largestLastIndex, that takes as parameters and int array and its size and returns the index of the last occurrence of the largest element value in the array.

In the main function, prompt the user to enter 5 numbers and populate your array, then pass your array to each function, displaying the return values (in the main function) for each function call."

And this is what I have so far and the problem is that for largest number and smallest number i keep getting 0. What is wrong with my code? what can I do to fix it?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include using namespace std; double smallestIndex (int list[], int size); double largestIndex (int list[], int size); int main() { int idx; int array[5]; int smallestIndex; int largestIndex; int location; int lenght; int temp; smallestIndex = 0; largestIndex = 0; cout << "Enter 5 numbers." << endl; for (idx = 0; idx < 5; idx++) { cin >> array[idx]; } cout << "The smallest number is: " << smallestIndex << endl; cout << "The largest number is: " << largestIndex << endl; return 0; } double smallestIndex (int list[], int size) { int idx; int smallestIndex; int location; int lenght; int temp; smallestIndex = 0; for (location = idx + 1; location < lenght; location++) if (list[location] < list[smallestIndex]) smallestIndex = location; temp = list[smallestIndex]; list[smallestIndex] = list[idx]; list[idx] = temp; return smallestIndex; } double largestIndex (int list[], int size) { int idx; int largestIndex; int location; int lenght; int temp; largestIndex = 0; for (location = idx + 1; location < lenght; location++) if (list[location] > list [largestIndex]) largestIndex = location; temp = list[largestIndex]; list[largestIndex] = list[idx]; list[idx] = temp; return largestIndex; }``````
Your code doesn't actually call the functions you wrote. You have to call functions if you want the code inside them to be executed.
 Your code doesn't actually call the functions you wrote. You have to call functions if you want the code inside them to be executed.

How do I do that? Can you give me an example or write a piece of code so I can understand better? Thanks.
Here is an example of how to call your function largest index:

`double returnedValue = largestIndex(array, 5);`

If this is news to you, you need to go right back to the start and learn about functions. It seems amazing that you can write a function but have no idea how to use it.
`double returnedValue = largestIndex(array, 5)`

Do i need to write that inside my main function or inside the largestIndex function (like from line 53 downwards)?

 If this is news to you, you need to go right back to the start and learn about functions. It seems amazing that you can write a function but have no idea how to use it.

I don't have the best professor ever and he just confuses us, not on purpose though. So all I know I just read it from the book and the functions chapter was the most confusing and I had a really hard time understanding it so I kind of go by the "trail and error" method and learn that way even though it's not really the best way.
 Do i need to write that inside my main function or inside the largestIndex function (like from line 53 downwards)?

You need to write it at the point in your program where you want to calculate the value.

How much of the following do you understand?

Your program effectively starts running at the beginning of the function named main and goes downwards through the code, and then each time you call a function, the flow of code being executed jumps to the start of that function, and then flows downwards through that function, and each time you get to a return, the flow of execution goes back to the place where you were before you jumped into that function and carries on downwards.
Last edited on
 Your program effectively starts running at the beginning of the function named main and goes downwards through the code, and then each time you call a function, the flow of code being executed jumps to the start of that function, and then flows downwards through that function, and each time you get to a return, the flow of execution goes back to the place where you were before you jumped into that function and carries on downwards.

Yeah I understand that, isn't that basically how functions work?

 You need to write it at the point in your program where you want to calculate the value.

Wait, looking at what you said below, I think that it means I need to write that piece of code in my main function, so then it jumps into the largestIndex function and calculates the largest number, then hits the return and jumps again into the main function?
Last edited on
 I think that it means I need to write that piece of code in my main function, so then it jumps into the largestIndex function and calculates the largest number, then hits the return and jumps again into the main function?

Yes. Call the largestIndex function in your main function. Remember, when you get to the end of the main function, just like any other function, control goes back to whatever called main - which is outside your program, and as far as you are concerned, when you return from main, the program has ended. Flow of execution does not just carry on downwards after main ends.
Last edited on
Now since I have to call both the largestIndex and smallestIndex do I have to write two lines of codes in the main function like this:

`double returnedValue = largestIndex(array, 5);`

and

`double returnedValue = smallestIndex(array, 5);`

`double returnedValue = largestIndex(array, 5);`

This means; create an object of type double, named returnedValue, and set it to the value that you get back from calling the function largestIndex (with the parameters array and 5).

`double returnedValue = smallestIndex(array, 5);`

This means; create an object of type double, named returnedValue, and set it to the value that you get back from calling the function smallestIndex (with the parameters array and 5).

So if you do this, you are trying to create two variables with the same name; returnedValue. You cannot have two objects with the same name.
Last edited on
What can I do ? Do I just create another object so they don't both have the name returnedValue? Can I do this:

`double smallestValue = smallestIndex(array, 5)`

and

`double largestValue = largestIndex(array, 5)`

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#include using namespace std; double smallestIndex (int list[], int size); double largestIndex (int list[], int size); int main() { int idx; int array[5]; int smallestIndex; int largestIndex; int location; int lenght; int temp; smallestIndex = 0; largestIndex = 0; double largestValue = largestIndex(array, 5); double smallestValue = smallestIndex(array, 5); cout << "Enter 5 numbers." << endl; for (idx = 0; idx < 5; idx++) { cin >> array[idx]; } cout << "The smallest number is: " << smallestIndex << endl; cout << "The largest number is: " << largestIndex << endl; return 0; } double smallestIndex (int list[], int size) { int idx; int smallestIndex; int location; int lenght; int temp; smallestIndex = 0; for (location = idx + 1; location < lenght; location++) if (list[location] < list[smallestIndex]) smallestIndex = location; temp = list[smallestIndex]; list[smallestIndex] = list[idx]; list[idx] = temp; return smallestIndex; } double largestIndex (int list[], int size) { int idx; int largestIndex; int location; int lenght; int temp; largestIndex = 0; for (location = idx + 1; location < lenght; location++) if (list[location] > list [largestIndex]) largestIndex = location; temp = list[largestIndex]; list[largestIndex] = list[idx]; list[idx] = temp; return largestIndex; }``````

Now this is what I wrote and first of all i get red lines under largestIndex and smallestIndex in lines 19 and 20 and it tells me that "expression preceding parenthesis of apparent call must have (pointer-to-) function type" , then when I debug it the errors are the following: "term does not evaluate to a function taking 2 arguments." and it is reffering to the lines 19 and 20.

Can you explain that to me in a way i can understand it?
 ``12`` ``````smallestIndex = 0; largestIndex = 0;``````

You've got variables with the same name as your functions. This is bad.
I deleted the
 ``12`` ``````smallestIndex = 0; largestIndex = 0;``````

from lines 16 and 17 and I get the same errors, then I deleted the ones from line 43 and 64 and I still get the same errors :(

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include using namespace std; double smallestIndex (int list[], int size); double largestIndex (int list[], int size); int main() { int idx; int array[5]; int smallestIndex; int largestIndex; int location; int lenght; int temp; cout << "Enter 5 numbers." << endl; for (idx = 0; idx < 5; idx++) { cin >> array[idx]; } cout << "The smallest number is: " << smallestIndex << endl; cout << "The largest number is: " << largestIndex << endl; return 0; } double smallestIndex (int list[], int size) { int idx; int smallestIndex; int location; int lenght; int temp; for (location = idx + 1; location < lenght; location++) if (list[location] < list[smallestIndex]) smallestIndex = location; temp = list[smallestIndex]; list[smallestIndex] = list[idx]; list[idx] = temp; return smallestIndex; } double largestIndex (int list[], int size) { int idx; int largestIndex; int location; int lenght; int temp; for (location = idx + 1; location < lenght; location++) if (list[location] > list [largestIndex]) largestIndex = location; temp = list[largestIndex]; list[largestIndex] = list[idx]; list[idx] = temp; return largestIndex; }``````