using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MonthlySales
{
class Program
{
static void Main(string[] args)
{
int num;
double[] sales = {0};
Inputs(out num, sales);
CLASS obj = new CLASS();
obj.setNumMonths(num);
obj.setSales(sales);
obj.Process();
Outputs(obj);
Console.Read();
}
public static void Inputs(out int a, params double[] b)
{
Console.Write("Enter number of sales: ");
a = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < a; i++)
{
Console.Write("Please enter sale #{0}: ", i + 1);
b[i] = Convert.ToDouble(Console.ReadLine());
}
}
public static void Outputs(CLASS obj)
{
for (int i = 0; i < obj.getNumMonths(); i++)
{
Console.WriteLine("Sale # {0} was {1:C2} and contributed {2:P2}", i + 1, obj.getSales()[i], obj.prop_Contrib);
}
Console.WriteLine("Total sum of sales is {0:C2}", obj.prop_Total);
}
}
}
CLASS.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MonthlySales
{
class CLASS
{
int numMonths, i;
double[] sales;
double total, contrib;
public CLASS() { } // def constructor
public CLASS(int a, double[] b) //constructor
{
numMonths = a;
sales = new double[numMonths];
sales = b;
}
public void setNumMonths(int a) //setter
{
numMonths = a;
}
public void setSales(double[] a) //setter
{
sales = a;
}
public int getNumMonths() //getter
{
return numMonths;
}
public double[] getSales() //getter
{
return sales;
}
public double prop_Total //property
{
get { return total; }
}
public double prop_Contrib //property
{
get { return contrib; }
}
public void Process()
{
total = 0;
for(i=0; i < numMonths; i++)
{
total = total + sales[i];
}
contrib = sales[i] / total;
}
}
}
When the program asks for the inputs of the user, it runs well. But when it is time to display the output, the console automatically closes. It throws an error in Inputs(out num, sales);
and b[i] = Convert.ToDouble(Console.ReadLine());
( located in the main program). It says
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Any insights would help. Many thanks.