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

LVL 30
flubbsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi flubbster,

at a first view I found at least two errors:

1. in main the if statements use assignment-operator '=' instead of comparsion operator '=='. This means always the first 'if' is true ( because 'x = 1' == 1) and the second is always false ('x= 0' == 0).

2. in order the outer loop starts with index 1, but in C arrays are always zero-based, so the first element isn't evaluated.

Beside this I would suggest to implement the sorting in order in a more convenient way, there's no need to do an extra loop for every element (i.e. your code cannot work for any other size of input array).

It is better to implement some common, generic sort algorithms - a good start to find out more about these could be http://www.sorting-algorithms.com/

Hope that helps,

ZOPPO
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi flubbster,

The way that the sort is coded suggests that the author doesn't really understand arrays and loops.  But that's part of what learning to program is all about!  The code suggests that if the array contained 100 items the sort would be VERY long!

The simplest sort to code is a successive maximum (or successive minimum) sort.  It's very inefficient on large volumes of data, but it works and codes easily.  Try this, then move to more advanced sorting techniques.  Many of them code easily enough for beginners to understand.  

Good Luck!
Kent


SORT (double Array[], int Length)
{
  int i;
  int j;
  double temp;

  for (i = 0; i < Length; ++i)
    for (j = i+1; j <= Length; ++j)
      if (Array[i] > Array[j])
      {
        Temp = Array[i];
        Array[i] = Array[j];
        Array[j] = Temp;
      }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
flubbsterAuthor Commented:
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.
0
Kent OlsenData Warehouse Architect / DBACommented:
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.  :)
0
flubbsterAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.