ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have three loops. One loop has values saved at indexes 65-90. The second loop has values stored at indexes 97-122. I tried using nested for loops to add the values at the indexes and save them in a third array with a range of 26, but when I print the third array, the value are all zeroes. For example array1 [65] = 2 + array2 [97] = 1 = array3 [0] = 3
Here is my code:
Code:
for (q=65; q<91; ++q){
for (r=97; r<123; ++r){
for (s=0; s<26; ++s){
array3[s] = array1[q] + array2[r];
}
}
}
for (m=0; m<26; ++m){
printf("%d total %d\n",m,array3[m]);
}
for (q=65; q<91; ++q){
for (r=97; r<123; ++r){
for (s=0; s<26; ++s){
array3[s] = array1[q] + array2[r];
}
}
}
Also I would say that code does not do what you intended to do.
How many times is array3 written to? hint it is not 26 and each previous value is overwritten.
I thought it was being written to 26 times, but apparently not. In my stupid reasoning I thought the code would have the s start at index 0, q at 65 and r at 97 then just add the values until indexes s=25, r=122 and q=90.
If I've understood properly the intent was probably to do:
Code:
for (s=0 ; s<26 ; ++s)
{
array3[s] = array1[ s + 65 ] + array2[ s + 97 ] ;
}
... though I'm a little curious about why it's using non overlapping index ranges. It's almost as if the code is intended to manipulate a single array rather than 3 different arrays.
To recap what you did wrong, when you nest for loops, the innermost loop is executed in its entirety, then the next outer loop is iterated once, then the innermost loop is executed in its entirety again.
This logic flows out until all loops have been executed in their entirety, with the outermost loop being executed entirely once, the next loop in being executed entirely as many times as necessary for the outer loop to execute once, the next inner loop executed entirely as many times as the product of the number of executions of the two outer loops and so forth.
What GazL wrote might be considered even better, but I thought you should see the right syntax for the code you apparently were trying to write.
I'm not sure what I suggested is any better. I'm just used to thinking in languages that don't have fancy stuff like multi-index loops, so I tend to forget to use features like that.
Which is more efficient, 3 increment operations, or one increment and 2 integer additions? Yours certainly looks more elegant and I suspect it might be more efficient too.
Which is more efficient, 3 increment operations, or one increment and 2 integer additions? Yours certainly looks more elegant and I suspect it might be more efficient too.
I tend to rely on the optimizer to sort out simple stuff like that, such that there would be no difference in efficiency after optimization.
Sometimes there are surprises: Sometimes the optimizer fails to see something incredibly obvious, so the performance would be different. Sometimes the execution time of very bad looking asm code turns out to be quite good, so even looking at the generated asm code might not tell you which is better.
If you really care about performance (which I assume is not the case in this example), you need to use a profiler and find out the real performance.
If the optimizer doesn't sort it so well that the results are identical, I think it would handle your version better. Those two ADDs are quite a bit cheaper at run time than you'd expect, very likely cheaper than the two extra increments. If the ADDs (for complex) reasons turn out not to be super cheap, the optimizer can easily pull them out of loop in x86 32bit. In x86-64, either having the ADDs cheap in the loop or having the optimizer pull the ADDs out of the loop could happen if you had used 65L and 97L, but not with int constants.
I focus much more than most programmers on writing C++ code in ways that will generate fast running asm code, but in an example like this even I wouldn't worry about that.
The decision between these two versions of that code should be made on which best conveys the underlying meaning (to avoid bugs in case some revision is required) not on which compiles to better asm code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.