Help with dynamic array/pointer return. Feel like an idiot

All,

1. Yes I'm super new.
2. Yes,this is partially a sanity check for a homework related question
3. NO--Im not looking for the easy way out, I've written the majority of the code myself and am just looking for some help with compile issues. If, along the way, you can see if my code would be wrong--please let me know.
4. I've edited out the smallestIndex code as I don't want to post the entire thing up (I don't want others copying). I hate asking for help enough, and I've been racking my head around this for a couple of days. If I recieve help for the largest return, ill be able to get the rest fixed.

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

int main()
{
	int n, i;
	int *p;
	cout << "Please input the number of entries you would like to store: ";
	cin >> n;
	p = new int[n]();

	for (i = 0; i<n; i++)
	{
		cout << "Enter Value: ";
		cin >> p[i];
	}

	cout << "The Largest Value in the list is: " << largestIndex << "\n";
	cout << "The Smallest Value in the list is:" << smallestIndex << "\n";

	delete[] p;
	return 0;
}

int largestIndex(int **p, int **arraySize)
{
	int farthest = 0;
	for (int i = 0; i < **arraySize; i++) {
		if (p[i] >= p[farthest])
			farthest = i;
	}
	return farthest;
}



What I'm trying to do in a nutshell-- I'm trying to have the user input a list of values and length of a dynamic array. This works just fine. Then im trying to return the index of the largest value of the list. I'm trying to accomplish this using array pointers instead of arrays as parameters to the functions. My problem...

how the heck do I successfully return the value of largestPosition back to my main? From there it would be a simple cout << "Largest value is: " << largestPosition[l] <<"\n"; .. when I try to return the largest position like I want I get a "return value type does not match the function type" compile error.
Last edited on
There are a number of problems in the code. You only need a single * to pass a one-dimensional array, and no need for any * when passing a plain integer value.

There's an extraneous () when allocating the array with new.

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

using namespace std;

int largestIndex(int * p, int arraySize);

int main()
{
    cout << "Please input the number of entries you would like to store: ";
    
    int n;
    cin >> n;
    
    int *p = new int[n];

    for (int i = 0; i<n; i++)
    {
        cout << "Enter Value: ";
        cin >> p[i];
    }

    int large = largestIndex(p, n);
    cout << "The Largest Value in the list is: " << p[large] << "\n";

    delete[] p;
    return 0;
}

int largestIndex(int * p, int arraySize)
{
    int largestPosition = 0;
    
    for (int i = 0; i < arraySize; i++) 
    {
        if (p[i] > p[largestPosition])
            largestPosition = i;
    }
    
    return largestPosition;
}


Last edited on
Darnit, thanks for spotting the pointers to pointers trying to be passed. This was from last night when I was at wits end--seems like I made some changes to my code that I didn't need.

As for the extra "()" -- Was under the belief that this would simply set the array to 0, but I can see how it isn't needed.

Attempted to compile the code ref. above--however ran into some odd undeclared errors. Namely all of the cout/cins. "error c2065 'cout' undeclared identifier"

Thank you for the quick look--I really appreciate it, looks like I wasn't too far off--which is comforting...slightly...
The error message
 "'cout' undeclared identifier"

usually should be fixed provided your program has this at the start:
1
2
3
#include <iostream>

using namespace std;


There may be some particular issue with the header #include "stdafx.h" which I can't really help with as it is a Microsoft own way of doing things. This link may help:
https://stackoverflow.com/a/26517329
Chervil,

You are absolutely correct. Gotta love Visual Studio. I created a new project and imported the code as well as my commented out statements for smaller index value--and it worked like a champ. Looks like my biggest mistake was not using "p[]" for the cout statements as well as those "**" (I must have been completely zonked when I typed those as none of my other code versions have em). Thank you again so much! That really helped me learn! So frustrating when you are so close to being correct--only to have the stupid IDE throw things to make you go down a rabbit hole.

THANK YOU!
Topic archived. No new replies allowed.