Hello, 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. 8.1.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.

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

int* addToSize (int*, int, int);

using namespace std;


int main()
{   

    int userSize=0;
    int userInts;
    int *memory; //dynamically allocate an array


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

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

    memory = new int [userSize];


    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];
    }

    memory = addToSize(memory, userSize);

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

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

    return 0;

}


    int* addToSize(int* arrayNew, int newSize, int userSize) 
{
    int* expandSize= new int [userSize +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;
}

Recommended Answers

All 2 Replies

I think the project is not well worded ...

and also, it seems to me to be poorly thought out,
since creating an 'hole' for a new value ...
at the front of an enlarged array ...
this is usually handled by an 'insert' function
or ...
in a linked-list ... by a push_front function.

But ultimately here ...it seems you are to maintain 2 dynamic arrays of integers ...

For the first one, you need to firstly prompt and have the user input the size
then (prompt and) input that many integers in a loop to fill up all the slots

Then get new memory to hold an array of int's that has size+1 slots

Then the thing to do now, since all the values in array one at index 0..(size-1)
are to be copied to array 2 at index 1..size ... is to just copy them over ...

Then set value at slot with index 0 in array 2 to value = 0

Then you need to use functions for each of ...

getMode, getMedian and getAverage ... and also showAry

but for getAverage, getMedian, getMode, you will be working on 'ary one'

double getAverage( const int* ary, int size )
{
    double sum = 0;
    // sum up
    return sum/size;
}

Then could sort array one in increasing order ...
(a simple bubble sort would do ok for small size arrays used here)
and report middle value as median if size is an odd number ... index i = size/2
BUT if size was an even number ...
median can be found as average of values at index i = size/2 and at index i-1

To find most freq number (mode ... if one exists) ...
since array is sorted ...
traverse the array ...
count duplicate values
the value with greatest count of duplicates, if it exists ... is the mode
if not exists could report -1 index and error mesage

// returns index of mode
int getMode( const int* ary, int size )
{
    // your code goes here
}

This (working) example may give you some more ideas about how to get started ...

// meanMedianMode.cpp //

// a start //


#include <iostream>

using namespace std;

const int MIN_SIZE = 5;


int takeInInt( const char* msg )
{
    int val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << "Invalid input ... numbers only!\n";
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}

void print( const int* ary, int size )
{
    int i;
    for( i = 0; i < size; ++ i )
    {
        cout << ary[i] << '\t';
        if( (i+1) % 8 == 0 ) cout << endl;
    }
    if( i % 8 != 0 ) cout << endl;
}

double getAverage( const int* ary, int size )
{
    double sum = 0;
    for( int i = 0; i < size; ++ i )
        sum += ary[i];

    return sum/size;
}




int main()
{
    int size = 0;

    for( ; ; )
    {
        size = takeInInt( "Please enter the array size: " );
        if( size >= MIN_SIZE ) break;
        cout << "Try again ... size must be >= " << MIN_SIZE << " ...\n";
    }

    int* ary1 = new int [size];
    int* ary2 = new int [size+1];
    ary2[0] = 0;

    // take in new values ... and put into correct places


    for( int i = 0; i < size; ++ i )
    {
        cout << "For 'int' " << i+1 << ", ";
        ary1[i] = takeInInt( "please enter an integer: " );
        ary2[i+1] = ary1[i];
    }

    cout << "showing array 1 ...\n";
    print( ary1, size );

    cout << "showing array 2 ...\n";
    print( ary2, size+1 );

    cout << "Average of array 1 is "
         << getAverage( ary1, size ) << '\n';


    delete [] ary2;
    delete [] ary1;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.