Thread: Is there a better way?

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    56

    Is there a better way?

    I have this finished and it works perfect. I was wondering if there is a better way of writeing the code.

    Thank You
    WackoWolf

    Code:
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    
    using namespace System;
    
    int _tmain()
    {
    
    	/*counter variables*/
    	int i;
    	int j;
    	/*variable for decrement value*/
    	int x=5;
    
    
    	/*1st loop for values 0-5*/
    	for (i=0; i<=5; i++)
    	{
    		for (j=0; j<=i; j++)
    		{
    			printf("%d", j);
    		}
    		printf("\n");
    	}
    
    	/*2nd loop for decrementing values 4-0*/
    	for (i=0; i<=5; i++)
    	{
    		for (j=0; j<x;j++)
    		{
    			printf("%d", j);
    		}
    		x=x--;
    		printf("\n");
    	}
    	return 0;
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Code:
    x=x--;
    This is undefined behaviour. What was your intention, to decrement x? If so, x-- will suffice.
    Code:
    #include "stdafx.h"
    #using <mscorlib.dll>
    using namespace System;
    int _tmain()
    Is the above a weird Microsoft way of trying to break C?

    C programs have a main function, not _tmain, and #using is not part of C. Further, namespaces are part of C++, not C.
    Last edited by cwr; 11-03-2005 at 12:32 PM.

  3. #3
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    I am a newbie too, but except the thing cwr wrote, I cant see of something that could become simpler, or in less lines. I think it's OK.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    is this the only mistake that I have made, dam not bad. And yes to your question, but I understand what your saying. Is there anything I could fix to make this simple?

    Thank You

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    I am useing Visual Studio to write C. And this is what comes up when you use .net 2003

    But Thank you all for helpping me with this. I have learn alot for this forum, by going through everything that has been posted.

    Thank You
    WackoWolf

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by WackoWolf
    I am useing Visual Studio to write C. And this is what comes up when you use .net 2003
    Okay, just making sure you're aware that parts of what you gave above is not C.

    Replace everything upto and include int _tmain() with:
    Code:
    #include <stdio.h>
    int main(void)
    And you have a valid C program.

    I can think of ways to make your code smaller, but not simpler.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    I did what you said, and I also rem out the xx--;. but now when I run the program it doesn't go to the fifth number in the line. Run the program and you will see what I mean.

    Thank You
    WackoWolf

  8. #8
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Don't comment it out completely just do a
    x--;
    instead of
    x=x--;

Popular pages Recent additions subscribe to a feed