wootscootinboogie
wootscootinboogie

Reputation: 8695

Method using user-defined size not returning anything to the Console

Below is a program that I wrote to practice some C# basics and principles.

This program asks the user for how large they want an int array to be, then to fill the array and finally return the average of the individual elements of the array.

I know that I can do this using LINQ but since I'm learning I need to learn the nuts and bolts way.

As it stands, the method I wrote doesn't return anything to the console, can someone give me a clue as to what's wrong with it?

There's also a comment near one of the for loops that I need some help understanding why it's behaving that way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _9_21_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("enter the amount of numbers you would like to find the average of: ");
            int arraylength = Int32.Parse(Console.ReadLine());
            int[] AverageArray = new int[arraylength];

            ////filling the array with user input
            for (int i = 0; i < AverageArray.Length; i++)
            {
                Console.Write("enter the numbers you wish to find the average for: ");
                AverageArray[i] = Int32.Parse(Console.ReadLine());

            }
            //printing out the array, without the -1 the array prints out more one number than it should, don't know why
            Console.WriteLine("here is your array: ");
            for (int i = 0; i < AverageArray.Length -1 ; i++)
            {
                Console.WriteLine(AverageArray[i]);
            }
            Console.WriteLine(Calcs.FindAverage(AverageArray));
            Console.ReadLine();


        }

    }
    //Method to find the average is another class for learning porpoises
    class Calcs
    {
        public static double FindAverage(int[] averageNumbers)
        {
            int arraySum = 0;

            for (int i = 0; i < averageNumbers.Length; i++)
                arraySum += averageNumbers[i];

            return arraySum / averageNumbers.Length;
        }
    }
}

Upvotes: 0

Views: 1281

Answers (2)

Shaz
Shaz

Reputation: 11

Well I think Visual Studio C#(Sharp) Will give you an error on the second statement of code before the for loop,Because the Array uses the constant variable for the index size.

        Console.WriteLine("enter array length ");
  const int arraylength = Int32.Parse(Console.ReadLine());
        int[] AverageArray = new int[arraylength];

also Check Calcs does not exist in the current context.

Upvotes: 1

george.zakaryan
george.zakaryan

Reputation: 980

I've tried debugging, your code is working OK. It's just you don't need the ArrayLength-1 in the second loop. Use ArrayLength instead.

 static void Main(string[] args)
        {
            Console.WriteLine("enter array length ");
            int arraylength = Int32.Parse(Console.ReadLine());
            int[] AverageArray = new int[arraylength];


            for (int i = 0; i < AverageArray.Length; i++)
            {
                Console.Write("enter the numbers you wish to find the average for: ");
                AverageArray[i] = Int32.Parse(Console.ReadLine());

            }

            Console.WriteLine("here is your array: ");
            for (int i = 0; i < AverageArray.Length ; i++)
            {
                Console.WriteLine(AverageArray[i]);
            }

            Console.WriteLine("here is the result");
            Console.WriteLine(Calcs.FindAverage(AverageArray));
            Console.ReadLine();

        }

Upvotes: 2

Related Questions