0

I have a program that calculates the average sales from 3 regions. My problem is that the input is more than half the program, is there a way to reduce the all the input commands?

static void Main()
        {

            while (true)
            {
                try
           
                    System.Console.Write("Enter sales for region1: store 1: "); // asks user to enter Store1, region1 sales.
                    double a = Convert.ToDouble(Console.ReadLine());// creates double variable a
                    System.Console.Write("Enter sales for region1: store 2: "); // asks user to enter Store2, region1 sales.
                    double b = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region1: store 3: "); // asks user to enter Store3, region1 sales. 
                    double c = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region1: store 4: "); // asks user to enter Store4, region1 sales.
                    double d = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region1: store 5: "); // asks user to enter Store5, region1 sales.
                    double e = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region2: store 1: "); // asks user to enter Store1, region2 sales.
                    double f = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region2: store 2: "); // asks user to enter Store2, region2 sales.
                    double g = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region2: store 3: "); // asks user to enter Store3, region2 sales.
                    double h = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region3: store 1: "); // asks user to enter Store1, region3 sales.
                    double l = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region3: store 2: "); // asks user to enter Store2, region3 sales.
                    double k = Convert.ToDouble(Console.ReadLine());

                    double[][] region = new double[3][];    // scores
                    region[0] = new double[5] { a, b, c, d, e }; // input into region0 array
                    region[1] = new double[3] { f, g, h, };//inout into region1 array
                    region[2] = new double[2] { l, k }; //input into region3 array
                    double sum;       // sum of the sales for each region
                    double total = a + b + c + d + e + f + g + h + l + k; //for calulation of company sales.

Thanks for the ideas.
-sloshy

3
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by sloshymon
Featured Replies
  • Take a look at something like this and add the data directly into the array instead of temporary variables: [CODE] using System; namespace DW_412294_CS_CON { class Program { const int int_NumRegions = 3; static int[] arr_intStoresInRegions = { 5, 3, 2 }; static double[][] arr_dblRegions = new double[int_NumRegions][]; static void … Read More

0

You can use a List<double> to hold your values.
...or two List<double>

I have an array holding the values I believe.

double[][] region = new double[3][]; // scores
region[0] = new double[5] { a, b, c, d, e }; // input into region0 array
region[1] = new double[3] { f, g, h, };//inout into region1 array
region[2] = new double[2] { l, k }; //input into region3 array

I was wondering if there is anyway, I could loop through and just increase 'a' by one.

System.Console.Write("Enter sales for region1: store 2: "); // asks user to enter
double a = Convert.ToDouble(Console.ReadLine());// creates double variable a

instead of double a, double b, and so on
Thanks,
-sloshy

Edited by sloshymon: n/a

1

Take a look at something like this and add the data directly into the array instead of temporary variables:

using System;

namespace DW_412294_CS_CON
{
   class Program
   {
      const int int_NumRegions = 3;
      static int[] arr_intStoresInRegions = { 5, 3, 2 };

      static double[][] arr_dblRegions = new double[int_NumRegions][];

      static void Main(string[] args)
      {
         for (int i = 0; i < int_NumRegions; i++)
         {
            arr_dblRegions[i] = new double[arr_intStoresInRegions[i]];
            for (int j = 0; j < arr_intStoresInRegions[i]; j++)
            {
               Console.WriteLine("Enter sales for Region {0}: Store{1}:", i+1, j+1); 
               // ADD THE DATA DIRECTLY TO THE ARRAY HERE
            }
         }
      }
   }
}
Comments
thanks,
0

Hmmm some type of loops might help you, as well as exploring other containers (besides arrays)

//Declare the number of stores in each region
            int[] Stores_In_Region = new int[] {5, 3, 2};
            //This will organize the input data into seperate regions
            Dictionary<int, List<double>> InputDataByRegion = new Dictionary<int,List<double>>();
            
            //This temp will hold the input for all the stores in a given region
            List<double> tempRegionInput = new List<double>();
            //Temp value to use with tryparse to get the decimal value entered
            double tempInputDouble = 0;
            //Calculates the total regions based on the length of the Stores_In_Region array
            int TotalRegions = Stores_In_Region.Length;

            //Iterate through every region
            for (int i = 0; i < TotalRegions; i++)
            {
                //Iterate through each store in that region
                for (int ii = 0; ii < Stores_In_Region[i]; ii++)
                {
                    //Get the input, validate it with TryParse()
                    Console.WriteLine(string.Format("Enter sales for region{0}: store{1}: ", i + 1, ii + 1));
                    while (!double.TryParse(Console.ReadLine(), out tempInputDouble))
                        Console.WriteLine("Invalid input, enter a new value: ");
                    //Add this value to the temp input list
                    tempRegionInput.Add(tempInputDouble);
                }
                //Add this list of entries to the current region in the InputByRegion dictionary
                InputDataByRegion[i] = new List<double>(tempRegionInput);
                //Clear the temp list to get new values
                tempRegionInput.Clear();
            }

            Console.WriteLine("Values by region: ");
            //Iterate the dictionary (sorted by region number)
            foreach (int RegionIndex in InputDataByRegion.Keys.OrderBy(x => x))
                //Iterate the list of values at this region and display it
                for (int i = 0; i < InputDataByRegion[RegionIndex].Count; i++)
                    Console.WriteLine("Region{0} Store{1} = {2}", (RegionIndex + 1).ToString(), i.ToString(), InputDataByRegion[RegionIndex][i].ToString());

            Console.WriteLine("Sums by region: ");
            //Iterate the dictionary and display the Sum() of each list in it
            foreach (int RegionIndex in InputDataByRegion.Keys.OrderBy(x => x))
                    Console.WriteLine("Region{0} Sum = {1}", (RegionIndex + 1).ToString(), InputDataByRegion[RegionIndex].Sum().ToString());

            Console.WriteLine("Sums totals: ");
            //using LINQ, this will iterate the dictionary
            Console.WriteLine("Total = {0}", (from a in InputDataByRegion select a.Value.Sum()).Sum().ToString());
0

OK. I'm stealing a concept from @skatamatic (an oversight on my part):

using System;

namespace DW_412294_CS_CON
{
   class Program
   {
      static int[] arr_intStoresInRegions = { 5, 3, 2 };
      static double[][] arr_dblRegions = new double[arr_intStoresInRegions.Length][];
      static void Main(string[] args)
      {
         for (int i = 0; i < arr_intStoresInRegions.Length; i++)
         {
            arr_dblRegions[i] = new double[arr_intStoresInRegions[i]];
            for (int j = 0; j < arr_intStoresInRegions[i]; j++)
            {
               Console.WriteLine("Enter sales for Region {0}: Store{1}:", i+1, j+1); 
               // ADD THE DATA DIRECTLY TO THE ARRAY HERE
            }
         }
      }
   }
}
0

Thanks, the book actually shows them storing value. I have been done with the assignment, just didn't like the big mess at top.
-sloshy

0

I understand. I just wouldn't do it that way when it's not necessary.
The book doesn't use loops?

0

Hmmm some type of loops might help you, as well as exploring other containers (besides arrays)

//Declare the number of stores in each region
            int[] Stores_In_Region = new int[] {5, 3, 2};
            //This will organize the input data into seperate regions
            Dictionary<int, List<double>> InputDataByRegion = new Dictionary<int,List<double>>();
            
            //This temp will hold the input for all the stores in a given region
            List<double> tempRegionInput = new List<double>();
            //Temp value to use with tryparse to get the decimal value entered
            double tempInputDouble = 0;
            //Calculates the total regions based on the length of the Stores_In_Region array
            int TotalRegions = Stores_In_Region.Length;

            //Iterate through every region
            for (int i = 0; i < TotalRegions; i++)
            {
                //Iterate through each store in that region
                for (int ii = 0; ii < Stores_In_Region[i]; ii++)
                {
                    //Get the input, validate it with TryParse()
                    Console.WriteLine(string.Format("Enter sales for region{0}: store{1}: ", i + 1, ii + 1));
                    while (!double.TryParse(Console.ReadLine(), out tempInputDouble))
                        Console.WriteLine("Invalid input, enter a new value: ");
                    //Add this value to the temp input list
                    tempRegionInput.Add(tempInputDouble);
                }
                //Add this list of entries to the current region in the InputByRegion dictionary
                InputDataByRegion[i] = new List<double>(tempRegionInput);
                //Clear the temp list to get new values
                tempRegionInput.Clear();
            }

            Console.WriteLine("Values by region: ");
            //Iterate the dictionary (sorted by region number)
            foreach (int RegionIndex in InputDataByRegion.Keys.OrderBy(x => x))
                //Iterate the list of values at this region and display it
                for (int i = 0; i < InputDataByRegion[RegionIndex].Count; i++)
                    Console.WriteLine("Region{0} Store{1} = {2}", (RegionIndex + 1).ToString(), i.ToString(), InputDataByRegion[RegionIndex][i].ToString());

            Console.WriteLine("Sums by region: ");
            //Iterate the dictionary and display the Sum() of each list in it
            foreach (int RegionIndex in InputDataByRegion.Keys.OrderBy(x => x))
                    Console.WriteLine("Region{0} Sum = {1}", (RegionIndex + 1).ToString(), InputDataByRegion[RegionIndex].Sum().ToString());

            Console.WriteLine("Sums totals: ");
            //using LINQ, this will iterate the dictionary
            Console.WriteLine("Total = {0}", (from a in InputDataByRegion select a.Value.Sum()).Sum().ToString());

I am guessing we are supposed to use Arrays, as the chapter is on arrays, but will read about other containers.

0

no it has the nested for loop.
My original code without a way to exit.

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

namespace Chapter7
{

    public class RegionSales
    {

        static void Main()
        {

            while (true)
            {
                try{
           
                    System.Console.Write("Enter sales for region1: store 1: "); // asks user to enter Store1, region1 sales.
                    double a = Convert.ToDouble(Console.ReadLine());// creates double variable a
                    System.Console.Write("Enter sales for region1: store 2: "); // asks user to enter Store2, region1 sales.
                    double b = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region1: store 3: "); // asks user to enter Store3, region1 sales. 
                    double c = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region1: store 4: "); // asks user to enter Store4, region1 sales.
                    double d = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region1: store 5: "); // asks user to enter Store5, region1 sales.
                    double e = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region2: store 1: "); // asks user to enter Store1, region2 sales.
                    double f = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region2: store 2: "); // asks user to enter Store2, region2 sales.
                    double g = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region2: store 3: "); // asks user to enter Store3, region2 sales.
                    double h = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region3: store 1: "); // asks user to enter Store1, region3 sales.
                    double l = Convert.ToDouble(Console.ReadLine());
                    System.Console.Write("Enter sales for region3: store 2: "); // asks user to enter Store2, region3 sales.
                    double k = Convert.ToDouble(Console.ReadLine());

                    double[][] region = new double[3][];    // scores
                    region[0] = new double[5] { a, b, c, d, e }; // input into region0 array
                    region[1] = new double[3] { f, g, h, };//inout into region1 array
                    region[2] = new double[2] { l, k }; //input into region3 array
                    double sum;       // sum of the sales for each region
                    double total = a + b + c + d + e + f + g + h + l + k; //for calulation of company sales.     {


                    for (int i = 0; i < region.Length; i++) // nested for loop // could have uses a foreach as well.
                    {
                        sum = 0;
                        for (int j = 0; j < region[i].Length; j++)
                            sum += region[i][j];
                        Console.Write
                           ("The average weekly sales for region {0} is: ", i);
                        Console.WriteLine((sum / region[i].Length).ToString("F2"));// write the avrage saqles for each region 



                    }
                    Console.WriteLine("total of all stores: {0}", (total).ToString("F2"));// used to show to total amount made for the company.
                   // Console.ReadKey(); used to check output
                }
                catch (FormatException)// error is throw if anything but an number is entered
                {
                    Console.WriteLine("Please enter numbers only");// prompt to user

                }//end format catch

            }

        }
    }
}

Thanks,
-sloshy

Edited by sloshymon: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.