Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code in C# , i want to Sort random "complex number " by real number.
Problem is didn't sort by real number!

any idea?

like this :
120 + 160 i
121 + 105 i
122 + 189 i
...



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tamrine2
{
    class Complex
    {
        public int real, imag;


        private static Random TheRandom = new Random();

        public void Set()
        {
          
            
            real = TheRandom.Next(100, 200);
            
            imag = TheRandom.Next(100, 200);
            

        }



        public void Write()
        {
            char sign;
            sign = (imag < 0) ? '-' : '+';
            
            Console.WriteLine("{0} {1} {2} i ", real, sign, Math.Abs(imag));
        }

    }
    class Program
    {
        private static int n;
        static void Main(string[] args)
        {
            Console.Write("How Many Number : ");
            n = Convert.ToInt32(Console.ReadLine());

            Complex[] complexArray = new Complex[n];

            for (int i = 0; i < n; ++i)
            {
                complexArray[i] = new Complex();
               
            }

           
            for (int i = 0; i < n; ++i)
            {
                
                complexArray[i].Set();
                
                complexArray[i].Write();
             
            }
         


            realSort(complexArray);



            Console.ReadKey();
        }

        public static void realSort(Complex[] s)
        {
            Complex temp;
            for (int i = 1; i <= s.Length - 1; ++i)
                for (int j = 0; j < s.Length - i; ++j)
                    if (s[j].real > s[j + 1].real)
                    {
                        temp = s[j];
                        s[j] = s[j + 1];
                        s[j + 1] = temp;
                    }
        }
    }
}
Posted
Updated 27-Nov-15 22:41pm
v2
Comments
OriginalGriff 28-Nov-15 4:32am    
And?
What is your problem?
sdvfs 28-Nov-15 4:40am    
didn't sort by real number !
Patrice T 28-Nov-15 5:33am    
How do you see that the numbers are not sorted ?
BillWoodruff 28-Nov-15 7:05am    
You have some choices here:

1. write your own sort code (as you are doing now), and get it to work. as many other people here have said to you, you need to learn to debug. for a beginner, in my opinion learning to debug is a critical skill.

2. learn how to use Linq to sort a generic List of Complex.

3. learn how to implement the IComparable and/or IEquatable interfaces for the Complex Class so sorting is "built-in."

My impression is that you are very new to the language, so, I suggest you stick with writing your own simple sort ... you'll learn a lot doing that.

Using Linq to sort, and implementing custom sorts via Interface implementation can come later ... and then you'll really appreciate how they simplify sorting.

 
Share this answer
 
"didn't sort by real number !"

OK - but we have no idea what data you were looking at, or how you decided that it didn't work.
Plus, it's your homework, so it's a good idea that you sort this yourself - you will learn a lot more than if I fix it and give you the sorted code.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
sdvfs 28-Nov-15 5:25am    
yes it is my homework and definitely i've done what ever you mention after that post here!
as you see in last section i've write sort class that work for other cases but not work for random number! and thing the reason is this ! i mean random numbers are different (maybe because using seed)!
do you have any other idea?
Thank you in advance
OriginalGriff 28-Nov-15 5:47am    
Use the debugger and look at what is happening. Seriously.
Put a breakpoint at the top of the sort method, and when it hits, look at the actual data. Then step through looking at what actually happens with your code. At some point it should be obvious that what you expect to happen isn't - and then it's generally pretty simple to work out why.
BillWoodruff 28-Nov-15 7:20am    
Sorting a list of random numbers is no different from sorting any other list of numbers.
BillWoodruff 28-Nov-15 7:21am    
+5 Amen !
I see 2 reasons for your program not listing the numbers sorted.

First: you list the numbers before sorting them.
Second: the sort routine is bugged to the core. The general layout is of an insertion sort, but it is bugged and don't work.

Since it is HomeWork, no solution, but advices.
- Review your program to list the numbers after sorting them.
- Review the insertion sort algorithm and do necessary corrections.

More general advice:
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behaviour, you match it against your expectations.

secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.

You should find pretty quickly what is wrong.
 
Share this answer
 
v2
Comments
BillWoodruff 28-Nov-15 7:21am    
+5 good advice and good link
Patrice T 28-Nov-15 8:04am    
Thank you
[no name] 28-Nov-15 7:42am    
Cool answer, my 5!
Patrice T 28-Nov-15 8:04am    
Thank you
Never ever reinvent sorting yourself. If this is a homework assignment, do it according to your text book. If it is otherwise needed, use any existing sorting, i.e. by using some of the existing Sort functions, e.g.
C#
Array.Sort(complexArray,(a,b)=>a.real.CompareTo(b.real));
Regards
Andi
 
Share this answer
 
Comments
sdvfs 28-Nov-15 11:10am    
Thank you so much dear Andi, it work perfectly. ;)

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