different way to find a name to iterator [closed] - c

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
After writing so many for loop, I could not find to name iterator. I want write meaningful iterator' name, can you help me ?
ex ;
i
j
ii
jj
iii
jjj
usage :
for i = 0 ; i < ...
for j = 0 j < ...
Finding new iterator name makes my life a bit miserable. How do you find name to your iterator ?

If you have more then 3 nested loops in the same function, this is a code-smell.
You should probably refactor your code [move some staff into other functions, and use these functions].
i,j,k should be enough for most functions.

Try to have meaningful variable names, e.g. int RowNumber, int ColumnNumber.

loop indexes are not limited to 1 or 2 character names. try using a fully describing name, like current_element, inner_iterator, ...
the name should describe what you are indexing, but also tell the purpose of the loop.

I want write meaningful iterator' name
Then the name you use should reflect the meaning of what the iterator is used for.
For example if you are looping over products inside a loop of categories inside a loop of catgegory-groups, you might use names like producti, categoryi, groupi, perhaps together with number-of-item names likes productn/categoryn/groupn and/or array names like products/categories/groups.
I'd only use i for the simplest short non-nested loops that one can understand with a quick glance. Personally I wouldn't even use j; when loops are nested that usually calls for more self-explanatory variable names.

Related

Is there any justification for addressing array like <number>[array]? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Recently I saw an answer on a question where they explained that addressing arrays in this way <number>[array] is valid C code.
How do square brackets work in C?
Example:
char x[] = {'A','B','C','D','E','F','G','H','I','J'};
printf("%d\n",5[X]);
//Will print 70 == 'F'
This kind of notation seems cumbersome and potentially confusing for everybody including the author.
Does this way of addressing arrays come with some justifiable advantage?
or
Can I continue with my life without worrying?
I have never encountered this in "real code" (i.e., outside of intentionally obfuscated things and puzzles with artificial limitations) so it would seem that it is quite universally agreed that this shouldn't be done.
However, I can come up with a contrived example where it might be considered by some (not necessarily me) a nicer syntax: if you have multiple pieces of data related to a single entity in a column, and you represent the rows as different arrays:
enum { ADA, BRIAN, CLAIRE };
const char *name[] = { "Ada", "Brian", "Claire" };
const unsigned age[] = { 30, 77, 41 };
printf("%s is %u years old\n", ADA[name], ADA[age]);
I will be the first to agree that this obfuscates the syntax by making it look like the people are the arrays instead of being the indexes, and I would prefer an array of struct in most cases. I think a case could be made for this being nicer-looking, though, or perhaps in some cases it would be a way to swap the rows and columns (arrays and indexes) with minimal edits elsewhere.
As far as I can tell, there are no technical pros or cons with either method. They are 100% equivalent. As the link you provided says, a[i] = *(p+i) = [addition is commutative] = *(i+p) = i[a].
For subjective pros and cons, well it's confusing. So the form index[array] is useful for code obfuscation, but other than that I cannot see any use of it at all.
One reason (but I'm really digging here) to use the standard way is that a[b+c] is not equivalent to b+c[a]. You would have to write (b+c)[a] instead to make it equivalent. This can be especially important in macros. Macros usually have parenthesis around every single argument in every single usage for this particular reason.
It's basically the same argument as to write if(2==x) instead of if(x==2). If you by accident write = instead of == you will get a compiler error with the first method.
Can I continue with my life without worrying?
Yes.
Yes, the pointer arithmetic is commutative because addition is commutative. References like a[n] are converted to *(a+n) but also n[a] is converted to *(n+a), which is identical. If you want to win ioccc competitions you must use this.

Use grep and map function to sort a hash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Given the structure below:
my %names = qw ( hanibal lecter Harry Potter INDIANA JONES Sarah connor scarlet O’Hara JAMES Bond );
Write a program that builds an array of names based on this list. The resulting array should contain full names (first + last name) in their original capitalization and should be ordered by the last name/first name. When sorting, you must ignore capitalization.
For reasons unknown, we don’t want people named “Jones” or “Connor” in our list so they should be filtered out.
this is my requirement, and i need to solve this problem using grep, map and sort functions.
Both the key and values in your hash are names. So the easiest way to "build an array of names" from your hash is simply:
my #names = %names;
But that doesn't use map or grep. So we can (pointlessly) add them.
my #names = grep { 1 } map { $_ } %names;
I think, perhaps, you should explain what you want in a little more detail.

finding the biggest value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
PLEASE NOTE: I am not looking for code, but for a way how to solve this problem.
My input is world that looks like this:
The problem is, i have to find the biggest number, without using OWN variables I could declare myself, and I'm only allowed to use turnLeft(), turnRight(), move(), isLeft/Right/FrontClear(), getNumber() and putNumber() functions to move < around the world.
Could you please give me a 'verbal solution' or a hint how to do such thing?
While you cannot use any variable, note that you do have available memory (getNumber() and putNumber()). For instance, you could think about leaving a mark in positions you have already been to implement some kind of flood fill.
Further, you can fill the floor with the biggest number you have seen yet. Basically, encoding your own state in the floor.
Important questions:
Is the configuration of the maze always fixed?
Is the range of possible numbers in the floor fixed to a reasonable range (e.g. digits 1-9)?

What are 'Magics' in Database and When to Use Them [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've seen a lot of article mentioning these 'Magics' like 'Magic Tables', 'Magic Columns', 'Magic Number', etc. What are they, really? Please give a concise explanation and example for these 'magics'.
[Edit]
The link below says that rownum is a magic column
http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html
Is it a general term?
I bet that the text is just using a metaphoric expression but there is a very similar term in programming called Magic Strings and Magic numbers. You can see in the wikipedia there are several meanings.
I personally think on the meaning of:
Unique values with unexplained meaning or multiple occurrences which
could (preferably) be replaced with named constants
This means that it is better to write:
for(int = 0; i < MAX_ATTEMPTS; i++){
Than
for(int = 0; i < 20; i++){
See this stackoverflow answer for more information why magic numbers/strings is an anti-pattern

Is Foreach loop a definite or indefinite? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is Foreach loop a definite or indefinite one? Meaning do I know prior to the execution of the loop the number of iterations it is going to do?
No. foreach in most languages (c#, java) doesn't know beforehand how many iteration it will have to do. You could even use foreach to go through an infinite sequence.
That's why for exists
In most languages a foreach loop will iterate over a collection of elements until the collection ends. In most languages I can think of a collection can be a definitive set of elements contained in memory OR a calculated set of elements that could in theory be of an infinite length. This would make the foreach loop "indefinite" in most modern languages.

Resources