# Searching for a number less than 5 in an array of 20

Page 1 of 1

## 14 Replies - 2058 Views - Last Post: 04 May 2009 - 07:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=103128&amp;s=d88feb24065eb4e04572bee00d973a26&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Tastybrownies

Reputation: 1
• Posts: 52
• Joined: 13-May 08

# Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 12:42 PM

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, because once less than 5 is figured out I can also know how to do high than average as well.

Thank you
```/*
*  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"));

}
}

```

Is This A Good Question/Topic? 0

## Replies To: Searching for a number less than 5 in an array of 20

### #2 papuccino1

• His name was Robert Paulson.

Reputation: 63
• Posts: 1,121
• Joined: 02-March 08

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 12:57 PM

Hi there!

Use this assuming your items in the array are ints:

```//declare a seperate int array to hold items.
int[] numberslessthanfive;

/*Here you're telling this loop 'hey bro I need you to cycle through all of the items in my array and see if they are less than 5, if you find any please send them to my NumbersLessThanFive office. Thanks.*/
foreach (int i in ArrayNameGoesHere)
{
if (i < 5)
{
numberslessthanfive[i] = i;
}
}

```

I think that would work.

With that code you'll save every item in your array less than five to another array.

Then when you want to display them, just use a foreach loop to shift through every item there.

```/*you walk into your office where are the numbers less than 5 were sent. 'Ok guys, I need you to go to the console. ONE AT A TIME. DON'T PUSH. Thank you.*/
foreach (int i in numberslessthanfive)
{
Console.WriteLine(i);
}

```

This post has been edited by papuccino1: 03 May 2009 - 01:44 PM

### #3 KYA

• Wubba lubba dub dub!

Reputation: 3207
• Posts: 19,239
• Joined: 14-September 07

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 12:58 PM

If I may ask, what is up with your seemingly random and incorrect bracket placement?

### #4 Tastybrownies

Reputation: 1
• Posts: 52
• Joined: 13-May 08

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 01:36 PM

KYA, on 3 May, 2009 - 11:58 AM, said:

If I may ask, what is up with your seemingly random and incorrect bracket placement?

Oh, I was trying another loop here and there, and when I deleted them I must have backspaced or spaced too far.

Thanks for such a quick response guys, I'll give it a try.

### #5 papuccino1

• His name was Robert Paulson.

Reputation: 63
• Posts: 1,121
• Joined: 02-March 08

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 01:41 PM

Did you understand my solution correctly?

### #6 KYA

• Wubba lubba dub dub!

Reputation: 3207
• Posts: 19,239
• Joined: 14-September 07

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 01:43 PM

You don't need an extra array to hold them though. At the very least a total variable and counter:

```for loop
if value < 5
total += array[index]
counter++

average = total/counter

```

### #7 papuccino1

• His name was Robert Paulson.

Reputation: 63
• Posts: 1,121
• Joined: 02-March 08

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 01:45 PM

You do need an array because he wants to display the numbers that are less than 5.

### #8 KYA

• Wubba lubba dub dub!

Reputation: 3207
• Posts: 19,239
• Joined: 14-September 07

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 01:46 PM

If the print has to be afterward, then yes. If not, then print it as soon as it is found.

### #9 papuccino1

• His name was Robert Paulson.

Reputation: 63
• Posts: 1,121
• Joined: 02-March 08

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 01:49 PM

The beautiful side of programming is there are millions of solutions to any single problem. Choose whats best.

### #10 Tastybrownies

Reputation: 1
• Posts: 52
• Joined: 13-May 08

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 01:59 PM

papuccino1, on 3 May, 2009 - 11:57 AM, said:

Hi there!

Use this assuming your items in the array are ints:

```//declare a seperate int array to hold items.
int[] numberslessthanfive;

/*Here you're telling this loop 'hey bro I need you to cycle through all of the items in my array and see if they are less than 5, if you find any please send them to my NumbersLessThanFive office. Thanks.*/
foreach (int i in ArrayNameGoesHere)
{
if (i < 5)
{
numberslessthanfive[i] = i;
}
}

```

I think that would work.

With that code you'll save every item in your array less than five to another array.

Then when you want to display them, just use a foreach loop to shift through every item there.

