Random Variable names

This is a discussion on Random Variable names within the C Programming forums, part of the General Programming Boards category; I have a standard variable name VarX, where X is a random number wich is in Integer Num. How can ...

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    Random Variable names

    I have a standard variable name VarX, where X is a random number wich is in Integer Num.


    How can i create a varable with the name VarNum, for example if Num = 3, VarNum would be like Var3.


    How can i achieve that?

    Thanks!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't. However there are such things as arrays.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by anon View Post
    Quote:
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

    Quoted more than 1000 times (I hope).
    Phirst!

    It's not VarX that you want, but Var[X]. Learn arrays.
    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"

  4. #4
    Banned
    Join Date
    Mar 2008
    Posts
    78
    I know arrays, i was avoiding to make and array inside other array inside other array, because i already had a 2D array...

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    That's the way it works...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    This reminded me of my first job maintaining a DOS based application. The original programmer used multiple global variables named : flag1, flag2...flag137... There were literally hundreds of them. None of his functions took parameters. All variables were GLOBAL! His function names were like random letters jumbled together. They had no relation to the function, ie. it wasn't an acronym. Amazingly the program ran and was used in a professional company that was actually fairly successful.

  7. #7
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Ok, why is this 3 of 3 arrays wrong:

    Code:
         matrix[3][3][3];
         matrix[0] = {{{{'1','2','3'},{'1','2','3'},{'1','2','3'}},{{'1','2','3'},{'1','2','3'},{'1','2','3'}},{{'1','2','3'},{'1','2','3'},{'1','2','3'}}},{{'1','2','3'},{'1','2','3'},{'1','2','3'}},{{'1','2','3'},{'1','2','3'},{'1','2','3'}},{{'1','2','3'},{'1','2','3'},{'1','2','3'}}},{{'1','2','3'},{'1','2','3'},{'1','2','3'}},{{'1','2','3'},{'1','2','3'},{'1','2','3'}},{{'1','2','3'},{'1','2','3'},{'1','2','3'}}};
    I'm doomed..

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    An array is not a modifiable lvalue, so it cannot be assigned. It can be initialized:
    Code:
       char matrix[3][3][3] = 
       {
          {{'1','2','3'},{'1','2','3'},{'1','2','3'}},
          {{'1','2','3'},{'1','2','3'},{'1','2','3'}},
          {{'1','2','3'},{'1','2','3'},{'1','2','3'}},
       };
    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.*

  9. #9
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Split the initialization across several lines. Having everything on one line is absolutely ridiculous and confusing. If I had to guess just from looking at your massive line of code, I would suspect that you are missing curly braces throughout.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Code:
    char matrix[3][3][3] = 
    {
    	{
    		{ '1', '2', '3' },
    		{ '1', '2', '3' },
    		{ '1', '2', '3' }
    	},
    	{
    		{ '1', '2', '3' },
    		{ '1', '2', '3' },
    		{ '1', '2', '3' }
    	},
    	{
    		{ '1', '2', '3' },
    		{ '1', '2', '3' },
    		{ '1', '2', '3' }
    	}
    };
    Couldn't resist.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Couldn't resist.
    Code:
    char matrix[3][3][3] = 
    {
    	{
    		{
    			'1',
    			'2',
    			'3'
    		},
    		{
    			'1',
    			'2',
    			'3'
    		},
    		{
    			'1',
    			'2',
    			'3'
    		},
    	},
    	{
    		{
    			'1',
    			'2',
    			'3'
    		},
    		{
    			'1',
    			'2',
    			'3'
    		},
    		{
    			'1',
    			'2',
    			'3'
    		},
    	},
    	{
    		{
    			'1',
    			'2',
    			'3'
    		},
    		{
    			'1',
    			'2',
    			'3'
    		},
    		{
    			'1',
    			'2',
    			'3'
    		},
    	},
    };
    Seriously, there is a fine line between readable and spaced-out whitespace. Use whatever whitespace you think makes it easier to read. But there's no need to go too far.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I think that is a little too far.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Dave wins spacing battle.
    What is C++?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Dave is making it difficult to separate the different dimensions. That's why I went a step further.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I thought Elysia was splitting hairs. That's why I went a step further. (Of course, that's probably how I would indent it, anyway . . . .)

    I think that is a little too far.
    That was the point.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable names in Waterloo BASIC
    By Thantos in forum Tech Board
    Replies: 4
    Last Post: 12-27-2007, 03:57 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. underscore and variable names
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 09:57 AM
  4. random number between 0 and variable value
    By HybridM in forum C++ Programming
    Replies: 23
    Last Post: 02-06-2003, 09:14 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

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