0

so I needing some help with creating a program for my class. The lab requires us to use pointers. This is the description of what we have to do...

-Write a function that accepts an int array and the array's size as arguments. -The program should ask the size of the array and lets the users enter some integer values. -The function should create a new array that is one element larger than the argument array. -The first element of the array should be set to 0. -Element 0 of the argument array should be copied to element 1 of the new array. -Element 1 of the argument array should be copied to element 2 of the new array, etc. -The function should return a pointer to the new array. -There should be three other functions: getMode, getMedian and getAverage. -These functions should get Mode, Median and Average of the values within an array. -You should display the argument array and the new array as well as the mode, median and the average.

This is what I have so far I'm not sure if its right. Any help is greatly appreciated. UPDATE: I run the program and it asks the user for the size of the array and the values for it...

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>

int* addToSize (int*, int);

using namespace std;


int main()
{   

    int userSize=0; //Holds user size
    int userInts; //Holds uaer values
    int *memory; //dynamically allocate an array


    //int  *intptr;
    //int *arrayNew;
    //int newA;

    //Gets array size
    cout << "Please enter the array size!" << endl;
    cin >> userSize; 

    //Memory array 
    memory = new int [userSize];

    //Grab values for the amount of user size 
    for (int count = 0; count < userSize; count ++)
    {
        cout << "Please enter the value for " << count+1 << endl;
        cin >> userInts;
    }


    for (int index = 0; index < userSize; index ++)
    {
        cin >> memory[index];
    }

    //Sets addToSize function to memory array
    memory = addToSize(memory, userSize);

   //Shows memory array
    for(int index=0;index< (userSize + 1);index++)
        cout<<memory[index]<<endl;

    delete[] memory;    //Used to delete memory array
    memory = 0; //sets memory array to 0

    return 0;

}


    int* addToSize(int* arrayNew, int newSize) 
{
    int* expandSize= new int [newSize +1];

    for (int index = 0; index < newSize; index++)
    {
        expandSize[index]= arrayNew[index];
    }
    for (int index = newSize; index < (newSize+1); index ++) 
    {
        expandSize[index]=0;
    }   
    return expandSize;
}
p. sawyer
  • 1
  • 4

1 Answers1

0

You have declared a forward reference to some addToSize() function which accepts 2 arguments, and you proceed to invoke it from within your main(), and that's all very fine and dandy, but then further down you define an addToSize() which accepts 3 arguments.

So, what is happening is that the declared function is unrelated to the defined function. The compiler completely ignores the 3-argument function, since you are not calling it, and it assumes that you will provide a definition for the two-parameter function in some other source file.

But then when the linker attempts to link your program, there is no other file containing an addToSize() with two arguments, so the linker fails with an error message which describes precisely what happened. (It is a linker error. It has nothing to do with pointers and arrays.)

Hint: before fixing your function definition to match the declaration, lose the forward declaration, move the function to the start of the file, and make it static.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Okay so I have fixed the addToSize function and only put it to accept 2 arguments. The program now runs and asks the user for the size of the array and the values – p. sawyer Oct 11 '15 at 19:29
  • Great. Upvote the correct answer, accept it, and happy programming! – Mike Nakis Oct 11 '15 at 19:30
  • Question, how do I set the element of the first array to 0 and then copy elements 0 and 1 etc.. of the argument array to the new array? – p. sawyer Oct 11 '15 at 19:34