Link to home
Create AccountLog in
Avatar of flubbster
flubbsterFlag for United States of America

asked on

C programming help for beginner sort routine

Greetings all. I had been trying to learn C Programming but have not gotten very far. Anyway, my son is trying to get a program to work but is having trouble. I received excellent help last time and I am hoping I can get more.

The included code is supposed to have the following parameters:

The user enters 5 numbers.
The code checks for a duplicate entry.
The code sorts the numbers in ascending order.

I know enough to know that the code could be much more compact with the use of better arrays and loops, however, the coding shown is what my son was instructed to use at this level of learning. According to my son, the Duplicates check works, but the sort does not. If the following is entered:

5,4,3,2,1

The output is:

2,1,3,4,5

I am at a loss and I am unable to run the code to try to help as I do not have a compiler and the ones I try on-line all report errors, even though my son does not get any. Not sure what he is using. Anyway, I am hoping someone can spot the mistake and help out. Once it is working, maybe someone can show how it could be done more efficiently.
#include <stdio.h>

int TheDupe(double mark[]);
void order(double mark[]);

int main(void){

	double a, b, c, d, e, mark[5];
	
	int x;

	printf("Please enter 5 numbers. They can be positive or negative. No duplicates please.\n");
	printf("Value 1=> ");
	scanf_s("%lf", &a);
	printf("Value 2=> ");
	scanf_s("%lf", &b);
	printf("Value 3=> ");
	scanf_s("%lf", &c);
	printf("Value 4=> ");
	scanf_s("%lf", &d);
	printf("Value 5=> ");
	scanf_s("%lf", &e);

	mark[0] = a;
	mark[1] = b;
	mark[2] = c;
	mark[3] = d;
	mark[4] = e;

	order(mark);

	x = TheDupe(mark);

	if (x = 1)
		printf("%d Duplicates detected! You are a failure at following even the simplest\ndirections.\n", x);
	
	if (x = 0)
		printf("%d The ascending order of the numbers you've entered is %f\n%f\n%f\n%f\n%f\n", x, mark[0], mark[1], mark[2], mark[3], mark[4]);
	
	return(0);
}

void order(double mark[]){

	double temp;
	int x;
	int y;
	int z;

	for (int i = 1; i<5; i++){
		if (mark[0] > mark[i]){
			temp = mark[0];
			mark[0] = mark[i];
			mark[i] = temp;
		}
		for (int i = 2; i<5; i++){
			if (mark[1] > mark[i]){
				temp = mark[1];
				mark[1] = mark[i];
				mark[i] = temp;
			}
			for (int i = 3; i<5; i++){
				if (mark[2] > mark[i]){
					temp = mark[2];
					mark[2] = mark[i];
					mark[i] = temp;
				}
				for (int i = 4; i<5; i++){
					if (mark[3] > mark[i]){
						temp = mark[3];
						mark[3] = mark[i];
						mark[i] = temp;
					}
				}
			}
		}
	}
}

int TheDupe(double mark[]){

	int x = 0;

	for (int i = 0; i < 4; i++) {
		for (int j = i + 1; j <= 4; j++) {
			if (mark[i] == mark[j]){
				x = 1;
				return (x);
			}
		}
	}
	return(x);
}

Open in new window

SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of flubbster

ASKER

Still working it with my son long distance.

<Kent>

I changed
 j <= Length
to
j < Length

because it always returned a zero (0) as the first number no matter what numbers were being sorted.
Actually, two lines should be changed.  

for (i = 0; i < Length; ++i)
    for (j = i+1; j <= Length; ++j)

change to:

for (i = 0; i < Length-1; ++i)
    for (j = i+1; j < Length; ++j)

The inner loop iterates through the items after the item at *i*, so *i* need never be set to the last item.  However, just changing the loop control for *j* sorts the items correctly as that loop exits immediately when *i* is set to the last item.

Working from memory here.  Guess I should have looked just a bit more carefully before submitting that.  :)
Gentlemen,

Thank you both for the help. My son texted me and he thinks he is all set. I will know for sure tonight (hopefully) and will close this question tomorrow. Always nice to be on the other side of helping people and getting help instead when I need it.

Thanks again.