Jump to content
            //Ask user for Complex Number + reads it             Console.WriteLine("Please Enter your Complex Number");            string imaginaryNo = Console.ReadLine();            //Converts number into char array            char[] imaginaryNoArray = imaginaryNo.ToArray();            //Goes through each char in array            for (int i = 0; i < imaginaryNo.Length; i++)            {                //finds the index of the imaginary part of the number                if (imaginaryNoArray[i] == 'i')                {                    //Only works for single digit imaginary numbers                     char Imaginary = imaginaryNoArray[(i - 1)];                    Console.WriteLine("The Imaginary Part of your number is {0}" ,Imaginary);                 }            }#region                 //Ignore this            //var c1 = new Complex(1,2);             //Console.WriteLine(c1.ToString()); #endregion        

I'm trying get an imaginary part of a number this example works only for single digit Imaginary numbers:

e.g. 

10 + 5i ---> result being = The Imaginary Part of your number is 5 

 

10+ 63i ---> result being = The Imaginary Part of your number is 3 (should be 63) 

 

How would I implement it to work for any number of digits? 

 

Also I can't seem to get Imaginary to work outside the if loop(just says doesn't exist within the current context.

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
https://linustechtips.com/topic/294068-how-would-i-do-this-c/
Share on other sites

Link to post
Share on other sites

You're almost there. Instead of hard-coding the i - 1 to get the index of the number, call a function that walks backwards from the index of "i", gathering all the preceding integers into a string to print.

Yeah, I finally did it, not exactly the most succinct code ever but, it's better than not working at all.

//empty string to hold the data             string imagString = "";            int imaginaryNoIndex = 1;             //Ask user for Complex Number + reads it             Console.WriteLine("Please Enter your Complex Number");            string imaginaryNo = Console.ReadLine();            //Converts number into char array            char[] imaginaryNoArray = imaginaryNo.ToArray();            //Goes through each char in array backwards            for (int i = imaginaryNo.Length- 1; i > 0; i--)            {                //finds the index of the imaginary part of the number                if (imaginaryNoArray[i] == 'i')                {                    imaginaryNoIndex = i-1;                     Console.WriteLine(imaginaryNoIndex);                }                //Goes through the array backwards until it no longer finds a # value.                while (char.IsNumber(imaginaryNoArray[imaginaryNoIndex]))                {                    imagString = imagString + imaginaryNoArray[imaginaryNoIndex];                    imaginaryNoIndex--;                    break;                }                            }                        char[] imagStringArray = imagString.ToCharArray();            //Reverse the char array.             Array.Reverse(imagStringArray);            //Make a new string from the inversed char array.             string stringReverse = new string(imagStringArray);             Console.WriteLine("Imaginary Number is {0}i", stringReverse);            Console.ReadLine();            } 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
https://linustechtips.com/topic/294068-how-would-i-do-this-c/#findComment-4000313
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×