Click here to Skip to main content
15,913,486 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
hi I write a program with C# and when i test it the result is wrong
and i write this with php and the output is true
.
in exchange array in C# it has problem.
please say to me what's the problem?

please download it from this link and trace the code. and say to me.
download link


the code in C#:

C#
try
            {

                StreamReader sr = new StreamReader("input.in");
                using (StreamWriter sw = new StreamWriter("output.out"))
                {
                    int a = int.Parse(sr.ReadLine());
                    for (int i = 0; i < a; i++)
                    {
                        string neveshteh = sr.ReadLine();
                        string[] arrayin;
                        arrayin=neveshteh.Split(' ');
                        int n = int.Parse(sr.ReadLine());
                        string[] arrayout = arrayin;

                        for (int j = 0; j < n; j++)
                        {
                            string charkhesh = sr.ReadLine();
                            string[] arr = charkhesh.Split(' ');
                            string bod = arr[0];
                            int m = int.Parse(arr[1]);
                            Console.WriteLine(arr[0]);
                            Console.WriteLine(arr[1]);
                            if (bod == "X")
                            {
                                for (int k = 1; k <= (m % 4); k++)
                                {
                                    arrayout[0] = arrayin[2];
                                    arrayout[1] = arrayin[0];
                                    arrayout[2] = arrayin[5];
                                    arrayout[3] = arrayin[3];
                                    arrayout[4] = arrayin[4];
                                    arrayout[5] = arrayin[1];
                                    arrayin = arrayout;


                                }
                            }
                            else if(bod == "Y")
                            {
                                for (int k = 1; k <= (m % 4); k++)
                                {
                                    arrayout[0] = arrayin[0];
                                    arrayout[1] = arrayin[3];
                                    arrayout[2] = arrayin[4];
                                    arrayout[3] = arrayin[2];
                                    arrayout[4] = arrayin[1];
                                    arrayout[5] = arrayin[5];

                                    arrayin = arrayout;
                                }
                            }
                            else if (bod == "Z")
                            {
                                for (int k = 1; k <= (m%4) ; k++)
                                {
                                    arrayout[0] = arrayin[3];
                                    arrayout[1] = arrayin[1];
                                    arrayout[2] = arrayin[2];
                                    arrayout[3] = arrayin[5];
                                    arrayout[4] = arrayin[0];
                                    arrayout[5] = arrayin[4];

                                    arrayin = arrayout;

                                }
                            }

                        }
                        for (int s = 0; s < 6; s++)
                        {
                            sw.Write(arrayout[s] + " ");
                        }
                        sw.WriteLine();




                    }

                }

            }
            catch
            {
            }

the cod ein php :

PHP
<?php

                $sr = file("input.in");
                $handle=fopen("output.out","a+");

                    $a = (int)$sr[0];
                    $myline=0;
                    for ($i = 0; $i < $a; $i++)
                    {
                        $myline++;
                        $neveshteh = $sr[$myline];
                        $neveshteh=str_replace(array("\r","\n"),"",$neveshteh);
                        //string[] arrayin = new string[6];
                        $arrayin=explode(" ",$neveshteh);
                        $myline++;
                        $n = (int)$sr[$myline];
                        $arrayout = $arrayin;

                        for ($j = 0; $j < $n; $j++)
                        {
                            $myline++;
                            $charkhesh = $sr[$myline];
                            $arr = explode(" ",$charkhesh);;
                            $bod = $arr[0];
                            $m = (int)$arr[1];
                            if ($bod == "X")
                            {
                                for ($k = 1; $k <= ($m % 4); $k++)
                                {
                                    $arrayout[0] = $arrayin[2];
                                    $arrayout[1] = $arrayin[0];
                                    $arrayout[2] = $arrayin[5];
                                    $arrayout[3] = $arrayin[3];
                                    $arrayout[4] = $arrayin[4];
                                    $arrayout[5] = $arrayin[1];
                                    $arrayin = $arrayout;


                                }
                            }
                            else if($bod == "Y")
                            {
                                for ($k = 1; $k <= ($m % 4); $k++)
                                {
                                    $arrayout[0] = $arrayin[0];
                                    $arrayout[1] = $arrayin[3];
                                    $arrayout[2] = $arrayin[4];
                                    $arrayout[3] = $arrayin[2];
                                    $arrayout[4] = $arrayin[1];
                                    $arrayout[5] = $arrayin[5];
                                    $arrayin = $arrayout;
                                }
                            }
                            else if ($bod == "Z")
                            {
                                for ($k = 1; $k <= ($m%4) ; $k++)
                                {
                                    $arrayout[0] = $arrayin[3];
                                    $arrayout[1] = $arrayin[1];
                                    $arrayout[2] = $arrayin[2];
                                    $arrayout[3] = $arrayin[5];
                                    $arrayout[4] = $arrayin[0];
                                    $arrayout[5] = $arrayin[4];
                                    $arrayin = $arrayout;

                                }
                            }

                        }
                        for ($s = 0; $s < 6; $s++)
                        {
                            fwrite($handle,$arrayin[$s]." ");
                        }
                        fwrite($handle,"\r\n");




                    }




?>
Posted
Updated 14-Oct-14 10:33am
v3
Comments
Drazen Pupovac 14-Oct-14 16:22pm    
There is the link, but wrong path. It should be: http://www.gozindadeh.com/mokaab.rar
Member 10289380 14-Oct-14 16:25pm    
excuse me, i correct it
Kornfeld Eliyahu Peter 14-Oct-14 16:27pm    
Probably not too much CPians will download something from an unknown link from an unknown person...
Please paste in your relevant code as part of your question...
Member 10289380 14-Oct-14 16:33pm    
i do it
Member 10289380 14-Oct-14 16:29pm    
yes, and i correct the link

1 solution

You must explain better the issue, but I think you have problem with references in C#. Basically, what are you trying is to clone the array and create temporary, but array in C# is reference, what mean that "arrayout" and "arrayin" point to the same value in the heap.

C#
string[] arrayin;
arrayin=neveshteh.Split(' ');
int n = int.Parse(sr.ReadLine());
string[] arrayout = arrayin;


Instead of using "=", you can copy all values from one array to the another in a loop. Also, this method should work link (I didn't try it yet).
 
Share this answer
 
v2
Comments
Member 10289380 14-Oct-14 16:49pm    
please insert a checkpoint and press f11

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