It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
can anyone supply a complicate example using loop invariant example such as sum(int n) is so trivial that it can not show the power of loop invariant. I want a example that is not that obivious, and we can use method like loop invariant to solve it.
The Wikipedia example is quite good:
for (int i = 0; i < n; i++) {
x = y + z;
a[i] = 6 * i + x * x;
}
Two invariant can be moved (y + z and x * x). The advantage of this example is that after LICM has been applied, you can apply other optimizations on the code to have something very easy.
There are plenty on papers/slides/courses about that, you sure can find a satisfying example.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am trying to make my "Steps" or increments in a while loop such that my values of are like N=2, 4, 8, 16... basically powers of 2 till 2^20. I have tried to do
for(j=1;j<=20;j++){
m=pow(2,2*i);
MAX=pow(2,20);
INC=pow(2,i);
while(m<=MAX){
then have my code running inside this. But in the output it gives me 2,4,6,8,10,12.. does anyone know what the right way is to do this?
Thanks!
You can start with a value of 2 for m and multiply m by two every iteration. As an optimization you can replace the multiplication by two with an left shift:
int MAX = pow(2, 20);
int m = 2;
while(m <= MAX) {
//do your work
m <<= 1;
}
In each iteration of the while loop; multiply your variable by two.
int m = 2;
while(m < maxpow)
{
m = m * 2;
printf("%d\n", m);
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was wondering how you would search through a 2D array (used as a parameter for a function), and find a specific character, e.g. an exclamation mark?
Say I have a 2D array island[20][40] and I want to find the character X. My approach would be to use a nested for loop, to go through each element and an if statement. E.g.
for (i = 0; i < 20; i++) {
for (j = 0; j < 40; j++) {
//Not sure what goes here (I want a function that identifies the element in the array)
if ((some variable) == 88)
printf("The treasure is at: (%d, %d)", i, j);
Thanks for your help :)
-island[20][40] works fine. I just want to know how to search through it for a specific character.
use the condition
if (island[i][j] == 88);
If your array(s) are not ordered then you have no choice but to search through sequentially, there are no short cut.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need this translated into MIPS Assembly (to work on Mars Assembler)
void mm ( double x[][], double y[][], double z[][], int n)
{
int i,j;
for (i=0; i !=n; i++)
for (j=0; j !=n; j++)
z[i][j] = 0.0;
for (k=0; k !=n; k++)
z[i][j] = z[i][j] + x[i][k] * y[k][j];
}
This seems a little bit like a "Do My Homework" question, but i'll give you a hint.
Passing the -S switch to gcc will cause it to emit assembly code. Note, this code may need to be tweaked for the mars assembler.
Good luck!
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
int main()
{
int x = 2, y = 6, z = 6;
x = y == z;
printf("%d", x);
}
== has higher precedence than =, and y==z is 1.
I will end the answer there, because this looks like homework.
http://codepad.org/fp4ZYJX5
The output is:
1
Have a look at this, which explains a similar, yet more complex question and will answer yours as well.
Output is 1
If you want more help we can give you
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Tips on how to do multiplication table in c??
ummm... two (nested) loops?
This should work. Really.
printf("2x1=2");
printf("2x2=4");
printf("2x3=6");
printf("2x4=8");
...
You should read books this is very basic things of programming you must clear this things yourself.
I personally recommend you not just to post here and get answer
Try reading and try developing by yourself before you post it overhere.
http://www.cprogramming.com/tutorial/c/lesson3.html
int main () {
int n = 10;
int i, j;
// Print row labels
for (i=1; i<=n; i++) {
for (j=1; j<=n; j++) {
//printf("\t%d",i*j);
//Do something here to get it working.. :-)
}
printf("\n");
}
}
You have to use 2 nested loops and if you want to make it more organized, use a two-dimensional array.