0

I am trying to create a method that sums and outputs integers in an array. I understand how to sum the integers, however I am having trouble producing the desired output.

If I pass into the method 8, 3, 3 I need the output to look as follows.

For the list = <8, 3, 3> the sum is: 14

Once again I am familiar with how to sum, I am unfamiliar with how to format this. Here is my method so far...

public static void Sum(params int[] number)
{
    int total = 0;
    for (int i = 0; i < number.Length; ++i)
      {
         total = total + number[i];
      }

    Console.Write("For the list =, the sum of its elements is : {0}.", total);
    Console.Write("\n");
}   
2
  • String.Join() is the easy and elegant answer, but for learning: You have to break up the Write() part into a head, a body with a for-loop and a tail. Commented Jun 14, 2014 at 10:05
  • hey thanks. I understood that concept, however the formatting wasn't quite right.
    – PerryH
    Commented Jun 14, 2014 at 10:39

4 Answers 4

1

Use the String.Join method to make a string with the values in the array:

string values = String.Join(", ", number);

Then just add it to the output:

Console.Write("For the list = <{0}> the sum of its elements is : {1}.", values, total);

Prior to framework 4 there is no overload of String.Join that can take an array of anything other than string, so if you are using an older framwork you need to turn the integers to strings:

string values = String.Join(", ", number.Select(n => n.ToString()));
3
  • You'll need to convert the items to strings in the array.. since string.Join takes a string array.. Commented Jun 14, 2014 at 9:40
  • @SimonWhitehead: You only need to do that if you are using a framework older than 4. I just added a paragraph about that.
    – Guffa
    Commented Jun 14, 2014 at 9:44
  • Thanks Guys...That is doing the trick. I need to look into the .Join method a little more. very handy.
    – PerryH
    Commented Jun 14, 2014 at 10:40
0
public static void Sum(params int[] number)
{
    int total=0;
    for (int i = 0; i < number.Length; ++i) total = total + number[i];
    string ext = String.Format ("<{0}>", String.Join (",", number));

    Console.Write("For the list ={0} the sum is: {1}.",ext,  total);
    Console.Write("\n");
} 
0

There are several ways you could go to produce the desired output, one would be: public static void Sum(params int[] number)

{
    int total=0;
    StringBuild tmp="For the list =
    for (int i = 0; i < number.Length; ++i)
      {
         total = total + number[i];

      }
    Console.Write("For the list =, the sum of its elements is : {0}.", total);
    Console.Write("\n");
}   
0

This will do it:

using System;
using System.Linq;

public class Test
{
    public static void Main()
    {
        Sum(8,3,3);
    }

    public static void Sum(params int[] number)
    {
        Console.WriteLine("For the list <{0}>, the sum of its elements is: {1}", 
            string.Join(", ", number), 
            number.Sum());
    }   
}

outputs:

For the list <8, 3, 3>, the sum of its elements is: 14

Click here for a working sample

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.