Like Tree6Likes

Who can improve this excercise?

This is a discussion on Who can improve this excercise? within the C Programming forums, part of the General Programming Boards category; Basically I have done this program just to try calling some function. Who can give me some idea, or give ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    23

    Who can improve this excercise?

    Basically I have done this program just to try calling some function.
    Who can give me some idea, or give me a excercise? (Like do this..., I will try to do it)

    Code:
    void numf(int, int);
    int takenum ();
    void banner(void);
    
    
    #include <stdlib.h>
    #include <conio.h>
    #include <stdio.h>
    
    
    int p=1;
    void main()
    {
    int number1, number2;
    
    
    banner();
    
    
    number1=takenum();
    number2=takenum();
    
    
    numf(number1, number2);
    
    
    }
    void banner()
    {
    
    
        printf("This program add two number.\n\n\n");
    
    
    }
    
    
    int takenum()
    {
        
        int in1;
        
        printf("Enter number %d:",p);
        scanf("%d",&in1);
    
    
        p=p+1;
        return(in1);
    }
    
    
    
    
    void numf(int num1, int num2)
    {
        int sum;
        sum=num1+num2;
    
    
        printf("The sum of this two number are %d",sum);
        getch();
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    Remove and use of the non standard header "conio.h".

    Improve the indentation.

    Get rid of the use of Global variable.

    Put the include files before the function prototypes; it is a more common ordering.

    Tim S.
    Last edited by stahta01; 01-07-2012 at 10:26 AM.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    It can instantly be improved by using correct grammar. :P

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    "p" is a pretty lame variable name, especially for a global variable. I would make it local to main(), give it a proper name, and pass it to your takenum() function.

    Also, you can avoid having to do p=p+1 by doing this:
    Code:
    printf("Enter number %d:",p++);
    stahta01 likes this.
    If you understand what you're doing, you're not learning anything.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The return type of main() is int.

    Figure out how to make your IDE leave the console window open so you don't have to use (the non-standard) getch(). I think one way to do that with Visual Studio is to launch with CTRL-F5, but there may be a better way under "Preferences" or something.
    Last edited by MK27; 01-07-2012 at 10:34 AM.
    stahta01 likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you want a "next level" exercise, how about adding up all the numbers between the numbers entered. So if 5 and 9 are entered, you'd calculate 5+6+7+8+9 (using a loop) and print the result.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    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

    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.

    mcpaddington
    It can instantly be improved by using correct grammar. :P
    For english gcse I got C J. You can improve it, if you want or can.


    itsme86
    I tried but the first and second print say’s
    “Enter number 0”
    “Enter number0”
    Code:
    void numf(int, int);
    int takenum ();
    void banner(void);
    void error_message(int);
     
    #include <stdlib.h>
    #include <conio.h>
    #include <stdio.h>
     
     
    void main()
    {
    int number1, number2;
     
    banner();
     
    number1=takenum();
    number2=takenum();
     
    numf(number1, number2);
     
    }
    void banner()
    {
     
         
     
    printf("            **************************************************************\n");
    printf("            *                                                            *\n");
    printf("            *               This program add two number                  *\n");
    printf("            *                                                            *\n");
    printf("            **************************************************************\n");
     
     
     
    }
     
    int takenum()
    {
          int in1,error,p=0;
          do{
         
         
          printf("Enter number %d:",p++);
          scanf("%d",&in1);
          error=in1;
          error_message(error);
          }while(error==0);
     
     
          return(in1);
    }
     
     
    void numf(int num1, int num2)
    {
          int sum;
          sum=num1+num2;
     
          printf("The sum of this two number is %d",sum);
          getch();
    }
     
    void error_message(int error)
    {
          if(error==0)
          {
                printf("\n*** Illegal character***\n");
                printf("\nPress any key to continue\n");
                getch();
                fflush (stdin);
          }
    }
    MK27
    The my uni teacher told me tu use getch() so I use.

    Latest and correctly working:
    Code:
    void numf(int, int);
    int takenum ();
    void banner(void);
    void error_message(int);
     
    #include <stdlib.h>
    #include <conio.h>
    #include <stdio.h>
     
    int p=1;
    void main()
    {
    int number1, number2;
     
    banner();
     
    number1=takenum();
    number2=takenum();
     
    numf(number1, number2);
     
    }
     
     
    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 numf(int num1, int num2)
    {
          int sum;
          sum=num1+num2;
     
          printf("The sum of this two number is %d",sum);
          getch();
    }
     
    void error_message(int error)
    {
          if(error==0)
          {
                printf("\n*** Illegal character***\n");
                printf("\nPress any key to continue\n");
                getch();
                fflush (stdin);
          }
    }

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Still awful indentation, I see.

    Don't use fflush(stdin) -> FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    Don't use void main, it's outdated and bad practice.

    There's a much better way for takenum() that doesn't use another random "error" function, can you find it?

    >> Put the include files before the function prototypes; it is a more common ordering.
    This is actually really important, not only is it non-standard, sometimes your #includes will define structures that are used in the function prototypes, and because C reads from the top down, it will report them as errors.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    Quote Originally Posted by tahmod View Post
    stahta01
    The my uni teacher told me tu use getch() so I use.
    FYI: Your teacher is a idiot and is not teaching you to program correctly!!

    Tim S.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    oogabooga What do you think?
    Code:
    void numf(int, int);
    int takenum ();
    void banner(void);
    void error_message(int);
    
    
    #include <stdlib.h>
    #include <conio.h>
    #include <stdio.h>
    
    
    int p=1;
    void 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",sum);
    
    
    getch();
    
    
    }
    
    
    
    
    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 numf(int num1, int num2)
    {
        int sum;
        sum=num1+num2;
    
    
        printf("The sum of this two number is %d",sum);
        getch();
    }
    
    
    void error_message(int error)
    {
        if(error==0)
        {
            printf("\n*** Illegal character***\n");
            printf("\nPress any key to continue\n");
            getch();
            fflush (stdin);
        }
    }

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    We see your code, you don't need to keep pasting it. And I think your indentation is actually getting worse.

    Also, did you even read our suggestions and try to improve it? Asking oogabooga seems like you're trying to avoid negative criticism by blatantly ignoring everyone that tried to help you.
    stahta01 likes this.

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    MK27
    Thanks for CTRL-F5, My teacher is z*** brain I think, I noticed a bit from the start.

    memcpy
    Still awful indentation, I see.

    Don't use fflush(stdin) -> FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    I will read this.
    Don't use void main, it's outdated and bad practice.
    So what do I use? I don’t know?

    There's a much better way for takenum() that doesn't use another random "error" function, can you find it?
    You want I put the if from the random error function to the takenum() ?

    >> Put the include files before the function prototypes; it is a more common ordering.
    This is actually really important, not only is it non-standard, sometimes your #includes will define structures that are used in the function prototypes, and because C reads from the top down, it will report them as errors.
    Ok, I done it I got 18 error.
    Code:
     
    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 numf(int num1, int num2)
    {
          int sum;
          sum=num1+num2;
     
          printf("The sum of this two number is %d",sum);
          getch();
    }
     
    void error_message(int error)
    {
          if(error==0)
          {
                printf("\n*** Illegal character***\n");
                printf("\nPress any key to continue\n");
                getch();
                fflush (stdin);
          }
    }
     
     
    void numf(int, int);
    int takenum ();
    void banner(void);
    void error_message(int);
     
    #include <stdlib.h>
    #include <conio.h>
    #include <stdio.h>
     
    int p=1;
    void 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",sum);
     
     
    getch();
    }
    stahta01
    FYI: Your teacher is a idiot and is not teaching you to program correctly!!

    What can I do, Im in a S*** uni.
    I got good grades in college but just a small mistake brought me here.
    I wanted to become a pharmacist or medicine lol

    Latest programme working:

    Code:
    
    
    
    
    
    void numf(int, int);
    int takenum ();
    void banner(void);
    void error_message(int);
    
    
    #include <stdlib.h>
    #include <conio.h>
    #include <stdio.h>
    
    
    int p=1;
    void 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 numf(int num1, int num2)
    {
    	int sum;
    	sum=num1+num2;
    
    
    	printf("The sum of this two number is %d",sum);
    	
    }
    
    
    void error_message(int error)
    {
    	if(error==0)
    	{
    		printf("\n*** Illegal character***\n");
    		printf("\nPress any key to continue\n");
    		getch();
    		fflush (stdin);
    	}
    }

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    memcpy
    We see your code, you don't need to keep pasting it.
    I have change it a bit I think.
    And I think your indentation is actually getting worse.

    Also, did you even read our suggestions and try to improve it? Asking oogabooga seems like you're trying to avoid negative criticism by blatantly ignoring everyone that tried to help you.

    I don’t refresh the page every time, so I cant see the latest post. So sorry to answer to your post later.
    Last edited by tahmod; 01-07-2012 at 12:05 PM.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Code:
    void banner()
    {
     
     
         
     
     
    printf("            **************************************************************\n");
    printf("            *                                                            *\n");
    printf("            *               This program add two number                  *\n");
    printf("            *                                                            *\n");
    printf("            **************************************************************\n");
     
     
     
     
     
     
    }
    Are you trying to just make your code seem as long as possible? You keep pasting your code and it's 5 times longer than it needs to be. Get rid of your extraneous blank lines if you're going to keep pasting your code.
    If you understand what you're doing, you're not learning anything.

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    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.

    Second, listen to our ....ing suggestions. Don't use void main, use int main. Don't put #includes after function prototypes. 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. I really don't feel like guiding you through a homework assignment, especially if you're not gonna take any of my advice.

    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).

Page 1 of 2 12 LastLast
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, 11:26 AM
  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, 02:25 AM
  4. Help with Excercise
    By Blanket in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2003, 04:21 PM
  5. Creative Excercise
    By ... in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-29-2003, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21