Click here to Skip to main content
15,888,250 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
namespace Assignment4_2
{
    class SumArray
    {
        public class ProjectArray_Nonyelu
        {
            private int[] data;
            
            public void SumArray(int[] d)
            {
                data = d;
            }

            public int[] SumOfArray()
            {
                Console.Write("Enter an array size: ");
                int size = int.Parse(Console.ReadLine());

                //int i = 0, sum = 0;
                int[] anArray = new int[size];

                for (int i = 0; i < size; i++)
                    //sum += anArray[i];
                {
                    Console.Write("anArray[" + i + "] =  " );
                    anArray[i] = int.Parse(Console.ReadLine());
                }
                return anArray;
            }

            public static int[] SumOfArray(int[] d)
            {
                int[] a = { 5, 4, 6, 8 };

                int x = 0, sum = 0;
                for (int i = 0; i < a.Length; i++)
                    sum += a[i];
                {
                    Console.WriteLine(sum);
                }

                return a;
            }

            public int MaxOfArray()
            {
                int[] c = new int[5];
                int max;

                for (int i = 0; i < 5; i++)
                {
                    c[i] = int.Parse(Console.ReadLine());
                }
                max = c[0];

                for (int i = 0; i < 5; i++)
                {
                    if(c[i] > max)
                    {
                        max = c[i];
                    }
                }
                return max;
            }

            public static int[] MaxOfArray(int[] a)
            {
                int[] b = new int[5];
                int max;

                Console.Write("Enter 5 numbers: ");
                for (int i = 0; i < 5; i++)
                {
                    b[i] = int.Parse(Console.ReadLine());
                }

                max = b[0];

                for (int i = 0; i < 5; i++)
                {
                    if(b[i] > max)
                    {
                        max = b[i];
                    }
                }
                //return b;
                Console.WriteLine("The largest number is {0}", max);
                return b;
            }

            public void DisplayArray(String m)
            {
                Console.Write(m + "{");
                for (int i = 0; i < data.Length; i++)
                {
                    if(i != 0)
                        Console.Write(",");
                    Console.Write(data[i]);
                }
                Console.WriteLine("}");
            }

            public static void DisplayArray()
            {
                int[] a = { 3, 4, 5, 6 };
                Console.Write("{");
                for (int i = 0; i < a.Length; i++)
                {
                    Console.WriteLine(a);
                }
            }

            public static int[] ReadIntArray()
            {
                Console.WriteLine("Enter an array size:");
                int size = int.Parse(Console.ReadLine());

                int[] anArray = new int[size];

                for (int i = 0; i < size; i++)
                {
                    Console.WriteLine("anArray [" + i + "] = ");
                    anArray[i] = int.Parse(Console.ReadLine());
                }

                return anArray;
            }
        }

        public class TestProjectArray_Nonyelu
        {

        }
        static void Main(string[] args)
        {
            /*TestProjectArray_Nonyelu();
            SumOfArray();
            MaxOfArray();
            DisplayArray();
            ReadIntArray();*/
            
        }
    }
}


What I have tried:

I've tried to call all of the methods, but I keep getting errors. I've cancelled it out in the main.

I posted the directions of the assignment, so you can see how I wrote my code. If you can let me know my mistakes I'd really appreciate it.

Create a console application, Assignment4_YourLastName, then add two classes ProjectArray_yourLastName and TestProjectArray_yourLastName to your project.
I. Make your class ProjectArray_yourLastName have:
1. a private variable data of int array type;
2. a constructor which takes one parameter to initialize data;
3. an instance method SumOfArray which returns the sum of data elements;
4. a static method SumOfArray which takes one parameter of int array type and returns the sum of its parameter’s elements;
5. an instance methods MaxOfArray which returns the maximum element of data elements;
6. an static methods MaxOfArray takes one parameter of int array type and returns the maximum element of its parameter’s elements;
7. an instance method DisplayArray of displaying a message and data
8. a static method DisplayArray of displaying a message and an array
9. a static method ReadIntArray of reading an array and returning the array

II. Make your class TestProjectArray_yourLastName only has the Main method. In the Main method, add statements to call every method you declared in class ProjectArray_yourLastName at least once. Note: there are instance methods and static methods. They are declared and used in different ways!!!!!!!
Posted
Updated 6-Mar-16 21:21pm
v2
Comments
Philippe Mori 7-Mar-16 13:13pm    
The question does not make much sense. Your teacher want you to write at least one call to each of the functions. Thus you modify your Main method to simply do that. Also, it should be easy to figure out that you want to read data you need before calling functions that need some data.

1 solution

I'm not a fan of giving it all away for homework questions, so here's a hint:

Take a look at this line:
C#
Console.WriteLine("anArray [" + i + "] = ");

What are you doing there? You're calling the method WriteLine(..) of the class Console. Did you have to create an instance of Console to be able to do that? No - so it must be a static method. And, of course, you have to write "Console." in front of it, otherwise the compiler wouldn't know that you intend to call WriteLine(..) on the Console class and not as a method of the class you've written yourself there.
 
Share this answer
 
Comments
Yetco 6-Mar-16 22:11pm    
I'm still having trouble understanding instance and static methods and I see what you've explained, but I still don't get it
Sascha Lefèvre 7-Mar-16 3:24am    
Typing "Console." in Visual Studio brings up a small window with completion-suggestions, containing the valid options for the class "Console", right?
Have you tried this with the name(s) of your class(es) yet?
Yetco 7-Mar-16 10:55am    
yes, but it's not bringing up any of the classes. I don't know what I'm doing wrong.
Sascha Lefèvre 7-Mar-16 11:00am    
Just to make clear: It's not supposed to bring up your class names if you type "Console.". You need to type your class name and a dot to bring up the static members of the class - or the name of a variable of the type of your class and a dot to bring up the instance members of the class.
Yetco 7-Mar-16 18:13pm    
When I type the class SumArray, it only brings up ProjectArray_Nonyelu and TestProjectArray_Nonyelu, nothing else

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