```/*you walk into your office where are the numbers less than 5 were sent. 'Ok guys, I need you to go to the console. ONE AT A TIME. DON'T PUSH. Thank you.*/
foreach (int i in numberslessthanfive)
{
Console.WriteLine(i);
}

```

Most of what you said worked, the only drawback with it is when i delcare the array it makes me put the [20] memory locations in it. When I do that i get zeros in the places of the numbers that are not less than 5. Also let's say i type the number 1 three times, right now it only displays 1 one time. Somehow, it has to be to a point where i can type 2 or any other number let's say six times and it shows that same number six times.

```/*
*  Author:
*  Purpose:
*
*/
using System;

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

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

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

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

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

}

for (int cntt = 0; cntt < entryPrice.Length; cntt++)
{
totalValues += entryPrice[cntt];
priceAverage = totalValues / 20;
}
///////////making room for the next loop
// int[] numbersLessThanFive;
foreach (int i in entryPrice)
{ if
(i < 5)
{
numbersLessThanFive[i] = i;
}
}
foreach (int i in numbersLessThanFive)
{
Console.WriteLine(i.ToString("C0"));
}

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"));
}
}

```

### #11 KYA

• Wubba lubba dub dub!

Reputation: 3207
• Posts: 19,239
• Joined: 14-September 07

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 02:04 PM

Use a vector for the numbersunderfive[] and push_back() each time you find one, then you won't have unused memory.

### #12 Tastybrownies

Reputation: 1
• Posts: 52
• Joined: 13-May 08

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 02:12 PM

KYA, on 3 May, 2009 - 01:04 PM, said:

Use a vector for the numbersunderfive[] and push_back() each time you find one, then you won't have unused memory.

Unfortunately for me, I haven't learned vectors yet. I guess I will have to research them in due time, i do know what the push_back is though.

### #13 beatles1692

Reputation: 13
• Posts: 62
• Joined: 03-December 08

## Re: Searching for a number less than 5 in an array of 20

Posted 03 May 2009 - 10:14 PM

Hi!
Instead of using an array( with a fixed length) you can use a List so you can add values to it.

```private static int[] GetLessThanFiveNumbers(int[] numbersArray)
{
var listOfLessThanFive=new List<int>();
for(var i=0;i<numbersArray.Length;i++)
if(numbersArray[i]<5)
{
}
return listOfLessThanFive.ToArray();
}

```

I suggest you take a look at Linq that has a great syntax to query nearly all kind of data collections (objects,XML files and databases)
Using linq you could write the above function in just a line like this:

```int[] arrayOfLessThanFive=(from num in numberArrays where num<5 select num).ToArray();

```

### #15 baavgai

• Dreaming Coder

Reputation: 7505
• Posts: 15,553
• Joined: 16-October 07

## Re: Searching for a number less than 5 in an array of 20

Posted 04 May 2009 - 06:37 AM

beatles1692, on 3 May, 2009 - 11:14 PM, said:

Instead of using an array( with a fixed length) you can use a List so you can add values to it.

I'd agree with this, though the assignment may not allow it.

```I suggest you take  a look at Linq that has a great syntax

```

In the name of all this is good, no! Linq is a filthy functional beast that has no business in a nice clean OO language.

A simple array method will also do the trick.
```int[] allNums = { 3, 12, 43, 2, 9, 4 };
int[] arrayOfLessThanFive = Array.FindAll<int>(allNums, delegate(int n) { return n < 5; });

```

Note, the use of delegates in generics is how Linq does it's list comprehension tricks. Might as well just use the methods and not worry about shoving the square peg in the round hole.

This post has been edited by baavgai: 04 May 2009 - 06:38 AM

### #16 Tastybrownies

Reputation: 1
• Posts: 52
• Joined: 13-May 08

## Re: Searching for a number less than 5 in an array of 20

Posted 04 May 2009 - 07:11 PM

I've tried both Baavgai and Beatles 1692 solutions, I got 1692's to work but I don't know if we're supposed to use Linq yet or not; but either way im pretty impressed by it. After changing some things around, I implemented Bavvgai's soltuion but it didn't work so I probably wasn't doing something right because I'm not super experienced in C#.

Thank you everyone for the great responses!

Even if some did not work the way I wanted, they stretched my ideas on what could be possible.

Thanks again guys!