[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

form displaying input values and percetages

Posted on 2008-10-04
3
Medium Priority
?
231 Views
Last Modified: 2013-12-17
I have to create a program that accepts intergers and then displays a message box showing the original values, the total, and the percentage each value contibutes to the total. My question is how do I calculate the percentage each value contibutes to the total?
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
 
namespace Chp7Ex2
{
    class Program
    {
        static void Main(string[] args)
        {
            string inValue;
            int total = 0;
            Console.WriteLine("How many values will be entered: ");
            string sSize = Console.ReadLine();
            int arraySize = Convert.ToInt32(sSize);
            int [] input = new int[arraySize];
 
            for (int i=0; i < arraySize; i++)
            {
                Console.Write("Enter number {0}: ", i + 1);
                inValue = Console.ReadLine( );
                input[i] = Convert.ToInt32(inValue);
             }
 
            for (int i = 0; i < arraySize; i++)
            {
               total += input[i];
            }
            MessageBox.Show(total);
        }
    }
}

Open in new window

0
Comment
Question by:98fatboyrider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Assisted Solution

by:EricTViking
EricTViking earned 200 total points
ID: 22642551
100 / Total * Value
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 300 total points
ID: 22642678
you can show you data as:

// after calculating total
string message = "";
for (int i = 0; i < arraySize; i++)
{
       message += string.Format("{0} ({1}%)\r\n", input[i], input[i] * 100 / total);
}
message += string.Format("Total: {0}", total);
MessageBox.Show(message);
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 300 total points
ID: 22643298
Jaime's solution should work but if there are 3 values 12,12, 12 the % will just show 33% each time as round is not implemented where as it should show 33.33%

i have extended Jaimes solution further to take care of that too
			string inValue;
			int total = 0;
			Console.WriteLine ("How many values will be entered: ");
			string sSize = Console.ReadLine ();
			int arraySize = Convert.ToInt32 (sSize);
			int[] input = new int[arraySize];
 
			for (int i = 0; i < arraySize; i++)
			{
				Console.Write ("Enter number {0}: ", i + 1);
				inValue = Console.ReadLine ();
				input[i] = Convert.ToInt32 (inValue);
			}
 
			for (int i = 0; i < arraySize; i++)
			{
				total += input[i];
			}
			string message = "\r\n\r\n";
			for (int i = 0; i < arraySize; i++)
			{
				message += string.Format ("Input value no {0} : {1} having % contribution ({1}%)\r\n", i, input[i], Math.Round(input[i] * 100.00 / total, 2));
			}
			message += string.Format ("\r\nTotal: {0}", total);
			Console.WriteLine (message);
			
			
			Console.ReadLine();

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question