DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Functions and Array help (http://www.daniweb.com/forums/thread34800.html)

wdowell84 Nov 3rd, 2005 7:04 pm
Functions and Array help
 
I've had a terrible time with this program. I don't really understand it that much, but have been working on it for about 4 hours so far. One of the problems I'm having is that it keeps saying that local function definitions are illegal. Here's the program so far.


Input:
An integer array of ten numbers: 20,17,9,29,19,9,19,20,20,19
Compile-time array with initialization list of numbers.
NOTE: In main() function

Processing:
Function: computeAverage(const int[], const int):int
countLessThan(const int[], const int, const int):int

Output:
Function: display(const int, const int):void
Display as shown in sample output.

*/


#include <iostream>
using namespace std;

void main(void)
{
    // constants
    const int SIZE = 10;

    // function prototype(s)
    // return by value is used
    int computeAverage(const int[], const int);
    int countLessThan(const int[], const int, const int);
    void display(const int, const int);

    // local data
    int intAverage = 0;
    int count = 0;

    // data declaration(s) & initialization(s)
    // array data: use data from sample output above
    // TODO: declare & initialize array of ten integers
    int numbers[] = {20,17,9,29,19,9,19,20,20,19};

    // start the program
    cout << "*** start of 276Arrays_Ex02.cpp program ***" << endl;
    cout << endl;

    // Compute & return integer average of the array
    // TODO: call computeAverage()function,
    //  which returns average of numbers
    intAverage = computeAverage(numbers, SIZE);

    // Compute & return count of array values less than integer average
    // TODO: call countLessThan() function,
    //  which returns count of numbers less than the average
    count = countLessThan(numbers, intAverage, SIZE);

    // display the required output
    // TODO: call display function,
    //  passing integer average & count of numbers less than average
    display( intAverage, count);

    // terminate the program
    cout << endl;
    cout << endl;
    cout << "*** end of 276Arrays_Ex02.cpp program ***" << endl << endl;
    cin.get();
    return;
}  // end main()

//--------------------------------------------------------------------//
// Function Name: display(const int, const int):void
// Purpose: to display integer average & count less than average
// Values received: integer average, count
// Values returned: <none>
// Notes:
//    display label "Average Integer Score: "
//    display integer average
//    display label "Count of Integers Less Than Average: "
//    display count
//--------------------------------------------------------------------//
// TODO: code the display function
void display(const int num[], const int size)
{

  // display labels and data


// TODO: code the computeAverage function
int computeAverage(int num[], int size)
{
  // local variables (if needed)
        int sum;


  // compute sum
        sum += numbers;

  // compute integer average
        for(int index = 0; index < size; index++)
                sum += num[index];
                average = sum / size;

  // return integer average
  return intAverage;
}


// TODO: code the countLessThan function
int countLessThan(const int numbers[],
                  const int intAverage,
                  const int SIZE)
{
  // local variables (if needed)
        int count;

  // count number of values less than average

  // return count
  return count;
}
<< moderator edit: added code tags: [code][/code] >>

Dave Sinkula Nov 3rd, 2005 9:30 pm
Re: Functions and Array help
 
Quote:

Originally Posted by wdowell84
One of the problems I'm having is that it keeps saying that local function definitions are illegal. Here's the program so far.

Because of bad indenting, you can't see that you've actually got this:
// TODO: code the display function
void display(const int num[], const int size)
{

  // display labels and data


// TODO: code the computeAverage function
  int computeAverage(int num[], int size)
  {
The error message says what is true: you can't define a function within another function.


All times are GMT -4. The time now is 8:34 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC