Thread: Help...please help

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    Help...please help

    I have to get this lab do before midnight. Yet i tried my best to do it....but i cant seem to understand any of it...so far im four weeks into the program and i feel so lost... and i have a test on Monday and i dont know what to do.

    Here is what the lab question is asking.

    1. The program starts by asking the user for an integer between 1 and 9. If an invalid number is entered, print an error message, and ask the user to enter a valid number.
    2. The program then draws a pyramid that looks like this (Assuming an input value of '5'):


    1
    123
    12321
    1234321
    123454321





    and so this is what i did...

    Code:
    main () {
    
    int line; 
    int lc;
    int a;
    
    
    	printf("Enter an integer between 1 and 9: ");
    		scanf("%d" , &a);
    
    		if(a > 0 && a < 10){
    
    			for(lc = 1; lc <= a; lc++){
    
    		for(line = 0; line <= (a - lc); line++){
    			printf(" ");
    			}
    
    			for(line = 1; line >= lc; line++){
    				 printf("%d", line);
    					  }
    
    			for(line = lc - 1; line >= 1; line--){
    				 printf("%d", line);
    					}
    				printf("\n");
    					 }
    
    
    
    
    }
    else{ printf("You have entered an invalid number.\n"); }
    
    
    }
    please im new at this....can someone help me and be very slow and patient with me lol

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    - Thread subject lines like this one are seriously annoying, try and make it something relevant next time.

    >>I have to get this lab do before midnight
    Maybe start earlier next time, eh.

    >>and i feel so lost
    Don't worry, you'll get it eventually.

    >>and so this is what i did
    Jolly good, but what is your question... you haven't actually asked one? Ask specific questions, get specific answers; ask "for help", and who knows what you'll get in return.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    Oh sorry, i was just curious if there is anything wrong with this that im not seeing....

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK:

    For you to answer:
    - Did it compile?
    - Did the compiler generate error or warning messages?
    - Did it run?
    - Did it do what the requirements said it should do?


    - There are no #include's
    - main() is defined incorrectly:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    - You could do better by understanding how to get a number from the user in a more controlled manner:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    At the every least, check the return code from scanf().
    - Sort out the indentation. Use spaces, not tabs, it'll work better on this forum and other editors that don't match yours.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'll give you some pointers to get you started. First of all, the correct form of a C program is:

    Code:
    int main()
    {
        /*  Code goes here  */
        
        return 0;
    }
    Notice that the main function returns an int. You should return 0 or EXIT_SUCCESS to terminate the program normally.

    The next thing you need to look at is your code formatting. Look at the code you posted, and see how ugly it looks. Below I have posted your exact same code. The only thing I have changed, is the main function returning 0, and the formatting.
    Code:
    #include <stdio.h>
    
    int main () 
    {
    	int line; 
    	int lc;
    	int a;
    
    
    	printf("Enter an integer between 1 and 9: ");
    	scanf("%d" , &a);
    
    	if(a > 0 && a < 10)
    	{
    		for(lc = 1; lc <= a; lc++)
    		{
    			for(line = 0; line <= (a - lc); line++)
    			{
    				printf(" ");
                }
    
    			for(line = 1; line >= lc; line++)
    			{
    				printf("%d", line);
    			}
    
    			for(line = lc - 1; line >= 1; line--)
    			{
    				printf("%d", line);
    			}
    			printf("\n");
    		}
    	}
    	else
    	{ 
    		printf("You have entered an invalid number.\n"); 
    	}
    	return 0;
    }
    See how much easier it is to read? Proper formatting allows people to look at your code, and understand it faster.

    My last suggestion to you is to get into the habit of using comments in your code. This not only helps other people that read your code, but it also helps yourself when you come back to code that you wrote a couple months ago.

    Read the links that Hammer posted, and then take another look at your code. If you still have questions, come back here and ask away.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bithub
    I'll give you some pointers to get you started. First of all, the correct form of a C program is:

    Code:
    int main()
    {
        /*  Code goes here  */
        
        return 0;
    }
    Aren't you forgetting something? Like parameters for main? This isn't C++ you know.

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

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    quzah - The continuous pursuit of perfection.

    I think it should be your new sig.

    -Andy
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    its not perfection. its just the way.
    Code:
    int main(void){
    
      return 0;
    }

  9. #9
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Quote Originally Posted by sand_man
    its not perfection. its just the way.
    Code:
    int main(void){
    
      return 0;
    }
    While I agree stylistically, syntactically the two are identical.
    Kampai!

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Sake
    While I agree stylistically, syntactically the two are identical.
    FAQ:
    (C) The difference between int main() and int main(void)

    A common misconception for C programmers, is to assume that a function prototyped as follows takes no arguments:

    int foo();

    In fact, this function is deemed to take an unknown number of arguments. Using the keyword void within the brackets is the correct way to tell the compiler that the function takes NO arguments.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Quote Originally Posted by Dave_Sinkula
    You're confusing a declaration with a definition. In C, a declaration with an empty parameter list means that any number and type of arguments are allowed for compatibility with K&R style functions. On the other hand, a definition with an empty parameter list (as in the case of bithub's main) really does mean "no arguments". If you'd like, I can quote the C standard.
    Kampai!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote it then, because it does not mean "no arguments". It means "any number of arguments". In C++ it means "no arguments". As stated, this is not C++, this is the C board. They are not the same at all.

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

  13. #13
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>Quote it then, because it does not mean "no arguments".
    You're wrong:
    ISO C Standard section 6.7.5.3 Function Declarators

    14
    ... An empty parameter list that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.
    >>As stated, this is not C++, this is the C board.
    I'm well aware of that, which is why I made it clear that I was talking about C.
    Kampai!

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Sake
    While I agree stylistically, syntactically the two are identical.
    Yet the standard is clear that the ways to declare main are as the following:

    Code:
    int main(void)
    or

    Code:
    int main(int argc, char *argv[])
    So why the fuss?

    ~/
    Last edited by kermit; 02-12-2005 at 11:39 PM.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    quzah is correct, main should be either
    Code:
    int main(void)
    or
    Code:
    int main(int argc, char *argv[])
    It was my mistake writing it the way I did in my previous post.

Popular pages Recent additions subscribe to a feed