0

I am having issues finishing passing an array via pointers through a series of functions. I create a function using dynamic allocation to create it. Even though that is successful I cannot get it to pass through functions that take pointers as arguments. The functions return the mean median and mode and have been completed. However I cannot pass them when converting them into pointer syntax. Thanks for the help in advance.

#include <iostream>
#include <cstdlib>


using namespace std;
int students;
int * studentarray;
int  stumode;
double  stuavg;
int  stumed;
int arr;


int mode(int *[], int );
double average(int *[], int);
double median(int *[], int);
void selectSort(int [], int);
void swap(int *, int *);
int makeArray(int*, int);

int main()
{
    studentarray = &arr;
    cout << "How many students are there?" << endl;
    cin >> students;

    makeArray(studentarray, students);

    for (int i = 0; i < students; i++) {
        cout << "How many movies did student " << i + 1 << " view?" << endl;
        cin >> studentarray[i];
    }

    selectSort(studentarray, students);
    stumode = mode(&studentarray, students);
    stuavg = average(&studentarray, students);
    stumed = median(&studentarray, students);

    cout << "The array has been sorted in ascending order." << endl;
    cout << "The mode is " << stumode << "." << endl;
    cout << "The mean is " << stuavg << "." << endl;
    cout << "The median is " << stumed << "." << endl;

    delete[] studentarray;

    return 0;
}

int mode(int *arr, int size)
{
    if (size <= 0) return 0;

    int most = 0, position = 0, most_count = 0;
    int counter = 1;
    for (int i = 1; i < size; i++) 
    {
        if (* (arr +  i) != * (arr + position) ) 
        {
            if (counter > most)
            {
                most = counter;
                most_count = 0;
            }
            else if (counter == most) most_count++;
            position = i;
            counter = 0;
        }
        else counter++;
    }
    if (most_count) return 0;
    else return * ( arr + position );
}

double average(int *arr, int size)
{
    if (size <= 0) return 0;
    int total = 0;
    for (int i = 0; i < size; i++) {
        total += *(arr + i);
    }
    return (double)total / size;
}

double median(int *arr, int size)
{
    if (size <= 0) return 0;
    if (size % 2 == 0)
        return (double) (* (arr + (size + 1) / 2));
    else {
        int mid = size / 2;
        return (double)(* (arr + mid) + * (arr + mid + 1) / 2);
    }
    return 0;
}


void selectSort(int arr[], int size)
{
    int min;
    for (int i = 0; i < size - 1; i++)
    {
        min = i;
        for (int j = i + 1; j < size; j++)
        {
            if ( arr[j] < arr[min])
            {
                min = j;
            }
        }
        swap(&arr[min], &arr[i]);
    }

 }

void swap(int *one, int *two) {
    int temp = *one;
    *one = *two;
    *two = temp;
}

int makeArray(int *arr, int size)
{
    arr = new int[size];
    return *arr;
}

1 Answer 1

1

Your implementation of makeArray is not right.

int makeArray(int *arr, int size)
{
    // Allocates memory and assigns it to arr.
    // This is a local change to arr. The value of the variable in
    // main remains unchanged.
    arr = new int[size];

    // Returns an uninitialized value.
    return *arr;

    // The memory allocated in the previous line is now a memory leak.
}

You can make it simpler by using:

int* makeArray(int size)
{
   return new int[size];
}

and use it in main as:

arr = makeArray(students);

However, I don't see how that is better than using:

arr = new int[students];

If you do that, makeArray becomes unnecessary. If makeArray needs to have additional code to fill up the array with some values, it will be useful. Otherwise, it does not add any useful functionality to your program.


Having said all of that, it is better to use std::vector instead of managing dynamically allocated memory in your own code. You would use:

std::vector<int> arr(students);

PS

I did not go through rest of your code. There might be other errors.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the lecture on the complication of my simplicity. I restarted my visual studio and that took care of that. Then I was able to debug it on my own for once. Thank you.
@BrianMoore, you are welcome. Glad I was able to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.