Thread: array problem

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    array problem

    I need help fixing a problem with an array.

    The code is as follows:

    Code:
    #include <iostream>
    using namespace std;
    
    int max(int a[]) {
    int maximum = a[0];
    for (int i=1; i < sizeof(a); i++) {
    if (a[i] > maximum) {
    maximum = a[i];
    }
    }
    return maximum;
    }
    
    int main()
    {
    int j=1;
    int total=0;
    cout << "Please enter the amount of numbers you want to calculate: ";
    cin >> j;
    int numbers[j];
    
    
    for (int i=0; i<j; i++)
    {
    cout << "Please enter number " << i << ":" << endl;
    cin >> numbers[i];
    total=total+numbers[i];
    }
    double average;
    int findmax;
    average=total/j;
    findmax=max(numbers);
    
    cout << average;
    
    cout << findmax;
    
    
    return 0;
    }


    I have a problem with this line:

    Code:
    int numbers[j];
    it gives me these errors:

    j:\MaxAndAvg\MaxAndAvg.cpp(20): error C2057: expected constant expression
    j:\MaxAndAvg\MaxAndAvg.cpp(20): error C2466: cannot allocate an array of constant size 0
    j:\MaxAndAvg\MaxAndAvg.cpp(20): error C2133: 'numbers' : unknown size

    All this needs to do is ask the user for how many numbers they want to calculate, add them up, then get the average and the largest number. Thanks for any and all help!

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Thou shalt not attempt to create an array of variable size
    Code:
    cout << "Please enter the amount of numbers you want to calculate: ";
    cin >> j;
    int numbers[j];  <-- illegal in c++

    Your options are:

    1. declare an array of an arbitrary fixed size
    Code:
    int numbers[100];
    2. use a dynamic array and create an array of any given size at runtime
    Code:
    cout << "Please enter the amount of numbers you want to calculate: ";
    cin >> j;
    int *numbers = new int[j];
    3. Use an STL object such as <vector>. A vector class object simulates a resizable array.
    Code:
    #include<vector>
    
    vector<int> numbers;
    
    int input = 0;
    cout << "Please enter number " << i << ":" << endl;
    cin >> input;
    numbers.push_back(input);
    Last edited by The Brain; 03-30-2007 at 07:53 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    When you pass a to max(), it gets converted to a pointer, so sizeof(a) will just return the size of the pointer, not the array as you intended. You need to pass the size of the array to max() as a second argument.

    Edit: You should also initialize maximum to INT_MIN (or the long-winded but macro-free numeric_limits<int>::min()) for sensible behavior in case the array's size is 0.

    Edit: Also, if the array's size is 0, then a[0] doesn't even exist, so you can't use it. Initialize to one of the above and then loop from 0 to size-1.

    Edit: Better yet, just use max() from the Standard Library.
    Last edited by robatino; 03-30-2007 at 07:58 PM.

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Take out the line
    Code:
    int numbers[j];
    And the program will work.

    There is no need to use an array there.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    He needs to store all the numbers in order to call max() later (though the array could be avoided just by computing the maximum on the fly instead).

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I realized that j must be > 0, since the average of 0 numbers is 0/0 which is indeterminate. So just compute a running total and maximum as the numbers are entered in, and at the end, divide the total by j to get the average (remember that when dividing two ints, the result is truncated to an int, so convert either total or j to double before dividing). Since j > 0, you can just initialize total and maximum to the first number you enter. Forget about the INT_MIN stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM