Member Avatar for Falcon25

Hello everyone! I'm new to c# and i've come on here hoping your knowledge can help me out. I'm trying teach myself at home but have got really stuck on this question, any help would be great

"Declare an integer array of 10 elements. Fill the elements with values that the user types in at the keyboard. Display these values on the console."

I think this seems simple enough but I can't seem to fill the elements with what the user types in without writing:

elements[0] = Console.ReadLine();
elements[1] = Console.ReadLine();
elements[2] = Console.ReadLine();

This gets long and 'feels' wrong I think there is a way of just filling them all

And then the next question is this:

"Extend your program from activity 1 to calculate the sum and average of the numbers stored in the array. Display each number on the console with a message stating whether it is below average, above average or equal to the average."

This is way beyond what i'm capable of yet, help would be appreciated! thank you

Recommended Answers

All 5 Replies

Hello Falcon25, welcome.
What do you think should be the type of your elements array in your posted code?
You will also need some kind of looping, here is a start: http://msdn.microsoft.com/en-us/library/ch45axte.aspx

Member Avatar for Falcon25

Hello Falcon25, welcome.
What do you think should be the type of your elements array in your posted code?
You will also need some kind of looping, here is a start: http://msdn.microsoft.com/en-us/library/ch45axte.aspx

Hello ddanbe! thanks for the reply, and the link.

They are strings and then I think I need to convert all 10 of them into integers (as you can tell I'm not that experianced in programming yet, but i'll stick at it!) then I need to display what they have written onto the console window- I think I use a foreach for that.

Then the second question tells me to add them all up and show the average which is just too difficult for me to do yet. Tried making this code work with a for-loop but unfortunately it doesn't :icon_confused:

static void activityTwo()
        {
            int[] elements = new int[10];
            for (int count = 0; count > 10; count++)
            {
                Console.WriteLine("Please enter a number");
                string numbers = Console.ReadLine();
                elements[count] = Int32.Parse(numbers);
            }
                  
        }

Have a look at this:

namespace ConsoleApplication1
{
    class Program
    {
        const int arraysize = 10; // contant defs are handy

        static int[] elements = new int[arraysize]; // global to this class

        static void Main(string[] args)
        {
            activityTwo();
            Console.WriteLine("The sum is: {0}", Sum());
            Console.ReadKey();
        }

        static void activityTwo()
        {
            // moved def of elements
            for (int count = 0; count < arraysize; count++) //changed > to < !
            {
                Console.WriteLine("Please enter number {0}:", count + 1);
                string numbers = Console.ReadLine();
                elements[count] = Int32.Parse(numbers);
                // altenative coding: elements[count] = Int32.Parse(Console.ReadLine())
                // you still have to do some error checking, what if a user types a letter?
                // should leave that for later now
            }
        }

        static int Sum()
        {
            int sum = 0;
            for (int i = 0; i < arraysize; i++)
            {
                sum += elements[i];
            }
            return sum;
        }

    }   
}

Have a close look at the comments, you should be able to figure out the rest for yourself.
If not, just ask!

commented: Nice guy, very helpful +0
Member Avatar for Falcon25

Have a look at this:

namespace ConsoleApplication1
{
    class Program
    {
        const int arraysize = 10; // contant defs are handy

        static int[] elements = new int[arraysize]; // global to this class

        static void Main(string[] args)
        {
            activityTwo();
            Console.WriteLine("The sum is: {0}", Sum());
            Console.ReadKey();
        }

        static void activityTwo()
        {
            // moved def of elements
            for (int count = 0; count < arraysize; count++) //changed > to < !
            {
                Console.WriteLine("Please enter number {0}:", count + 1);
                string numbers = Console.ReadLine();
                elements[count] = Int32.Parse(numbers);
                // altenative coding: elements[count] = Int32.Parse(Console.ReadLine())
                // you still have to do some error checking, what if a user types a letter?
                // should leave that for later now
            }
        }

        static int Sum()
        {
            int sum = 0;
            for (int i = 0; i < arraysize; i++)
            {
                sum += elements[i];
            }
            return sum;
        }

    }   
}

Have a close look at the comments, you should be able to figure out the rest for yourself.
If not, just ask!

Thanks man you've helped me out a lot. I get it now and looking at your code visually helps me understand how to put it all together nicely. I'm new to programming, when it works there's a strange sence of satisfaction I guess thats why a lot of people program being creative and logical mould into one! Brilliant. Thank You :icon_biggrin:

when it works there's a strange sence of satisfaction

That's one of the reasons we all do it for. :)
Happy programming.
If this solves your problem, mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.