I'm helping by adding comments, so please read through your code below. I don't think their is a way to exit the program with CTRL + D, but anyone can close a program with CTRL + C
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 |
#include <stdio.h>
void selection_sort(int a[], int size);
int main(void) {
int a[100], i, j, N;
printf("How many numbers will you be entering: ");
scanf("%d", &N);
printf("Enter numbers to be sorted: ");
//Its nice to space out your for loops with an empty line
for(i = 0; i < N; i++) {
//Because Arrays are pointers, you don't need the & also add an /n
//scanf("%d /n", a[i[);
scanf("%d", &a[i]);
}
selection_sort(a, N);
printf("Your sorted list of integers is : ");
for(j = 0; j < N; j++) {
printf("%4d ", a[j]);
}
printf("\n");
return 0;
}
//While this is correct you should have a function as follows
//void selection_sort(int *a, int size){
void selection_sort(int a[], int size) {
int i, j, x;
//remove the smaller than or equal to and you can just have
//for(i = 1; i < size; i++){
for(i = 1; i <= size - 1; i++) {
j = i;
x = a[i];
//remember removing the smaller than or equal to
//while(a[j] > x && j > 0){
while(a[j - 1] > x && j > 0) {
a[j] = a[j-1];
j = j - 1;
}
a[j] = x;
}
} |
|
|
I might be missing some errors, I'm new to trying to help people with out giving them the full answer =/. Memory allocation and everything else is pretty much terminated when the process is closed, but its not nice to leave it like that... but the code you have, shouldn't raise any problems.