How do I optimize C code using loop transformation? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
loop fusion, does it mean the above code will become
for(j=1; j<=4,j++){
a[j]=3;
a[j]=a[j]*2*h;
b[j]=6;
b[j]=b[j]+3*k*k;
}

Aside from using two separate loops, the original code contains lots of redundant statements. One possible improvement would be this:
int tmp = 6 * h;
for (j = 1; j <= 4; j++) {
a[j] = tmp;
b[j] = 6 + 3 * j * j;
}
The main optimizations:
One loop instead on two
Removed assignments that get overwritten in the next statement
Calculate a value that does not depend on the loop index outside the loop
You should be aware that most of these optimizations would be done by a compiler anyways.

Related

My while loop is not evaluating conditions or performing tasks inside a nested loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 days ago.
This post was edited and submitted for review 7 days ago.
Improve this question
I am writing a program where several functions are performed within a while loop. When I run the program, nothing inside is done, though. Only the things written before the while loop were performed. Here is the code:
int main() {
int x;
int y;
int a = 1;
for (x = 0; x < 9; x++) {
for (y = 0; y < 9; y++)
board[x][y] = '\0';
}
displayBoard();
while (cWhoWon == '\0') {
printf("\n%c\n", cWhoWon);
if (iCurrentPlayer == 1 || iCurrentPlayer == 0) {
printf("\nPLAYER X\n");
and so on with nested loops until this if loop ends. (it's indented properly in the real code, i appreciate your patience) No malloc was used, nor struct - I am only a beginner with knowledge of arrays, loops, booleans, and basic functions like isdigit.
I tried to add another condition, a = 1, to the while loop with the 'and' boolean operator (&&) and ';' but the same thing happens. I thought that this would make the while loop iterate, at least, but it doesn't. Is it my compiler? I'm using "OnlineGDB.com" C compiler.

The logic of nested loops in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am new to programming and I'm following the CS50 course. I'm trying to fully understand the logic behind nested loops in C. I think I've got it, but I'd like to be sure before I move on to the next set of problems. Here's the code (provided by the course). It creates a cube made of hashes. My explanations are below the code.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n, j++)
{
printf("#");
}
printf("\n");
}
The first loop starts: It creates a new variable called i and sets it to 0. The command checks the new variable: If it is lesser than n (true), run it, starting the inside loop.
The inside loop also creates a new variable, j, sets it to 0, checks it and, if it is true (j < n), runs the code below and print a hash. Afterwards, the inside loop is incremented and this process occurs again until the inside loop condition is not met anymore. This will create a ROW of hashes if n is greater than 2.
The outer loop creates a new line, increments and the process starts all over again. It will run until the condition is false (i > n).
The next times the inside loop is accessed, the variable j is set to 0 again, that's why it is possible to print various rows in this program.
Is that correct? Thank you very much in advance!
Yes, your explanation is spot on.
With a minor mistake:
It will run until the condition is false (i > n).
The condition is false when i >= n.
And what I assume it's a typo:
for (int j = 0; j < n, j++);
// ^
// |
remove the ;

i am confused in indexing of arrays [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Following selection sort code is generating problem in a[p]>a[j] line why?Although if i put a[i] instead of a[p] it works fine
for (i=0; i<n; i++){
p=i;
for (j=i+1; j<n; j++) {
if(a[p] > a[j]) {
p=j;
t=a[i];
a[i]=a[p];
a[p]=t;
}
}
}
The line p=j; in the inner loop changes p. This causes p to be different from i later, when if(a[p]>a[j]) is evaluated with a new j.
Your code does not need p at all. The test can be merely if (a[i] > a[j]), and the code inside the loop can be:
t = a[i];
a[i] = a[j];
a[j] = t;
Looking at your code, it seems you think you needed to record i or j in p temporarily and then use p. That is not necessary. To use the values of i or j, simply use i or j.

What is the complexity of the following simple program? [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 6 years ago.
Improve this question
I am having trouble finding understanding complexity. Could someone help me understand what the complexity of the code below is and why.
for (int i = 1; i < n; i++) { // (n is a number chosen by the user)
for (int j = i - 1; j >= 0; j--) {
printf("i=%d, j=%d", i, j);
}
}
An explanation would be great.
Assuming i starts at 0, the complexity would be constant. The complexity is always expressed relative to a variable defining the number of executions, which is not the case here.
If one term should be used to describe this behavior, it is "constant". There will be a number of executions, but this number will never change
Original Question: Because i's initial value is undefined, the behavior of the code is unpredictable. There is no way to usefully answer the question other than that the complexity is undefined. There is no way to know how many operations the code will perform.
Updated Question: It's O(1). The code will always do precisely the same amount of work.
You can compute the time complexity of this code fragment by evaluating the number of operations, namely the number of calls to printf() which for simplicity's sake we shall assume to be equivalent:
Assuming i starts at 1 (you initially forgot to initialize it), the outer loop runs 99 times, for each iteration, the inner loop runs i times. Gauss was supposedly 9 years old when he computed the resulting number of iterations to be 99 * (99 + 1) / 2.
The complexity of the original piece of code was O(1) since it did not depend on any variable, but since instead you updated the code as:
void fun(int n) {
for (int i = 1; i < n; i++) {
for (int j = i - 1; j >= 0; j--) {
printf("i=%d, j=%d", i, j);
}
}
}
The time complexity would come out as O(n2).

Array and loops [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have an array contain 100 elements. Can anyone help me to figure out how to write loops that perform this:
data[0] = 1/(2*3*4)
data[1] = 1/(4*5*6)
data[2] = 1/(6*7*8)
...
data[99] = 1/(200*201*202)
data[0]-data[1]+data[2]-data[3]+data[4]-data[5]+...+data[98]-data[99]
I just can't understand how to start. Any suggestions would be appreciated!
Try this
double c=0;
for (int i=0;i<100;i++)
{
c=i*2+2;
data[i]=1/(c*(c+1)*(c+2));
}
for (int i = 0; i < 100; i+=2)
{
op+= data[i] - data[i+1];
}
My suggestions how to start, if you really want them and want to manage this by your own:
Generalize your algorithm:
Find a function f(x) such that data[i] = f(i)
Just write the algorithm in your native language.
Then learn basic operators of C language, including loop construct.
Write your "native language algorithm" in C language.
Just in one loop:
int total = 0;
for(size_t i=0; i<100; ++i){
int temp = (i+1)*2;
data[i] = 1/(temp*(temp+1)*(temp+2));
total = total + (i%2==0?data[i]:-data[i]);
}

Resources