Where is the error?

J

JOYCE

Please help me,I don't know where the error is.The following is my
programming.
#include<stdio.h>
#define SIZE 99
void mean (const int answer[]);
void median(int answer[]);
void mode(int freq[],const int answer[]);
void bubbleSort (int a[]);
void printArray(const int a[]);
int main(){
int frequency[10]={0};
int response[SIZE]=
{ 6,7,8,9,8,7,8,9,8,9,
7,8,9,5,9,8,7,8,7,8,
6,7,8,9,3,9,8,7,8,7,
7,8,9,8,9,8,9,7,8,9,
6,7,8,7,8,7,9,8,9,2,
6,7,8,9,8,7,8,9,8,9,
7,8,9,5,9,8,7,8,7,8,
6,7,8,9,3,9,8,7,8,7,
7,8,9,8,9,8,9,7,8,9,
6,7,8,7,8,7,9,8,9};
mean (response);
median(response);
mode(frequency,response);
return 0;
}
void mean(const int answer[])
{
int j;
int total=0;
printf("%s\n%s\n%s\n","********","Mean","********");
for(j=0;j<=SIZE-1;j++){
total+=answer[j];
printf("The mean is the average value of the data\n"
"items.The mean is equal to the total of\n"
"all the data items divided by the number\n"
"of data items(%d).The mean value for\n"
"this run is:%d/%d=%.4f\n\n",
SIZE,total,SIZE,(double)total/SIZE);
}

void median(int answer[])
{
printf("\n%s\n%s\n%s\n%s",
"********","Median","********",
"The unsorted array of responses is");
printArray(answer);
bubbleSort(answer);
printf("\n\nThe sorted array is");
printArray(answer);
printf("\n\nThe median is element %d of\n"
"the sorted %d element array.\n"
"For this run the median is %d\n\n",
SIZE/2,SIZE,answer[SIZE/2]);
}

void mode(int freq[],const int answer[])
{
int rating;
int j;
int h;
int largest=0;
int modeValue=0;
printf("\n%s\n%s\n%s\n",
"********","Mode","********");
for(rating=1;rating<=9;rating++){
freq[rating]=0;
}
for(j=0;j<=SIZE-1;j++){
++freq[answer[j]];
}

printf("%s%11s%19s\n\n%54s\n%54s\n\n",
"Response","Frequency","Histogram",
"1 1 2 2","5 0 5 0 5");
for(rating=1;rating<=9;rating++){
printf("%8d%11d ",rating,freq[rating]);
if(freq[rating]>largest){
largest=freq[rating];
modeValue=rating;
}
for(h=1;h<=freq[rating];h++){
printf("*");
}
printf("\n");
}
printf("The mode is the mostfrequent value.\n"
"For this run the mode is %d which occoured"
"%d times.\n",modeValue,largest);
}

void bubbleSort(int a[])
{
int pass;
int j;
int hold;
for(pass=1;pass<=SIZE-1;pass++)
{
for(j=0;j<=SIZE-2;j++){
if(a[j]>a[j+1]){
hold=a[j];
a[j]=a[j+1];
a[j+1]=hold;
}
}
}
}

void printArray(const int a[])
{
int j;
for(j=0;j<=SIZE-1;j++){
if(j%20==0){
printf("\n");}
printf("%2d",a[j]);
}
}
error C2601: 'median' : local function definitions are illegal
error C2601: 'mode' : local function definitions are illegal
error C2601: 'printArray' : local function definitions are illegal
error C2601: 'bubbleSort' : local function definitions are illegal
I can't understand it.
 
N

Nick Keighley

Please help me,I don't know  where the error is.The following is my
programming.

are you SURE the code you poseted is the code you compiled.
I get a couple of errors and warnings but they are completly different
from yours.
#include<stdio.h>

your layout is odd use
#include <stdio.h>

in general use more whitespace (both vertical and horizontal
void mode(int freq[],const int answer[]);

void mode (int freq[], const int answer[]);
void bubbleSort (int a[]);

note the above declaration of bubbleSort()
int main(){

int main (void)
{



void mean(const int answer[])
{
        int j;
        int total=0;
        printf("%s\n%s\n%s\n","********","Mean","********");
        for(j=0;j<=SIZE-1;j++){
                total+=answer[j];
                printf("The mean is the average value of the data\n"
                           "items.The mean is equal to the total of\n"
                           "all the data items divided by the number\n"
                           "of data items(%d)..The mean value for\n"
               "this run is:%d/%d=%.4f\n\n",
                           SIZE,total,SIZE,(double)total/SIZE);
        }

missing }

void median(int answer[])

void bubbleSort(int a[])

definition of bubbleSort(). How is this different from the earlier
declaration. Note you avoid many of these problems by defing before
using. Put main() at the end of your source file and the functions
it calls before it, etc.
 
M

maverik

void mean (const int answer[]);
void median(int answer[]);
void mode(int freq[],const int answer[]);
void bubbleSort (int a[]);
void printArray(const int a[]);
error C2601: 'median' : local function definitions are illegal
error C2601: 'mode' : local function definitions are illegal
error C2601: 'printArray' : local function definitions are illegal
error C2601: 'bubbleSort' : local function definitions are illegal

Hmm.
1. It's common technique to declare function prototypes in header
files (.h)
2. It's common technique not to use arguments names in function
prototype. So, you can try this:

void mean (const int *);
void median(int *);
void mode(int * ,const int *);
void bubbleSort (int *);
void printArray(const int *);

But in your case looks like you miss the bracket "}" for mean()
function
 
M

maverik

2. It's common technique not to use arguments names in function
prototype. So, you can try this:

void mean (const int *);
void median(int *);
void mode(int * ,const int *);
void bubbleSort (int *);
void printArray(const int *);

Ups. I miss one thing. So
void mean (const int []);
void median(int []);
void mode(int [] ,const int []);
void bubbleSort (int []);
void printArray(const int []);
 
J

James Kuyper

JOYCE said:
Please help me,I don't know where the error is.The following is my
programming.
#include<stdio.h>
#define SIZE 99
void mean (const int answer[]);
void median(int answer[]);
void mode(int freq[],const int answer[]);
void bubbleSort (int a[]);
void printArray(const int a[]);

The problem you're having is indirectly caused by your use of an
inconsistent style with respect to the placement of curly brackets ('{'
and '}'). There's no one "right" style, though some people passionately
claim otherwise. However, you must learn to use a consistent style,
otherwise you're going to have a hard time preventing errors like this.

In this function, you put the '{' right after the function name, and the
matching '}' at the very start of the final line.
int main(){
int frequency[10]={0};
int response[SIZE]=
{ 6,7,8,9,8,7,8,9,8,9,
7,8,9,5,9,8,7,8,7,8,
6,7,8,9,3,9,8,7,8,7,
7,8,9,8,9,8,9,7,8,9,
6,7,8,7,8,7,9,8,9,2,
6,7,8,9,8,7,8,9,8,9,
7,8,9,5,9,8,7,8,7,8,
6,7,8,9,3,9,8,7,8,7,
7,8,9,8,9,8,9,7,8,9,
6,7,8,7,8,7,9,8,9};
mean (response);
median(response);
mode(frequency,response);
return 0;
}

In this function, you put the '{' at the start of a line all by itself,
and put the '}' ... nowhere! That is the problem.
void mean(const int answer[])
{
int j;
int total=0;
printf("%s\n%s\n%s\n","********","Mean","********");
for(j=0;j<=SIZE-1;j++){
total+=answer[j];
printf("The mean is the average value of the data\n"
"items.The mean is equal to the total of\n"
"all the data items divided by the number\n"
"of data items(%d).The mean value for\n"
"this run is:%d/%d=%.4f\n\n",
SIZE,total,SIZE,(double)total/SIZE);
}

You forgot to enter the '}' that should have terminated the definition
of mean(). As a result, you are attempting to define all of the
following functions inside of mean(), something that is not permitted in C.

In the next two functions, you put the '{' on it's own line, but
indented, and the corresponding '}' on it's own line, indented to the
same level:
void median(int answer[])
{
printf("\n%s\n%s\n%s\n%s",
"********","Median","********",
"The unsorted array of responses is");
printArray(answer);
bubbleSort(answer);
printf("\n\nThe sorted array is");
printArray(answer);
printf("\n\nThe median is element %d of\n"
"the sorted %d element array.\n"
"For this run the median is %d\n\n",
SIZE/2,SIZE,answer[SIZE/2]);
}

void mode(int freq[],const int answer[])
{
int rating;
int j;
int h;
int largest=0;
int modeValue=0;
printf("\n%s\n%s\n%s\n",
"********","Mode","********");
for(rating=1;rating<=9;rating++){
freq[rating]=0;
}
for(j=0;j<=SIZE-1;j++){
++freq[answer[j]];
}

printf("%s%11s%19s\n\n%54s\n%54s\n\n",
"Response","Frequency","Histogram",
"1 1 2 2","5 0 5 0 5");
for(rating=1;rating<=9;rating++){
printf("%8d%11d ",rating,freq[rating]);
if(freq[rating]>largest){
largest=freq[rating];
modeValue=rating;
}
for(h=1;h<=freq[rating];h++){
printf("*");
}
printf("\n");
}
printf("The mode is the mostfrequent value.\n"
"For this run the mode is %d which occoured"
"%d times.\n",modeValue,largest);
}

In this function you put the '{' on it's own line, indented, but you put
the corresponding '}' at the beginning of the line.
void bubbleSort(int a[])
{
int pass;
int j;
int hold;
for(pass=1;pass<=SIZE-1;pass++)
{
for(j=0;j<=SIZE-2;j++){
if(a[j]>a[j+1]){
hold=a[j];
a[j]=a[j+1];
a[j+1]=hold;
}
}
}
}

Here you revert to the same style you used for median() and mode():
void printArray(const int a[])
{
int j;
for(j=0;j<=SIZE-1;j++){
if(j%20==0){
printf("\n");}
printf("%2d",a[j]);
}
}

Personally, I don't like that style because excessive indentation makes
it hard to fit code on a single line. But having any reasonable,
consistent style for placing brackets is much better than inconsistently
switching between two or more different styles, no matter how
well-chosen those styles are.
 
M

Martien Verbruggen

void bubbleSort (int a[]);

note the above declaration of bubbleSort()
void bubbleSort(int a[])

definition of bubbleSort(). How is this different from the earlier
declaration. Note you avoid many of these problems by defing before
using. Put main() at the end of your source file and the functions
it calls before it, etc.

The only difference I see is a space. Why is there a problem, you think?

Martien
 
M

Martien Verbruggen

Please help me,I don't know where the error is.The following is my
programming.

You're missing a closing bracket for the body of mean(), before the
definition of median(). Your compiler is trying to tell you that you
can't define functions inside other functions.

Martien
 
N

Nick Keighley

void bubbleSort (int a[]);
note the above declaration of bubbleSort()
void bubbleSort(int a[])
definition of bubbleSort(). How is this different from the earlier
declaration. Note you avoid many of these problems by defing before
using. Put main() at the end of your source file and the functions
it calls before it, etc.

The only difference I see is a space. Why is there a problem, you think?

I was a line out. I could have sworn the bubbleSort() declaration
had a const in it. My compiler complained about a mismatch between
formal
and actual parameters. Which of course goes away when I put the
curly bracket in.

If he used a more consistent layout style he'd have less problems.
 
N

Nick Keighley

void mean (const int answer[]);
void median(int answer[]);
void mode(int freq[],const int answer[]);
void bubbleSort (int a[]);
void printArray(const int a[]);
error C2601: 'median' : local function definitions are illegal
error C2601: 'mode' : local function definitions are illegal
error C2601: 'printArray' : local function definitions are illegal
error C2601: 'bubbleSort' : local function definitions are illegal

Hmm.
1. It's common technique to declare function prototypes in header
files (.h)

but only if the functions are shared between translation units.
If the functions are local to a single TU (file) then I don't
put declarations in unless I need them (for mutually recursive
functions) and if I do need them I don't put them in a header file
I put them in the C file. Occam's razor applies to programming as
well.

2. It's common technique not to use arguments names in function
prototype. So, you can try this:

void mean (const int *);
void median(int *);
void mode(int * ,const int *);
void bubbleSort (int *);
void printArray(const int *);

what does that buy you? In fact I find that pretty obscure.
I preferred his version to yours. But even better would be
remove them completly

But in your case looks like you miss the bracket "}" for mean()
function

yep
 
J

James Kuyper

Nick said:
what does that buy you? In fact I find that pretty obscure.

In a header file, that approach prevents any conflicts that might arise
from user code #including the header file after #defining a macro with
the same name as the dummy argument name. This is only a minor issue if
everyone follows the convention that macro names are all in UPPERCASE
and all other identifiers have at least one lowercase letter - but not
everyone does follow that convention.
I preferred his version to yours. But even better would be
remove them completly

Agreed.
 
N

Nick Keighley

In a header file, that approach prevents any conflicts that might arise
from user code #including the header file after #defining a macro with
the same name as the dummy argument name. This is only a minor issue if
everyone follows the convention that macro names are all in UPPERCASE
and all other identifiers have at least one lowercase letter - but not
everyone does follow that convention.

I tend to want the function declarations in a header file
to be documentation. Ideally I'd like people to be able
to use my library by reading the .h file and without reading
the .c file (they can read it if they want to but they don't
have to). Often the type of a parameter is enough to explain what
it's for. Though they may have to look at the comment attached to the
type
definition.

Success send (Port*, const Message*);

doesn't really need much explanation. But as soon as you start
dropping in basic types you tend to need more information.

I know a lot of people prefer
void mean (const int *);

but I like
void mean (const int []);

at least I know he intends to pass an array of items.

The wisdom (or lack) of

1. not returning a value
2. printing the result in the "function"
3. using a global for the array size
4. calling the input parameter "answer"

are of course a side issues.

double mean (const int values[], size_t size);


--
Nick Keighley

Your colleagues have been successfully briefed for years on
the progress of Special Projects without their ever having
any idea what they actually are.
 
W

Willem

JOYCE wrote:
) Please help me,I don't know where the error is.The following is my
) programming.

I would like to have answered 'the error is on line 42'
but unfortunately it's off by three lines.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
M

maverik

void mean (const int answer[]);
void median(int answer[]);
void mode(int freq[],const int answer[]);
void bubbleSort (int a[]);
void printArray(const int a[]);
error C2601: 'median' : local function definitions are illegal
error C2601: 'mode' : local function definitions are illegal
error C2601: 'printArray' : local function definitions are illegal
error C2601: 'bubbleSort' : local function definitions are illegal
Hmm.
1. It's common technique to declare function prototypes in header
files (.h)

but only if the functions are shared between translation units.
If the functions are local to a single TU (file) then I don't
put declarations in unless I need them (for mutually recursive
functions) and if I do need them I don't put them in a header file
I put them in the C file. Occam's razor applies to programming as
well.

Yes, this is my mistake. I'm agree with you.
what does that buy you? In fact I find that pretty obscure.
I preferred his version to yours.

See my second post:

Ups. I miss one thing. So
void mean (const int []);
void median(int []);
void mode(int [] ,const int []);
void bubbleSort (int []);
void printArray(const int []);
But even better would be remove them completly
Yes, good idea. I miss this. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top