nested for loop value of not initialized

user7854936

I know question sounds dumb, I can't really figure out what is wrong in this code?

void sort(int *arr, int size)
{
    int min = 0;
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = i; i < size; j++)
        {
            if (arr[min] > arr[j])
            {
                min = j;
            }
        }
        if (min != i)
        {
            Swap(&arr[i], &arr[min]);
        }
    }    
}

The following code should sort the arr but it is giving segmentation fault. I ran this code via debugger and it says the value of j at line

        for (int j = i; i < size; j++)

something like 3234 (not initialized) and program ends. But j should be 0.

debuger screenshort

Mankalas

In your second for loop, it should be j < size, not i < size.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is the value of 'i' in the loop not conflicting with the one initialized in the for loop

calculated value not appended in for loop, instead initialized value is appended

Nested loop with value comparison in Ansible

Retain value from nested for loop

Why j is initialized to 0 instead of its incremented value in the loop?

Count within nested loop and use value before second nested loop

Python Nested Loop Enter Value and Confirm Answer

Save the minimum value of a nested for loop to Pandas Dataframe

Sort Nested Json Object based on value in for loop?

resetting a variable back to starting value in a nested loop

javascript for loop, nested if incremental value not matching?

Loop structs and check nested to find matching value

OCaml return value in if statement nested in loop

xslt select matching value from nested loop

Nested IF inside FOR loop works only for first value

Getting value from parent in nested loop

For loop (nested) for value_counts() with Pandas

Pandas nested loop with a row matching a specific value

Idiomatic way to run nested loop with passing value

Nested loop is not returning value in Angular TypeScript

Nested for loop error (truth value ambiguous)

For loop initialized but not entered

Bash pass variable value from for loop to nested for loop

Nested Viewpager Fragment not getting initialized

When is a default value initialized?

Generic value initialized allocator

Ignoring the initialized value in a variable

Why can't we assign the value to a non-initialized string within a for loop in c++?

Improve nested loop efficiency and enter the xor value of the index row times the index of the column into an array using the nested loop?

TOP Ranking

HotTag

Archive