User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,430 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,927 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2250 | Replies: 1
Reply
Join Date: Sep 2005
Posts: 4
Reputation: wdowell84 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wdowell84 wdowell84 is offline Offline
Newbie Poster

Functions and Array help

  #1  
Nov 3rd, 2005
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] >>
Last edited by Dave Sinkula : Nov 3rd, 2005 at 9:28 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,473
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Functions and Array help

  #2  
Nov 3rd, 2005
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC