In this loop,:
for( i = 0; i < N>>1; i++)
does the N value shift throughout every repetition? i.e. if N=1024, then the first loop does
for( i = 0; i < 512; i++)
and the next loop does
for( i = 0; i < 256; i++)
It does not. If you want to do that, then a construct like the following would do that:
for( i = 0; i < N; N >>= 1, i++)
Or put the shift inside of your loop. The shift operator by itself doesn't change the value of the operand without an assignment statement.
No, you're shifting the value ofN, but you aren't assigning it a new value, so it doesn't change. Every iteration would be equivalent to this:
for( i = 0; i < 512; i++)
Related
I need two variables to increment, but the second variable depends on the first in this way
for (int i = 0, j = i % strlen(user_input); i < strlen(plaintext); i++)
Somehow the j variable does not increase; it stays at 0. Advice will be greatly appreciated.
Point to note is strlen returns size_t and every single iteration calling strlen is not for good performance.
As Ajay said if your intention is to update j as i changes then you have to write that change explicitly. By simply initializing j like that - it won't give you on-the-fly variable which changes as the value of i changes.
Example:
for (int i = 0, j = i % strlen(user_input); i < strlen(plaintext); i++) {
..
j++; // or any change that you want to have in j like j =i+2
// j = i%2
}
Also you can update variable j based on other variables too - it is just a variable, the condition that changes it's value will be decided by you by writing appropriate code.
To answer your comment
size_t len1 = strlen(user_input);
size_t len2 = strlen(plaintext);
for (size_t i = 0, j = i % len1; i < len2; i++) {
..
j++; // or any change that you want to have in j like j =i+2
// j = i%2
// j = i%len1;
}
This ensures that the string length calculation is done once outside the loop. This is done once before the loop starts.
I have an array[768] but now I have only 256 (from 0 to 255) samples in this array. I want to copy each value from 0 to 255 and fill this array better, I mean:
[1][2][3] - > [1][1][1][2][2][2][3][3][3]
How I can do that? Is there a library function that can do this?
I don't recall any known library function capable of doing this.
If you want to do it in-place, I'd do it from right to left (i.e. tail to head), I think this is the only way to do it in-place:
int i, j;
for (i = 255, j = 767; i > 0; i--) {
for (int k = 0; k < 3; k++) {
array[j--] = array[i];
}
}
If you don't need to do it in-place, this would suffice:
for (int i = 0, j = 0; i < 256; i++) {
for (int k = 0; k < 3; k++)
new_array[j++] = array[i];
}
You can do this:
for (int i = 0; i < 768; ++i)
new_array[i] = array[i/3];
The index of the right hand side of the assignment will vary only every three steps.
Array initialization Code:
int m = 100;
int n = 50;
int i = 0, j = 0;
float **a = (float**)malloc(m*sizeof(float*));
for (i = 0; i < m; i++)
{
a[i] = (float*)malloc(n*sizeof(float));
for (j = 0; j < n; j++)
a[i][j] = i + j;
}
a is a 2D array and I want to traverse and update the elements of the 1D array a[0]
Say I want to divide all elements of a[0] by 2:
for (i = 0; i < n; i++)
*a[0]++ /= 2; // instead of a[0][i] /= 2;
This doesn't seem to work..
I guess a is a 2 dimensional array like int a[10][20], then the given statement a[i]++ is "invalid".
The reason is that since a[i] being an array a[i] is a non-modifiable 'lvalue'.
In the above case *a[i] is valid but not the a[i]++
Yes, although it look like a homework. If you want more interesting code, you could write this without spaces, as *a[i]++/=K, and finally you could attach this to the containing loop, as for example while(p=a[i]++)*p/=K; to make things more compressed. ;-)
When iterating through an array how do you compare the current element to the previous one? This is easy except for the caveat the first element has no previous one. Is the best solution
for(i = 0; i < arrLen; i++)
{
arr[i] = process(i, someArg);
if(i > 0)
someFunc(arr[i], arr[i-1]);
}
This is one more comparison that needs to be performed for each element of the array which seems wasteful.
Sorry I forgot to say the array is being populated at the same time. So starting the loop at 1 would mean the first element is left empty.
Just start the loop at 1:
for (int i = 1; i < arrLen; i++)
someFunc(arr[i], arr[i-1]);
Edit: Given your new loop, I think your code is OK. Your optimizer will likely handle it just fine. If you're really concerned, just do the first assignment outside the loop:
arr[0] = process(0, someArg);
for(i = 1; i < arrLen; i++)
{
arr[i] = process(i, someArg);
someFunc(arr[i], arr[i-1]);
}
If you are going to do that, just start the counter at 1.
for (int i = 1 ...)
If you are doing it the other way, with an i+1, end the counter 1 sooner
for (int i = 1; i < arrLen -1 ; ++i)
i quess you have for in actual code
for( int i = 1; i < arrLen; i++)
someFunc(arr[i], arr[i-1]);
or
for( int i = 0; i < arrLen -1; i++)
someFunc(arr[i], arr[i+1]);
You just need to start iteration not from 0 but from 1. It is because 0-1=-1 (illegal array index)
for (int i = 1; i < arrLen; i++)
someFunc(arr[i], arr[i-1]);
And assignment of zeroth element should be done before loop:
arr[0] = process(0, someArg);
for(i = 1; i < arrLen; i++)
{
arr[i] = process(i, someArg);
someFunc(arr[i], arr[i-1]);
}
And also you may need to check if there is need to assign zeroth element so code should be:
if (arrLen)
{
arr[0] = process(0, someArg);
for(i = 1; i < arrLen; i++)
{
arr[i] = process(i, someArg);
someFunc(arr[i], arr[i-1]);
}
}
NOTE: in this way you don't need to use if condition because i always positive.
Start your loop at 1 instead of zero, then you will always have an i-1
for(i = 1; i < arrLen; i++)
someFunc(arr[i], arr[i-1])
EDIT:
Given your new information, I'd say just leave it with the if statement as you have it. I don't think there is a particularly better way. You could initialize a prevVal variable outside of the loop, but I don't think you'd really gain a lot over the if i > 0 statement you have.
I have for loop doubt that I need to ask .
once i saw in coding something like
for(i = 0; i<10; i+)
My doubt is why &when in for loop we use say i+ or i- rather than i++ or i--
Thanks in advance
It won't work, doesn't the compiler return an error if you do? ( or atleast a warning.. )
Just use ++i or i++
Using i+ instead of i++ should not work. As I think you know, i++ increases the value of i by one. When the compiler sees i+, it is expecting something after the +, which causes it to not compile.
The line in question is not valid C
for(i = 0; i<10; i+)
Some valid, equivalent (between themselves) options are
for(i = 0; i < 10; i++)
for(i = 0; i < 10; ++i)
for(i = 0; i < 10; i += 1)
for(i = 0; i < 10; ) { /*...*/ i++; }
You cannot use i+ or i- because if doesn’t work i.e doesn’t increase or decrease value of i for increment you have may use i++,++i,i+=1,i=i+1 and for decrement you can use these by changing sign to negtive