Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6891321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:23:43+00:00 2026-05-27T06:23:43+00:00

This is from a beginning C++ class, no grade involved since I’m simply following

  • 0

This is from a beginning C++ class, no grade involved since I’m simply following along trying to remember stuff after too many years. The class has reached the point where we’re using pointers and dynamic arrays. I’m trying to pass the array (or the pointer) to different functions having to do with various calculations on a list of integers. Some of the functions are working and others aren’t. But I’m passing them all the same way and I’m calling them all the same way as well. Since some of the functions are returning garbage values, it feels like I’m not actually pointing where I think I am AND the functions that are working are only working by accident.

Before someone helpfully suggests that I should be using vectors and not arrays, the point of the exercise is to use dynamically allocated arrays.

Here’s my code so far. It’s the findavg() and findmedian() that are returning garbage.

#include <iostream>
#include <iomanip>

using namespace std;

void getarray(int *a, int &l)
{
    int input = 0;

    //-1 is not important here, just to make the while loop run
    while(input != '-1') 
    {
        cout << "Enter length of the list of numbers: ";

        if(!(cin >> input))
        {
            cout << "Please enter numeric characters only." << "\n";    
            //show error if they don't enter a number
            cin.clear();                                                
            //get rid of invalid characters
            cin.ignore(10000,'\n');
        }

        else if(input <= 0)
        {
            cout << "Please enter a number greater than 0." << "\n";    
            //show error if they enter a non-positive number
            cin.clear();                                                
            //get rid of invalid characters
            cin.ignore(10000,'\n');
        }
        else
        {
            l = input;  //input is a positive number
            break; //got good input, break out of while loop
        }

    }


    int i;
    int x; 

    cout << "Enter " << l << " integers on one line seperated by spaces.\n";


    for( i = 0; i < l  &&  cin >> x; ++i)
    {
        a[i] = x;
    }

}

void printarray(int *a, int &l)
{
    int i;

    cout << "The array of integers is:\n";
    for( i = 0; i < l; ++i)
        cout << setw(4) << a[i];
    cout << "\n";

}

int findmin(int *a, int &l)
{
    int min = 0;
    min = a[0]; 

    for(int i = 1; i<l; i++)
    {
        if(a[i] < min)
            min = a[i];
    }
    return min; 
}

int findmax(int *a, int &l)
{
    int max = 0;
    max = a[0];

    for(int i = 1; i<l; i++)
    {
        if(a[i] > max)
            max = a[i];
    }
    return max; 
}

float findavg(int *a, int &l)
{
    int total = 0;
    total = a[0];

    for(int i = 1; i<l; i++)
    {
        total = total + a[i];
    }

    return static_cast<float>(total/static_cast<float>(l));

}

float findmedian(int *a, int &l)
{
    int max = 0;
    int min = 0;

    max = findmax(a, l);
    min = findmin(a, l);

    return static_cast<float>((max + min)/2.0);

}


int main()
{

    int length = 0;
    int *an_array;

    an_array = new int[length];

    getarray(an_array, length);

    printarray(an_array, length);

    cout << "Maximum value in list is: "<< findmax(an_array, length) << "\n";
    cout << "Minimum value in list is: "<< findmin(an_array, length) << "\n";
    printf("Average is: %10.5f", findavg(an_array, length));
    cout << "\n";
    printf("Median is: %10.5f", findmedian(an_array, length));
    cout << "\n";

    delete [] an_array;

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:23:44+00:00Added an answer on May 27, 2026 at 6:23 am
    int length = 0;
    int *an_array;
    
    an_array = new int[length]; //What is this ?
    

    You’re allocating zero bytes for an_array, because length is 0. That is one problem I can see.

    You should allocate memory for an_array in getarray function, after reading the length of array:

    void getarray(int * &a, int &l) //see the change here!
    {
        //code which reads length is omitted for brevity
    
        a = new int[l]; //alllocate memory after reading length
    
        //now read array elements here
    }
    

    Alternatively,you can write a struct as:

    struct array
    {
          int *data;
          size_t size;
    };
    

    And then use this everywhere. It is better because you tied the size and data together, and instead of using two independent objects, you can use just one object of type array, as described below.

    array getarray() 
    {
        array arr;
    
        //read length into arr.size
    
        arr.data= new int[arr.size]; //alllocate memory after reading length
    
        //now read array elements into arr.data
    
        return arr; //returning arr means you're returning both: data and size!
    }
    

    And then call this as:

     array arr = getarray(); //no need to pass any argument!
    

    Also, if you’re going to use this, then other functions signature would become:

    void printarray(array arr);
    int findmax(array arr);
    float findavg(array arr);
    float findmedian(array arr);
    

    Now you need to modify these functions as well, but fortunately, there will not be major change : wherever you’re using a and l, use arr.data and arr.size respectively instead. And you’re done!

    With all these improvements, your main() would become this:

    int main()
    {
        array arr = getarray();
    
        printarray(arr);
    
        cout << "Maximum value in list is: "<< findmax(arr) << "\n";
        cout << "Minimum value in list is: "<< findmin(arr) << "\n";
    
        printf("Average is: %10.5f\n", findavg(arr));
        printf("Median is: %10.5f\n", findmedian(arr));
    
        delete [] arr.data;
        return 0;
    }
    

    This looks better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Introduction After watching this video from LIDNUG, about .NET code protection http://secureteam.net/lidnug_recording/Untitled.swf (especially from
I'm a Java novice taking a beginning programming class and I'm trying to finish
This (from body of a stored proc) is throwing a syntax error: IF (name
I see this from time to time and want to know what it is.
Say I write this: from subprocessing import Popen, STDOUT, PIPE p = Popen([myproc], stderr=STDOUT,
I'm coming at this from the Windows world... On Windows, we have Windows Installer
How can I stop this from happening, it is grabbing files in .svn folders
I got this from php.net website. This is related to the problem I am
I found this from C++FAQ Generally, No. From a member function or friend of
Having issues referencing $(this) from within a the nested ajax 'success' function... I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.