Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to write follow this specifications:

1. I have an array read from user
2. Odd numbers is necessary to be at beginning
3. Even numbers is necessary to be at the end of array
4. Sort ascending the odd and even number
This is my code:

What I have tried:

#include <stdio.h>
#include <stdlib.h>

int n;

int main(void){
	scanf("%d", &n);
	int arr[n], aux[n], count = 0;
	for(int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
	}

	for(int i = 0; i < n; i++){
		if(arr[i] % 2 != 0){
			aux[i] = arr[i];
			count = i;
		}
		else
		{
			aux[n - i - 1] = arr[i];
		}
	}	

	for(int i = 0; i < count; i++){
		if( aux[i] > aux[i+1])
		{
			aux[i]   = aux[i] + aux[i+1];
			aux[i+1] = aux[i] - aux[i+1];
			aux[i]   = aux[i] - aux[i+1];

		}
	}

	for(int i = count; i < n; i++){
		if( aux[i] > aux[i+1])
		{
			aux[i]   = aux[i] + aux[i+1];
			aux[i+1] = aux[i] - aux[i+1];
			aux[i]   = aux[i] - aux[i+1];
		}
	}
	
	for(int i = 0; i < n; i++){
		printf("%d ", aux[i]);
	}

	printf("\n");

	return 0;
}


But, the output is wrong. For example if I entered the dimension of array 4, and elements of array is 4 3 2 1 the output is:2 4196800 1 4196832, but I want to be 1 3 2 4. Can you help me? Thank you!
Posted
Updated 20-Mar-18 6:48am

Your aux array is unitialised containing random values at start and you do not ensure that all elements are properly set.

I would use two helper variables for the odd and even indexes (odd index is zero at start and even index is n-1). Then fill aux in the first loop using the index that corresponds to the value, and increment resp. decrement that index.

Than you would have filled aux with the odd values at the beginnning, the even ones at the end, and the odd index should be one more than the even index (because of the increment/decrement).

Finally sort the odd and even values independantly. From the index helper variables you will know up to which index there are odd values. Sort that sub-array and then the sub-array of the even values starting behind.
 
Share this answer
 
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on your line:
C#
for(int i = 0; i < n; i++){

and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Yes, I could probably tell you what "the problem" is - but it's not difficult to do this yourself, and you will learn something really worthwhile at the same time!
 
Share this answer
 
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
The correct code is here! :D Thanks for help! :D
#include <stdio.h>
#include <stdlib.h>

#define n 4

int arr[n], aux[n];

int main(void)
{
	int count, j = 0, k = 0;
	
	for(int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
	}

	for(int i = 0; i < n; ++i)
	{
		if(arr[i] % 2 != 0)
		{
			aux[k] = arr[i];
			k++;
		}
		else
		{
			aux[n - j - 1] = arr[i];
			j++;
		}
	}

	for(int o = 0; o < k - 1; o++)
	{
		for(int i = 0; i < k - o - 1; i++)
		{
			if( aux[i] > aux[i+1])
			{
				aux[i]   = aux[i] + aux[i+1];
				aux[i+1] = aux[i] - aux[i+1];
				aux[i]   = aux[i] - aux[i+1];

			}
		}
	}

	for(int o = k; o < n - 1; o++)
	{
		for(int i = k; i < n - o - 1; i++)
		{
			if( aux[i] > aux[i+1])
			{
				aux[i]   = aux[i] + aux[i+1];
				aux[i+1] = aux[i] - aux[i+1];
				aux[i]   = aux[i] - aux[i+1];
			}
		}
	}

	for(int i = 0; i < n; i++){
		printf("%d ", aux[i]);
	}

	printf("\n");

	return 0;
}
 
Share this answer
 
Comments
Graeme_Grant 29-Aug-17 0:58am    
Please don't post answers, fixes, etc... to your own questions as a solution. This is considered "Reputation Farming", is bad etiquette and is seriously frowned upon. Instead, post an update in the question itself by clicking on the "Improve question" widget. Once done, I highly recommend that you delete this solution.
I didn't compile this, but something like this should work: I prefer just defining arrays... just old school I guess.

#include <stdio.h>
#include <stdlib.h>

int n;
int tmp;

#define swap( a, b ) { tmp = (a); (a) = (b); (b) = tmp; }

int main( void )
{
    int i, j, k;
    int arr[500], aux[500];
    int count = 0; 
    int odds = 0, evens = 0;
    
    printf( "How many numbers? " ); 
    scanf( "%d", &n );

    for ( i = 0; i < n; i++)
    {
        printf( "Entry #%0d:", i );
        scanf( "%d", &arr[i] );
    }

    // First break into odds and evens
    for ( i = 0; i < n, i++ )
    {
        // if it is odd
        if ( arr[i] & 0x01 )
        {
           aux[i] = arr[i];
           odds++;
        }
        else
        { 
           aux[n-1-i] = arr[i];
           evens++;
        };
    };
    
    // Now sort the odds... then sort the evens	
    if ( odds > 1 )
    {
        for ( j = 0; j < odds; j++ )
        {
            for ( k = 0; k < ( odds - 1 - j ); k++ )
            {
                if ( aux[k] > aux[k+1] ) swap( aux[k], aux[k+1] );
            };
        };
    };
    
    if ( evens > 1 )
    {
        for ( j = odds; j < n; j++ )
        {
            for ( k = odds; k < ( n - 1 - j ); k++ )
            {
                if ( aux[k] > aux[k+1] ) swap( aux[k], aux[k+1] );
            };
        };
    };
    
	
    for( i = 0; i < n; i++ )
    {
        printf("%d ", aux[i] );
    }

    printf("\n");

    return 0;
}


Hopefully this will help.
-Doug
 
Share this answer
 
Comments
Patrice T 20-Mar-18 12:53pm    
If you read solution 4, you will see the OP have solved its problem.
Doug Joseph 20-Mar-18 13:47pm    
I think this one is a better solution... but whatevs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900