Solved

form displaying input values and percetages

Posted on 2008-10-04
3
226 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 50 total points
ID: 22642551
100 / Total * Value
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 75 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 75 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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