Ok, the purpose of my program c homework is to insert a certain set of numbers,
ID: 3532285 • Letter: O
Question
Ok, the purpose of my program c homework is to insert a certain set of numbers, and sort it from least to greatest. I did write my function, and in my personal opinion, theoretically everything should work fine. However, after inserting the values, wierd numbers are printing at the end. When i inserted the order 1) 5 2) 1 4 6 3 2, then something like -1203481 -1204124 -230235 -123048124 1 2 3 4 6 prints.
i guess somewhat it is right, but i think there is an error in my insertion (main) function. OTHER STUDENTS, PLEASE DON'T COPY MY WORK, SERIOUSLY. please TELL ME how i could fix this error, or some suggestions! I'm currently stuck! and tired....
#include<stdio.h>
int sorting(int x[], int size);
int swap(int *a, int *b);
int printing(int x[], int size);
int main(){
int i = 0;
int j = 0;
int s = 0;
int data[20];
int len = sizeof(data)/sizeof(data[0]);
printf("Enter the total number of elements of the input: ");
scanf("%d", &s);
printf("Enter the %d elements in any order: ", s);
for(i = 0; i < s; i++){
scanf("%d", &data[i]);
}
sorting(data, len);
printing(data, len);
return(0);
}
int sorting(int x[], int size){
int i = 0;
int j = 0;
int min = 0;
for(i = 0; i < size; i++){
min = i;
for(j = i; j < size; j++){
if (x[j] < x[min]){
min = j;
}
swap(&x[i], &x[min]);
}
}
}
int swap(int *a, int*b){
int temp = *a;
*a = *b;
*b = temp;
}
int printing(int x[], int size){
int i = 0;
for(i = 0; i < size; i++){
printf("%d ", x[i]);
}
printf(" ");
}
Explanation / Answer
sorting(data, len);
printing(data, len);
in above statemetns instead of len u have to pass s i.e no of elemetns..
that is the reason y u r getting junk numbers in your answers..
change above statements to
sorting(data, s);
printing(data, s);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.