Thread: slight code peoblem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19

    slight code peoblem

    i am attempting to enter a max and min value for a factorial table but the application will only let me enter the first number and i cant see where i have gone wrong. any help would be apreciated

    Code:
    #include <stdio.h>
    
    void main()
    {
    	int maxfactorial=0, minfactorial=0;
    
    	printf("enter max factorial\n");
    	scanf("%d" maxfactorial);
    	getchar();
    	printf("enter min factorial\n");
    	scanf("%d" minfactorial);	
    	getchar();
    
    }

  2. #2
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    I am just a beginner and might not have the "right" answer to your question but a few other points to mention.

    1. The following line(s) need to be changed.
    scanf("%d" maxfactorial);
    To.
    scanf("%d", &maxfactorial);
    scanf("%d" minfactorial);
    To.
    scanf("%d", &minfactorial);
    2. Try to include "return 0;" at the end of every function (it is advised in most books on C).

    3. You are assigning a value to your variables in the beginning while you later are asking for user input. I do not see the point in this.

    4. Not sure on this one but if I am right you should change.
    void main()
    To.
    int main(void)
    Hope this helped you it is all the advise I can give but I am sure there are more people on here willing to help.
    Last edited by JOZZY& Wakko; 11-20-2009 at 08:18 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by JOZZY& Wakko View Post
    2. Try to include "return 0;" at the end of every function (it is advised in most books on C).
    Not necessarily every function, but you should on every function that is expected to return an integer--not specifically zero, but something meaningful to you. We use return 0; at the end of main because the OS expects to receive a return value (typically 0 for success, or something else like EXIT_FAILURE) when your program exits.

    Functions generally only need to return something if you need them to. If you don't, you generally make them void functions:
    Code:
    void printx( int x )
    {
        printf( "x is %d\n", x );
    }
    Here our function doesn't really need to return anything, because when we wrote it, we decided we didn't care if it did anything more than just print x. But yes, if your function is expected to return something, then you need to make sure it does so:
    Code:
    int thisiswrong( int x )
    {
        if( x == 5 )
            printf( "hello world!\n" );
    
        /* not returning something here should generate compiler warnings/errors */
    }
    Here we're expected to return an int, not doing so is an error.
    Quote Originally Posted by JOZZY& Wakko View Post
    3. You are assigning a value to your variables in the beginning while you later are asking for user input. I do not see the point in this.
    It's generally advised that you initialize your variables to something before you use them. This helps you avoid pulling your hair out because for some reason you're getting an odd number where you least expect it, scouring your code to figure out why...
    Code:
    int x;
    ... way later ...
    printf( "x * 5 is: %d\n", x * 5 );
    Generally if you compile with warnings on you'll get something like 'possible use of uninitialized variable, first used at <line number>', or something like that.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19
    thanks for the help. i have uploaded the assignment i have been given. i dont want anyone to do it for me. but any suggestion of where to go next would be a great help as i am completely lost. thanks again,
    Ciaran.

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    you're going to need a for loop to go from your min to max, a function to calculate the factorial (you can do this recursively or with a for loop), a for loop within the min to max for loop to print out the expressions like 5*4*3*2*1

    1. print your header
    2. loop from max to min (or min to max)
    3. foreach line: print n!, a couple tabs, for loop to do 5*4*3*2*1 etc, couple tabs, calculated factorial
    4.end

  6. #6
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19
    thank you. that is very helpful.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19
    Code:
    #include <stdio.h>
    
    void main()
    {
    	int maxfactorial=0, minfactorial=0;
    
    	printf("enter max factorial\n");
    	scanf("%d", &maxfactorial);
    	printf("enter min factorial\n");
    	scanf("%d", &minfactorial);	
    
    	if (maxfactorial>10){
    	printf("enter a max factorial of ten or less");
    	}
    
    }
    if i want that to restart the application if the if statement prints that line, how would i set that up?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You would start by learning about loops: for, while, and do-while.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by quzah View Post
    Not necessarily every function, but you should on every function that is expected to return an integer--not specifically zero, but something meaningful to you. We use return 0; at the end of main because the OS expects to receive a return value (typically 0 for success, or something else like EXIT_FAILURE) when your program exits.

    Functions generally only need to return something if you need them to. If you don't, you generally make them void functions:
    Code:
    void printx( int x )
    {
        printf( "x is %d\n", x );
    }
    Here our function doesn't really need to return anything, because when we wrote it, we decided we didn't care if it did anything more than just print x. But yes, if your function is expected to return something, then you need to make sure it does so:
    Code:
    int thisiswrong( int x )
    {
        if( x == 5 )
            printf( "hello world!\n" );
    
        /* not returning something here should generate compiler warnings/errors */
    }
    Here we're expected to return an int, not doing so is an error.
    Thanks for the explanation. But does that mean that in this case I was right because the program returns 2 values?
    Quote Originally Posted by quzah View Post
    It's generally advised that you initialize your variables to something before you use them. This helps you avoid pulling your hair out because for some reason you're getting an odd number where you least expect it, scouring your code to figure out why...
    Code:
    int x;
    ... way later ...
    printf( "x * 5 is: %d\n", x * 5 );
    Generally if you compile with warnings on you'll get something like 'possible use of uninitialized variable, first used at <line number>', or something like that.
    Ok, but would this be an issue when you are expecting input from an user to set a value for the integer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM