# Thread: code not working... help?

1. ## code not working... help?

First off I must stress that I am an absolute beginner. My friend and I were working on a piece of code that will sort an array using a "MAX" checker (i.e going through the array and looking for the highest value and comparing the rest of the values to it. every iteration the max value is set as the last in the array and the next iteration checks all values except for the last (of the previous iteration).
we aren't done yet - I know this. But we cannot figure out why the very first iteration isn't working. We worked on this for hours!
Please - all I want to know is why it isn't actually working. I ask that you not suggest any advanced options because we are just now learning the basics. If you can find 1-4 lines that are the problem I would be happy for the assistance!
P.S - I know we used both array placings and pointers to describe the same things - it was an attempt to see if using the "arr[i]" format will correct the problem caused possibly by wrong pointer usage. We gave up in the middle...

Code:
```#include <stdio.h>#include <stdlib.h>

void Sort_array(int *_arr, int _n)
{
int max, i, j, index, temp;
for (i = 0; i < _n; i++)
{
printf("%d ", _arr[i-1]);
}
putchar('\n');
for (i = _n - 1 ; i > 0; i--);
{
max = *_arr;
index = 0;
for (j = 1; j < i+1; j++);
{
if (*(_arr + j) > max)
{
max = *(_arr + j);
index = j;
}
}
temp = _arr[i];
_arr[i] = max;
_arr[index] = temp;
printf("temp %d arr[i]=%d arr[index]=%d i=%d \n", temp,*(_arr + i),*(_arr + index), i);
}

putchar('\n');
for (i = 0; i < _n; i++)
{
printf("%d ", _arr[i-1]);
}
putchar('\n');
return;
}

int main(int argc, char **argv)
{
int i, noItems = argc - 1;
int arr;

for (i = 1; i < argc; i++)
{
arr[i-1] = atoi(argv[i]);
}

Sort_array (&arr, noItems);
printf("the sorted array is: \n");
for (i = 1; i < argc; i++)
{
printf("%d ", arr[i-1]);
}

putchar ('\n');
/*printf("Array of %d size is {", noItems);*/
return 0;
}``` 2. If changing from array to poiinter syntax had fixed the problem, that would mean either that your compiler is broken (as the two are equivalent) or that you implemented the change wrong.

Look carefully at line 14. There is a semi-colon there which shouldn't be. The loop therefore sets i to zero, and does nothing else. All of the code between curly braces from line 13 to 30 is NOT part of the loop (your indenting does not make it so).

It doesn't help that your loops printing the arrays are printing invalid elements.

Also, get out of the habit of using variable names beginning with underscores. Such identifiers are reserved by the standard for use by the implementation (there are exceptions to that statement, but beginners are better off avoiding such identifiers completely). That means any code using such identifiers may behave unpredictably. 3. Originally Posted by grumpy Look carefully at line 14. There is a semi-colon there which shouldn't be.
Yup. Downed by a semicolon. Literally the first thing to check. A hard-learned and valuable lesson!
The program now works perfectly. Thanks a million!

Also, get out of the habit of using variable names beginning with underscores. Such identifiers are reserved by the standard for use by the implementation (there are exceptions to that statement, but beginners are better off avoiding such identifiers completely). That means any code using such identifiers may behave unpredictably.
Unfortunately this is demanded by our lecturers. It annoys the hell out of me but they are adamant.
Thanks again.

-- can't figure out how to mark the thread as "Solved". I guess this option is reserved only for mods.. Popular pages Recent additions 