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?

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

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
#include <iostream>
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?
1
2
smallestIndex = 0;
	largestIndex = 0;


You've got variables with the same name as your functions. This is bad.
I deleted the
1
2
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 :(

Please help me
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
#include <iostream>
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;
}


If I write the program just like this the error it gives me is that the variable smallestIndex and largestIndex are being used without being initialized. So I think the only problem with my code is that I haven't initialized the smallestIndex and largestIndex in the main function.
Topic archived. No new replies allowed.