Answer the question
In order to leave comments, you need to log in
What would this pseudocode look like in C code?
I am studying the subject "Informatics and programming", with arrays it's still a bit tight, the topic is "Sorting arrays". Tell me what the code will look like, based on the pseudocode. I want to compare it with my written code and analyze what I did wrong. I have no errors in the output, when you enter any n, the same 7-digit number always comes out.
Answer the question
In order to leave comments, you need to log in
I didn’t quite understand what X cut is: return value or output. Made with a conclusion.
void Sort()
{
int n;
printf("Input n: ");
scanf("%d", &n);
int* x = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; ++i)
{
printf("\nInput x[%d] = ", i);
scanf("%d", (x + i));
}
for (int i = 0; i < n - 1; i++)
{
char flag = 0;
for (int j = 0; j < n - i - 1; j++)
{
if (x[j] > x[j + 1])
{
int p = x[j];
x[j] = x[j + 1];
x[j + 1] = p;
flag = 1;
}
}
if (flag == 0)
break;
}
for (int i = 0; i < n; i++)
{
printf("\nElement x[%d]=%d", i, x[i]);
}
printf("\n");
free(x);
}
I see several problems. First, add curly braces to all loops. And set the indents carefully (opening bracket - + indent, closing - - indent). You now have instructions that you think are in the loop - they are outside of it.
Secondly, what is this all about?return flag=1;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question