i am confused in indexing of arrays [closed] - c

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.

Related

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 ;

Segmentation fault (Core dumped) error when trying to populate a 2d matrix [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 6 years ago.
Improve this question
I'm trying to populate my 2D array with random integers.
//rows, cols are randomly generated ints between 1 and 10
int rows = rand()%10 +1;
int cols = rand()%10 +1;
int arr[rows][cols];
for (i = 0; i <rows; i++){
for (j=0; i<cols; j++){
arr[i][j] = rand()%10;
}}
Every time I try to run this code, it gives me a segmentation default.
I edited the post to give the actual code I am running
I suppose it's because the i < ... which should be a j < ... in for (j=0; i<cols; j++);

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

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.

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).

Is this just a nested For Loop? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 8 years ago.
Improve this question
Came across this code the other day:
for(i=0; i<r1; ++i)
for(j=0; j<r1; ++j)
for(k=0; k<r1; ++k){
mult[i][j] += a[i][k] * b[k][j];
}
Now, is this just a different way of writing:
for(i=0; i<r1; ++i){
for(j=0; j<r1; ++j){
for(k=0; k<r1; ++k){
mult[i][j] += a[i][k] * b[k][j];
}
}
}
yes that is correct .. Put brackets around the first implementation to see for yourself.
PS - there is no one right way .. but I would highly recommend using brackets always ( especially if you are new to programming ) .
It goes a long way to help you understand and debug the code
Yes. The syntax for for is
for (init-statement; condition; iteration_expression)
statement
and for is itself an statement. Therefor it can make a recursive compound.
You can even drop last {}:
for(i=0; i<r1; ++i)
for(j=0; j<r1; ++j)
for(k=0; k<r1; ++k)
mult[i][j] += a[i][k] * b[k][j];
Indentation is very important to make the code readable.

Resources