views:

30

answers:

1

Below is draft number 5 for my C# Homework this week. I wrote the program out using Linq first, and it worked fine. Unfortunately, the directions state that I must create my own method instead of using the wonderful Sum() method already found in Linq. The major problem with this source code is that the method overload is incorrect (and it's also probable that my entire Sum() method is wrong too). Since our almighty text doesn't clearly explain how to overload a method like this, I'm kind of lost... (or a lot lost).

Here are the assignment instructions (again):

"Create a method named Sum()that accepts any number of integer parameters and displays their sum. Write a Main()method that demonstrates the Sum()method works correctly when passed one, three, five, or an array of 10 integers. Save the program as UsingSum.cs."

from Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell

Here is my source code:

using System;

public class UsingSum
{
    public static void Main()
    {

        //Step 1: Adding 1, 3 and 5

        int[] array1 = { 1, 3, 5 };

        int a;
        int b;
        int c;
        int d;
        int e;
        int f;
        int g;
        int h;
        int i;
        int j;        
        int firstSum;
        int secondSum;

        Console.Write("When the numbers 1, 3 and 5 are added together, using the Sum() method, the answer is: ");

        firstSum = Sum(array1);
        Console.WriteLine("{0}", firstSum);



        //Step 2: Entering variables into Array2[10]


        Console.Write("Enter first integer for addition: ");
        a = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter second integer for addition: ");
        b = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter third integer for addition: ");
        c = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter forth integer for addition: ");
        d = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter fifth integer for addition: ");
        e = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter sixth integer for addition: ");
        f = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter seventh integer for addition: ");
        g = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter eighth integer for addition: ");
        h = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter ninth integer for addition: ");
        i = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter tenth integer for addition: ");
        j = Convert.ToInt32(Console.ReadLine());

        int[] array2 = { a, b, c, d, e, f, g, h, i, j };

        Console.Write("The total of {0} + {1} + {2} + {3} + {4} + {5} + {6} + {7} + {8} + {9} is: ",
        a, b, c, d, e, f, g, h, i, j);

        secondSum = Sum(array2);
        Console.WriteLine("{0}", secondSum);


    }


//Step 3: Defining the Sum() method

   public static int Sum(int a, int b)

//My overload is generating error CS1501: No overload for method 'Sum' takes '1' arguments

   {

   int sum = 0;
   int[] adder = new int[0];
//designating an array with no parameters...

   for(int a = 0; a < adder.Length; ++a)
      adder[a] = a;

   foreach(int b in adder)
      sum += b;
      Console.WriteLine(" " + sum);
   }
}
+1  A: 

You are defining Sum to take 2 arguments

public static int Sum(int a, int b)

but only calling it with 1 argument

firstSum = Sum(array1);

Try defining Sum to take an int array:

public static int Sum(int[] input)
adrift
Ok, I'm giving that a try :-) Thank you for explaining the problem so clearly.
Nooob
It worked! :-) I forgot to add the return at the end. The actual Sum function is generating 0 for everything, but I'll figure it out... Thank you for your help.
Nooob
...My source codes are compiling and working perfectly. It was generating 0 because I referenced the adder array in foreach(int b in adder) instead of referencing the "input" array used to overload the Sum() method. A huge thank you to all the people who helped me figure this stuff out this weekend. :-)
Nooob
As a small note the keyword params is worth looking into for this sort of assignment :)
Frank