Thread: Who can improve this excercise?

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's like his strategy is to just post the same thing over and over making none of the changes suggested in order to p... someone off enough to make all the fixes for him and just post the fully corrected code. Hint: Don't fall for it.

    How about a sumary of the things that have not been changed yet as of the last post of the code:
    1. main has not been changed to return int
    2. The #includes have not been moved to the top
    3. Still using fflush(stdin); instead of what is in the FAQ
    4. conio.h has not been removed
    5. Still using getch instead of getchar
    6. Not using p++ inside the printf in the do-while loop
    7. Has not removed the ridiculous number of extra blank lines
    8. Indentation has not been improved noticeably

    I suggest you don't post the code again until you think you have fixed all of these things.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    memcpy
    This thread is really starting to be useless, as the OP won't listen to any of our suggestions.

    First off, indent your code properly. No one wants to read that jumbled mess of random whitespace and left-aligned loops. If you can't figure out how to do this, most IDEs and the GCC tool "indent" will do it for you.
    I tried rearrange what do you think?

    Second, listen to our ....ing suggestions. Don't use void main, use int main.
    Done,
    Don't put #includes after function prototypes.
    Where I put it then?
    Prototypes ARE NOT FUNCTION DECLARATIONS, of course you got 18 errors. We keep telling you this, and you're pasting the exact same errors over and over again.
    My teacher said to me they are function declarations, so I don’t know. So I need to search about function prototype.
    I really don't feel like guiding you through a homework assignment, especially if you're not gonna take any of my advice.
    It is not a homework assignment.


    Third, while I didn't say this, stahta01 had a point by saying that <conio.h> is outdated. Don't use it, and if your instructor tries to tell you to use it, explain why it's bad. (if you don't know, it's only usable by people with old Windows machines, and newer ones with backward compatibility).
    I got a new window machine, it work. But I will delect it.

    Code:
    
    
    Code:
    int takenum ();
    void banner(void);
    void error_message(int);
     
    #include <stdlib.h>
    #include <stdio.h>
     
    int p=1;
    int main()
    {
    int number1, number2;
    int a[20],k,sum;
    int i;
    sum=0;
     
    banner();
     
    number1=takenum();
    number2=takenum();
     
    i=0;
    a[i]=number1;
              do{                i++;
                                 a[i]=number1+i;
              }while(a[i]<number2);
     
    printf("The numbers are\n");
              i=0;
              do{printf("%d\t",a[i]);
                       sum=sum+a[i];
                       i++;
              }while (a[i]<number2);
     
    sum=sum+number2;
    printf("\n\nThe sum is %d\n",sum);
    }
     
    void banner()
    {
    printf("            **************************************************************\n");
    printf("            *                                                            *\n");
    printf("            *               This program add two number                  *\n");
    printf("            *                                                            *\n");
    printf("            **************************************************************\n");
    }
    int takenum()
    {         int in1,error;
              do{
              printf("Enter number %d:",p);
              scanf("%d",&in1);
              error=in1;
              error_message(error);
              }while(error==0);
    p=p+1;
              return(in1);
    }
    void error_message(int error)
    {
              if(error==0)
              {printf("\n*** Illegal character***\n");
              }
    }
    Last edited by tahmod; 01-07-2012 at 04:23 PM.

  3. #18
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    iMalc
    It's like his strategy is to just post the same thing over and over making none of the changes suggested in order to p... someone off enough to make all the fixes for him and just post the fully corrected code. Hint: Don't fall for it.

    I don’t do that lol, you think Im a bad person lol, secondly my friend this is a exercise for improving my c. Look im answering everyone question. If you don’t want to help me it is ok.

    How about a sumary of the things that have not been changed yet as of the last post of the code:
    1. main has not been changed to return int Done
    2. The #includes have not been moved to the top Where to put it?
    3. Still using fflush(stdin); instead of what is in the FAQ I will make this change soon, because I need to read the FAQ
    4. conio.h has not been removed I removed
    5. Still using getch instead of getchar Someone told me to use CTRL+F5
    6. Not using p++ inside the printf in the do-while loop I have answered the persone that told me, this I told him that the Number doesn’t change, go read it (post 7)
    7. Has not removed the ridiculous number of extra blank lines I have removed it when I have removed it
    8. Indentation has not been improved noticeably I don’t know what is indentation, I will go look at it

    I suggest you don't post the code again until you think you have fixed all of these things.

  4. #19
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Dude edit your first post with the changes you've made and stop pasting your code into every post !

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tahmod View Post
    stahta01
    "Remove and use of the non standard header "conio.h".
    If i remove coni.h i can't use getch(), (or i did not understand your question).
    Improve the indentation.
    I dont know what is indentation
    You shouldn't be using getch() in the first place... use getchar() from stdio.h instead.
    Indentation is the text sequt of your source code, it's done for readability and easy debugging.

    Put the include files before the function prototypes; it is a more common ordering.
    I like to put it at the end, but if it is the rule to present it more beautifully, I can do it.
    It's not about what YOU want... it's about what the compiler needs.

  6. #21
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    "CommonTater
    [IMG]file:///C:/Users/Tahir/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png[/IMG] Originally Posted by tahmod [IMG]file:///C:/Users/Tahir/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png[/IMG]
    stahta01
    "Remove and use of the non standard header "conio.h".
    If i remove coni.h i can't use getch(), (or i did not understand your question).
    Improve the indentation.
    I dont know what is indentation

    You shouldn't be using getch() in the first place... use getchar() from stdio.h instead."
    I used getchar() the window goes, when it show the answer, If I use Ctrl+F5 as someone said the window will stays.

    "Indentation is the text sequt of your source code, it's done for readability and easy debugging."
    So the last code that I put, it’s indentation is good then?


    "Put the include files before the function prototypes; it is a more common ordering.
    I like to put it at the end, but if it is the rule to present it more beautifully, I can do it.

    It's not about what YOU want... it's about what the compiler needs. "
    I know lol, I just wanted to make a joke

    mcpaddington
    Ok.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    2. The #includes have not been moved to the top Where to put it? Not quite sure what line of thinking has lead to not understanding this, but what we mean is that line 1 is where the first #include should be. The next on line 2 etc. Nothing whatsoever comes before them.
    5. Still using getch instead of getchar Someone told me to use CTRL+F5 Yes if it was just at the end of the program to keep the console window then that works also.
    6. Not using p++ inside the printf in the do-while loop I have answered the persone that told me, this I told him that the Number doesn’t change, go read it (post 7) I see that you made the change and that the changed code was correct and I guarantee you 100% that it would have made the number change, but then you changed it back into something where you put p = p + 1 after the loop which will make it not change. This simply means that The code which you thought didn't work was actually the code that did work. I don't know how you go it wrong, but go and change it back again as suggested and you'll see that it does work.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by tahmod View Post
    "CommonTater
    [IMG]file:///C:/Users/Tahir/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png[/IMG] Originally Posted by tahmod [IMG]file:///C:/Users/Tahir/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png[/IMG]
    stahta01
    "Remove and use of the non standard header "conio.h".
    If i remove coni.h i can't use getch(), (or i did not understand your question).
    Improve the indentation.
    I dont know what is indentation

    You shouldn't be using getch() in the first place... use getchar() from stdio.h instead."
    I used getchar() the window goes, when it show the answer, If I use Ctrl+F5 as someone said the window will stays.

    "Indentation is the text sequt of your source code, it's done for readability and easy debugging."
    So the last code that I put, it’s indentation is good then?


    "Put the include files before the function prototypes; it is a more common ordering.
    I like to put it at the end, but if it is the rule to present it more beautifully, I can do it.

    It's not about what YOU want... it's about what the compiler needs. "
    I know lol, I just wanted to make a joke

    mcpaddington
    Ok.
    Seriously, use the [QUOTE] tags, reading that awful BBCode mess is irritating.

    But as for your code, no, your indentation was still terrible. Usually I'm lenient about indentation errors, but if the entire program is shoved to one side of the page except for a couple random statements, you need to fix it. Look at any other program's source, and look at yours and tell me why yours is practically unreadable while theirs is easier to understand.

    Don't try to "make jokes" and do funny / cool tricks in a language until you've mastered the language. Most C standards exist for a reason, this one being especially important (failing to do it can result in errors). You can't go running around breaking standards for your own perceived aesthetic gain, just like I can't go around spraying graffiti for my own.


    Prototypes ARE NOT FUNCTION DECLARATIONS, of course you got 18 errors. We keep telling you this, and you're pasting the exact same errors over and over again.
    My teacher said to me they are function declarations, so I don’t know. So I need to search about function prototype.
    I really don't feel like guiding you through a homework assignment, especially if you're not gonna take any of my advice.
    It is not a homework assignment.
    Either it IS a homework assignment and your teacher misled you, OR you're just wrong about C standards. It can't be both.

  9. #24
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I learned C using "function declaration" and "function prototype" as meaning the exact same thing.
    Is it now different?

    Found a link http://opencbp.sourceforge.net/en_US...rototypes.html

    In the K&R C days it was "function declaration" with ANSI C or a little later the "function prototype" was created. I read it as a "function prototype" is a sub category of "function declaration" that contains more information. And, the term "function signature" is closely related to "function prototype". It might even be the same in the new C or C++ standard.

    Tim S.
    Last edited by stahta01; 01-07-2012 at 10:42 PM.

  10. #25

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. excercise from K&R2 book
    By psx-c in forum C Programming
    Replies: 4
    Last Post: 12-29-2009, 12:26 PM
  2. Excercise help again
    By gin in forum C++ Programming
    Replies: 17
    Last Post: 07-05-2008, 11:00 AM
  3. excercise
    By luigi40 in forum C# Programming
    Replies: 9
    Last Post: 11-22-2005, 03:25 AM
  4. Help with Excercise
    By Blanket in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2003, 05:21 PM
  5. Creative Excercise
    By ... in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-29-2003, 10:18 AM