Displaying values less than 5 in an Array - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Displaying values less than 5 in an Array

Posts: 6Member
in C#
Hello all, I have been going at this for hours now and still can't get it.

I have created an array where the user can input 20 values, upon that the total is calculated, average, enteries less than 5.

I have the other two done, but the less than five seems to be giving me trouble right now. I have it so i can count how many values are less than 5, i just dont know how to display them seperately right now.

Any help would be greatly appreciated.

Thank you
[code]/*
* Author:
* Purpose:
*
*/
using System;

class Prices
{
public static void Main()
{
// declaring necessary variables for the program.

double totalValues = 0;
int x;
int y;
double lessThanFive = 0;
double priceAverage = 0;
double higherThanAverage = 0;
double[] entryPrice = new double[20];
/////////// this was an attempt to try a parallel array at some point.
double[] lowLimit = new double[20];

double userInput;
int count = 0;
Console.WriteLine("In any order, enter 20 price values.");

for (int i = 0; i < entryPrice.Length; i++)

{
count += 1;
Console.Write("Enter Price {0}:", count);
userInput = entryPrice[i];

}

{
totalValues = 0;
for (int cntt = 0; cntt < entryPrice.Length; cntt++)

totalValues += entryPrice[cntt];
priceAverage = totalValues / 20;
}
///////////making room for the next loop

{
for (y = 0; y < entryPrice.Length; y++)
if (entryPrice[y] < 5)
lessThanFive++;
}

Console.WriteLine("The sum of the values is: " + totalValues.ToString("C") + ".");
Console.WriteLine();
Console.WriteLine("List of prices less than \$5.00. " + lessThanFive.ToString("C") + ".");
Console.WriteLine();
Console.WriteLine("The price average is: " + priceAverage.ToString("C") + ".");
Console.WriteLine();
Console.WriteLine("List of prices high than the average: " + higherThanAverage.ToString("C"));

}
}
[/code]

• Posts: 35Member
2 options

create a string that holds the values less than 5

or

create a temp array to hold the values less than 5

to display a conditional statement where it should be display code wise

I give you options you do the coding
I am in relearning process, sorry if i make a mistake
• Posts: 332Member ✭✭
If you've ever used generics before they can actually take care of your counts and averages for you

[code]
public static void Main()
{
var values = new List();

while (values.Count < 20)
catch (Exception) { Console.WriteLine("Invalid input."); }

Console.Clear();
Console.WriteLine("Your total value is: " + values.Sum(d => d));
Console.WriteLine("Your average value is: " + values.Average(d => d));
Console.WriteLine("The total values under 5: " + values.Sum(d => (d < 5) ? 1 : 0));
Console.WriteLine ("Values under 5:");
foreach (var val in values)
if (val < 5)
Console.WriteLine(val.ToString());

}
[/code]
><//~Psightoplasm`~