3

The code bellow is supposed to return the value of PI in an array of a given length.

Once i run this code the console shows: 0000System.Int32[].
Console.WriteLine(newPi[i]); should print out 3141 which is the newPi.

Bellow is where i got so far:

namespace MakePi
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(MakePi(4));
            Console.ReadLine();

        }
        public static int[] MakePi(int n)
        {
            double pi = Math.PI;
            string piString = pi.ToString().Remove(1, 1);
            int[] newPi = new int[n];

            for (int i = 0; i < n; i++)
            {
                Console.Write(newPi[i]);                    
            }
            return newPi;
        }
    }
}  

I think the mistake is at int[] newPi = new int[n]; but i still got some difficulties fixing it, could someone help?

coze
  • 95
  • 2
  • 6

4 Answers4

2

You need to assign the values to the newPi array. Get the character at the index i, parse it to an int, and assign it to newPi[i].

private static void Main(string[] args)
{
    MakePi(4);
    Console.ReadLine();
}

public static int[] MakePi(int n)
{
    var pi = Math.PI;
    var piString = pi.ToString().Remove(1, 1);
    var newPi = new int[n];

    for (var i = 0; i < n; i++)
    {
        newPi[i] = int.Parse(piString[i].ToString());
        Console.Write(newPi[i]);
    }
    return newPi;
}

Result:

3141

You are also seeing System.Int32[] because you are printing out the string representation of the array, which does not actually print the contents.

Cyral
  • 13,999
  • 6
  • 50
  • 90
  • Why the additional `.ToString()`? Do you just want to add to the stack? – Der Kommissar Apr 29 '15 at 20:14
  • @EBrown int.Parse does not have a char overload. – Cyral Apr 29 '15 at 20:14
  • 1
    Ah, interesting. Why not `Convert.ToInt32(piString[i])` then? – Der Kommissar Apr 29 '15 at 20:15
  • I thought it did too, oddly lots of other system methods accept plenty of overloads for different types. – Cyral Apr 29 '15 at 20:15
  • 1
    @EBrown That will return the ASCII decimal value of the character, see the [source](http://referencesource.microsoft.com/#mscorlib/system/convert.cs,1037). (Also TIL) – Cyral Apr 29 '15 at 20:17
  • Right you are, my friend. Right you are. – Der Kommissar Apr 29 '15 at 20:19
  • 1
    Looks like `(int)char.GetNumericValue(piString[i])` works though. – Cyral Apr 29 '15 at 20:19
  • It worked for me but as you said i'm still seeing System.Int32[]. Could i make the output just 3141 by returning it as an array of integer? I tried it without the var keyword, just normal variable types and i got the same result. I just know that it lets the compiler decide what type to use, can you explain what difference it makes using var? Thank you . – coze Apr 29 '15 at 20:49
  • 1
    Using my code above should not show that, I've tested it. You can make it simply return the array, and then display them after `MakePi(4);` by doing `var pi = MakePi(4); foreach (var num in pi) Console.Write(num);` Var simply lets the compiler/IDE infer the type, as you said. See [What does "var" mean in C#?](http://stackoverflow.com/questions/4307467/what-does-var-mean-in-c) – Cyral Apr 29 '15 at 20:51
0

newPi is an int array, each element having the value 0 by default. Since you don't do anything with them, these values will remain 0.

You're also printing the newPi object for some reason, since that's what's returned from MakePi(4). Here:

Console.WriteLine(MakePi(4));
async
  • 1,537
  • 11
  • 28
0

EDIT: With an assist from Cryal, the below code should help

Your problem is that you are not assigning any values to the array, you are just attempting to print them out.

If I am understand your code correctly you are missing the following line:

newPi[i] = Int32.Parse(piString)

What this will do is take your pi string and convert it back to an integer and then place that integer within the array at the specific index of i

End result should look something like this:

namespace MakePi
{
    public  class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(MakePi(4));
            Console.ReadLine();

        }
        public static int[] MakePi(int n)
        {
            double pi = Math.PI;
            string piString = pi.ToString().Remove(1, 1);

            Console.WriteLine(piString);

            int[] newPi = new int[n];

            for (int i = 0; i < n; i++)
            {
                //Console.WriteLine("i:"+i);
                newPi[i] = (int)char.GetNumericValue(piString[i]);
                Console.Write(newPi[i]);                    
            }
            return newPi;
        }
    }
}  
Pseudonym
  • 2,052
  • 2
  • 17
  • 38
  • 1
    This is incorrect, for his situation, and will not compile. Try it and see what happens. – Der Kommissar Apr 29 '15 at 20:13
  • How so? If you referring to it not correctly grabbing multiple decimal points I am aware of that, but I left it as an exercise to the asker – Pseudonym Apr 29 '15 at 20:16
  • You miss a semicolon after `Int32.Parse(piString)`, as well as you are parsing the **entire** `piString`, rather than just one of the characters in it. Even after fixing the compile issue, it will not run at run-time due to an overflow of the fact that you just tried to stick `314159265358979` into an `integer`, which is far to large, of course. – Der Kommissar Apr 29 '15 at 20:22
-1
    public static int[] MakePi(int n)
    {


        var pi = Math.PI;
        var piString = pi.ToString().Remove(1, 1);
        var newPi = new int[n];

        for (var i = 0; i < newPi.Count() ; i++)
        {
            int.TryParse(piString.Substring(i, 1), out newPi[i]);
            Console.WriteLine(newPi[i]);
        }

        return newPi;
    }

You never added any value into the array.

edited my answer due to a miss previously. This will crash if you go above the length of piString, maybe add an if to check the size.

Peter B
  • 98
  • 8
  • 1
    This is literally horrible. This will fail flat on it's face if n is not `1`. – Der Kommissar Apr 29 '15 at 20:23
  • Read the question and see what it is trying to do. The length is assigned correctly in the OP. – Cyral Apr 29 '15 at 20:23
  • Ah embaressing, yes length was assigned. I did a mistake in my loop using the string length instead of the array count. public static int[] MakePi(int n) { var pi = Math.PI; var piString = pi.ToString().Remove(1, 1); var newPi = new int[n]; for (var i = 0; i < newPi.Count() ; i++) { int.TryParse(piString.Substring(i, 1), out newPi[i]); Console.WriteLine(newPi[i]); } return newPi; } – Peter B Apr 29 '15 at 20:33