C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Displaying values less than 5 in an Array Posted by tastybrownies on 3 May 2009 at 12:39 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.

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);
            entryPrice[i] = Convert.ToDouble(Console.ReadLine());
            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"));
        Console.Read();

    }
}

Report
Re: Displaying values less than 5 in an Array Posted by losthelper on 3 May 2009 at 6:27 PM
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
Report
Re: Displaying values less than 5 in an Array Posted by Psightoplazm on 4 May 2009 at 7:49 AM
If you've ever used generics before they can actually take care of your counts and averages for you

        public static void Main()
        {
            var values = new List<double>();

            Console.WriteLine("Please enter 20 values:");
            while (values.Count < 20)
                try { values.Add(double.Parse(Console.ReadLine())); }
                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());

            Console.ReadKey();
        }

></\/~Psightoplasm`~



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.