Help me find bug, the loop is running infinite times

#include <stdio.h>

int main()
{
    int d[10],temp,i,j;
    for(i=1;i<11;i++)
    {
        scanf("%d",&d[i]);
    }
for(i=1;i<11;i++)
{
    for(j=i+ 1;j<11;j++)
    {
        if(d[i]>d[j])
        {
            temp=d[i];
            d[i]=d[j];
            d[j]=temp;
        }
    }
    printf("\n%d\n",d[i]);
}
int k[10];
for(i=1;i<11;i+2)
{
    for(j=1;j<=6;j++)
    {
        k[i]=d[j];
    }
}
for(i=2;i<=10;i+2)
{
    for(j=10;j>=6;j--)
    {
        k[i]=d[j];
    }
}
for(i=1;i<11;i++)
{
    printf("%d",k[i]);
}
    return 0;
}

Can you please provide more information? What should this code do? What does it do instead? How have you tried to debug it so far?

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

the code should print elements
ip:30 20 90 100 50 10 60 80 70 40
op:10 100 20 90 30 80 40 70 50 60

Ok, and this part?

1 Like

Here is your code with some formatting fixes

#include <stdio.h>

int main() {
    int d[10], temp, i, j;

    for (i = 1; i < 11; i++) {
        scanf("%d",&d[i]);
    }
    for (i = 1; i < 11; i++) {
        for (j = i + 1; j < 11; j++) {
            if (d[i] > d[j]) {
                temp = d[i];
                d[i] = d[j];
                d[j] = temp;
            }
        }
        printf("\n%d\n", d[i]);
    }

    int k[10];
    for (i = 1; i < 11; i + 2) {
       for (j = 1; j <= 6; j++) {
            k[i] = d[j];
       }
    }
    for (i = 2; i <= 10; i + 2) {
        for (j = 10; j >= 6; j--) {
            k[i] = d[j];
        }
    }
    for (i = 1; i < 11; i++) {
        printf("%d", k[i]);
    }
    return 0;
}

A few thoughts

  1. Some handful of comments would be massively helpful

  2. These magic numbers of 10, 11, 6 floating around should really be replaced with something sensible like const int num_elem = 10;

  3. Your incrementing by twos is wrong

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.