Where is the error?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JOYCE

    Where is the error?

    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(cons t 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",
    "********","Med ian","********" ,
    "The unsorted array of responses is");
    printArray(answ er);
    bubbleSort(answ er);
    printf("\n\nThe sorted array is");
    printArray(answ er);
    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",
    "********","Mod e","******** ");
    for(rating=1;ra ting<=9;rating+ +){
    freq[rating]=0;
    }
    for(j=0;j<=SIZE-1;j++){
    ++freq[answer[j]];
    }

    printf("%s%11s% 19s\n\n%54s\n%5 4s\n\n",
    "Response","Fre quency","Histog ram",
    "1 1 2 2","5 0 5 0 5");
    for(rating=1;ra ting<=9;rating+ +){
    printf("%8d%11d ",rating,fr eq[rating]);
    if(freq[rating]>largest){
    largest=freq[rating];
    modeValue=ratin g;
    }
    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",modeV alue,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(cons t 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.
  • Nick Keighley

    #2
    Re: Where is the error?

    On 13 Nov, 11:30, JOYCE <zzzzzz90...@12 6.comwrote:
    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)
    {



    <snip>
    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 isequal 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[])
    <snip>
    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.


    --
    Nick Keighley

    Comment

    • maverik

      #3
      Re: Where is the error?

      On Nov 13, 2:30 pm, JOYCE <zzzzzz90...@12 6.comwrote:
      void mean (const int answer[]);
      void median(int answer[]);
      void mode(int freq[],const int answer[]);
      void bubbleSort (int a[]);
      void printArray(cons t 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(cons t int *);

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

      Comment

      • maverik

        #4
        Re: Where is the error?

        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(cons t int *);
        Ups. I miss one thing. So
        void mean (const int []);
        void median(int []);
        void mode(int [] ,const int []);
        void bubbleSort (int []);
        void printArray(cons t int []);

        Comment

        • James Kuyper

          #5
          Re: Where is the error?

          JOYCE wrote:
          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(cons t 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",
          "********","Med ian","********" ,
          "The unsorted array of responses is");
          printArray(answ er);
          bubbleSort(answ er);
          printf("\n\nThe sorted array is");
          printArray(answ er);
          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",
          "********","Mod e","******** ");
          for(rating=1;ra ting<=9;rating+ +){
          freq[rating]=0;
          }
          for(j=0;j<=SIZE-1;j++){
          ++freq[answer[j]];
          }
          >
          printf("%s%11s% 19s\n\n%54s\n%5 4s\n\n",
          "Response","Fre quency","Histog ram",
          "1 1 2 2","5 0 5 0 5");
          for(rating=1;ra ting<=9;rating+ +){
          printf("%8d%11d ",rating,fr eq[rating]);
          if(freq[rating]>largest){
          largest=freq[rating];
          modeValue=ratin g;
          }
          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",modeV alue,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(cons t 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.

          Comment

          • Richard Heathfield

            #6
            Re: Where is the error?

            JOYCE said:
            Please help me,I don't know where the error is.
            Your mean() function is missing a closing brace.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Martien Verbruggen

              #7
              Re: Where is the error?

              On Thu, 13 Nov 2008 03:41:50 -0800 (PST),
              Nick Keighley <nick_keighley_ nospam@hotmail. comwrote:
              On 13 Nov, 11:30, JOYCE <zzzzzz90...@12 6.comwrote:
              >
              >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
              --
              |
              Martien Verbruggen | If at first you don't succeed, try again.
              | Then quit; there's no use being a damn fool
              | about it.

              Comment

              • Martien Verbruggen

                #8
                Re: Where is the error?

                On Thu, 13 Nov 2008 03:30:21 -0800 (PST),
                JOYCE <zzzzzz90829@12 6.comwrote:
                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
                --
                |
                Martien Verbruggen | Since light travels faster than sound, is
                | that why some people appear bright until you
                | hear them speak?

                Comment

                • Nick Keighley

                  #9
                  Re: Where is the error?

                  On 13 Nov, 11:56, Martien Verbruggen <m...@heliotrop e.com.auwrote:
                  On Thu, 13 Nov 2008 03:41:50 -0800 (PST),
                          Nick Keighley <nick_keighley_ nos...@hotmail. comwrote:
                  On 13 Nov, 11:30, JOYCE <zzzzzz90...@12 6.comwrote:
                  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.

                  --
                  Nick Keighley

                  Comment

                  • Nick Keighley

                    #10
                    Re: Where is the error?

                    On 13 Nov, 11:45, maverik <maverik.m...@g mail.comwrote:
                    On Nov 13, 2:30 pm, JOYCE <zzzzzz90...@12 6.comwrote:
                    >
                    void mean (const int answer[]);
                    void median(int answer[]);
                    void mode(int freq[],const int answer[]);
                    void bubbleSort (int a[]);
                    void printArray(cons t 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(cons t 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

                    --
                    Nick Keighley

                    Comment

                    • James Kuyper

                      #11
                      Re: Where is the error?

                      Nick Keighley wrote:
                      On 13 Nov, 11:45, maverik <maverik.m...@g mail.comwrote:
                      ....
                      >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(cons t int *);
                      >
                      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.

                      Comment

                      • Nick Keighley

                        #12
                        Re: Where is the error?

                        On 13 Nov, 13:54, James Kuyper <jameskuy...@ve rizon.netwrote:
                        Nick Keighley wrote:
                        On 13 Nov, 11:45, maverik <maverik.m...@g mail.comwrote:
                        ...
                        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(cons t int *);
                        >
                        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 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.

                        Comment

                        • Willem

                          #13
                          Re: Where is the error?

                          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

                          Comment

                          • maverik

                            #14
                            Re: Where is the error?

                            On Nov 13, 3:56 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
                            wrote:
                            On 13 Nov, 11:45, maverik <maverik.m...@g mail.comwrote:
                            >
                            On Nov 13, 2:30 pm, JOYCE <zzzzzz90...@12 6.comwrote:
                            >
                            void mean (const int answer[]);
                            void median(int answer[]);
                            void mode(int freq[],const int answer[]);
                            void bubbleSort (int a[]);
                            void printArray(cons t 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.
                            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(cons t int *);
                            >
                            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(cons t int []);
                            But even better would be remove them completly
                            Yes, good idea. I miss this. Thanks

                            Comment

                            Working...