-1

I'm absolutely stuck so I would appreciate some guidance into how I can do this.

First off, here is my code so far:

int i;
int x = 0;
int b = 0;
Console.Write("\nHow many stocks to enter price for:\t"); // enter size of array
int size = int.Parse(Console.ReadLine());
double[] arr = new double[size]; // size of array

// Accepting value from user 
for (i = 0; i < size; i++)
{
    Console.Write("\nEnter price for stock #{0}: \t", ++x);
    //Storing value in an array
    arr[i] = Convert.ToDouble(Console.ReadLine());
}

Console.WriteLine("\n\n");

//Printing the value on console
for (i = 0; i < size; i++)
{
    Console.WriteLine("Average Price: " + arr.Average() + " out of {0} stocks", x);
    Console.WriteLine("Minimum Price: " + arr.Min());
    Console.WriteLine("Number of stocks priced between 1.5-35: " + b);
}

Console.ReadLine();

Sorry, I'm not very sure on how to add the colors. Anyway, I am stuck on displaying the number of stocks priced between 1.5 and 35. Shown in this line here: Console.WriteLine("Number of stocks priced between 1.5-35: "+ b);

Basically, it asks for the number of stocks to enter the price for. This will determine the size of the array. Then the user will enter the prices for the stocks x many times as they set it in the beginning. Thus calculating the Average price of a stock, the minimum price then (what i'm stuck on) the number of stocks priced between 1.5 and 35.

Also, i'm sure I could figure this out myself, but for some reason it is displaying the results 2 times each. Not too sure about that as well.

Any help would be appreciated as I've been stuck on this for too damn long.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
nullcat
  • 3
  • 1
  • After reading in the price for the stock, simply do an `if (price >= 1.5 && price <= 35)`, and then increment `b`. Your output prints multiple times because you put it in a loop. Remove the `for()` block around your output and it'll work fine. – Rob Mar 26 '17 at 23:34

2 Answers2

1

Hello @nullcat as suggested by @Rob you have to fix your last loop. Also the variable b is never assigned and therefore you don't have the number of stocks that are priced between 1.5 and 35. I added a for sentence to check that

    for (i = 0; i < size; i++)
    {
        //Check if the stock on index i is between 1.5 and 35 and add 1 to the variable b
        if(arr[i] >=1.5 && arr[i] <=35.0){
             b++
        }
    }
    //Printing the value on console
    Console.WriteLine("Average Price: "+ arr.Average() + " out of {0} stocks", x);
    Console.WriteLine("Minimum Price: "+ arr.Min());
    Console.WriteLine("Number of stocks priced between 1.5-35: "+ b);
    Console.ReadLine();

Please check it out and let me know your comments

3vts
  • 778
  • 1
  • 12
  • 25
1

To provide a slightly shorter alternative solution:

static void Main()
{
    int x = 0;
    Console.Write("\nHow many stocks to enter price for:\t"); 
    int size = int.Parse(Console.ReadLine());
    double[] arr = new double[size]; 

    for (int i = 0; i < size; i++)
    {
        Console.Write($"\nEnter price for stock #{++x}: \t");
        arr[i] = Convert.ToDouble(Console.ReadLine()); //Storing value in an array
    }

    Console.WriteLine($"\r\nAverage Price: {arr.Average()} out of {arr.Count()} stocks");
    Console.WriteLine($"Minimum Price: {arr.Min()}");
    Console.WriteLine($"Number of stocks priced between 1.5-35: " + 
        $"{arr.Where(v => v >= 1.5 && v < 35).Count()}");

    Console.ReadLine();
}
Dec
  • 203
  • 2
  • 11