Pointers/Arrays

Sep 23, 2011 at 11:51pm
I'm not really sure where I'm going wrong here, I keep going back and forth, I fix one error and get another, etc. The whole concept of pointers and arrays really confuses me for some reason, I've sat through the class and read the book over and over again but I just can't seem to get it. The error I'm getting right now is about assigning from 'int' to 'const int*' and then again 'no matching function call for mean' I'm so lost, just trying to calculate the mean of a user input array, using the prototype at the top. Please help!



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


int main()
    {

        int i = 0;
        const int* a[100];
        int nums;
        
        cout << "Please enter some integers: " << endl;

        while (cin >> nums){
            nums = a[i];
            i++;
        }


        Mean(a, size);
        
             
 
    
    
    return 0;
}


float Mean (const int* a, size_t size)
{
    float mean = 0;
    
    for (int j = 0; j < 100; j++)
        {
            
            mean += a[j];
            cout << "Mean is: " << mean << endl;
        }
    

    return mean;
}

Sep 24, 2011 at 12:23am
its this right here:
 
const int* a[100];


it should look like:
 
const int *a = new int[100];


other wise it would look like
1
2
int array[100];
const int *a = &array[0];


when dealing with * base pointer the space needs to be allocated some where. "new" is one way and passing a reference to a pointer is another way to insure the space is allocated for the dynamic allocation.
Sep 24, 2011 at 2:20am
closed account (zb0S216C)
tinkydwd123 wrote:
const int* a[100]; (sic)

This is a constant array of integer pointers. Why you declared it constant is beyond me, but what I can say is that you need to dereference each element of the array in order to reveal the datum it's pointing to. For clarity, here's an example:

1
2
3
4
5
6
int Ints[4] = { 1, 2, 3, 4 };

int *ArrayOfPtrs[4] = { &Ints[0], &Ints[1], &Ints[2], &Ints[3] };

// Set Ints[1] to 10:
*ArrayOfPtrs[1] = 10;

Notice the asterisk before the ArrayOfPtrs symbol.

Also, Mean( ) returns a floating point. However, you never do anything with the value that was returned.

tinkydwd123 wrote:
"Thread 1: Stopped at breakpoint 1.' Any suggestions? (sic)

Where did it stop?

Wazzak
Last edited on Sep 24, 2011 at 5:55pm
Sep 24, 2011 at 2:33am
Thanks to both of you...Azagaros, I tried that and it got rid of the errors I was having. Framework, I figured out the breakpoint part, I think it was just a setting I had to change...I deleted it but I guess I wasn't fast enough. I have no idea why it's supposed to be const either, it's for an assignment and that's what the teacher told us to use. I'm still working on the Mean part, I think I have a bit to add to that function, but I have to figure this part out first.

Now I'm having this problem:

1
2
3
4
5
6
        cout << "Please enter some integers: " << endl;

        while (cin >> size){
            a[i] = size;
            i++;
        }


I can't get this to assign the values to my array. I had it working before, but it says a[i] is a read only variable that's not assignable. I see the above example, but I don't understand how to incorporate that from user input, since I have to declare the array before the user is prompted for integers.
Sep 24, 2011 at 4:33am
Ahh, I figured it out. THanks guys!
Topic archived. No new replies allowed.