ok so im making this console app in which I have to allow the user to enter any number of values then display orignal value , and then the percentage of the original value to the total. My question is how do I initialize my memory locations correctly for a correct report to display ?
also is my method of asking for more values correct?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class intReport
{
static void Main()
{
//declare memory locations
//
//user provided value
int userInteger = 1;
int userTotal= 1; //totals the value user provides
double percentage = (userInteger / userInteger) * 100;
string userInput; //read user input
bool dataOutput = true;
DisplayInstructions();
while (dataOutput)
{
userInteger = GetInput();
DisplayReport(userInteger, userTotal, percentage);
Console.WriteLine("Would you like to enter additional values?");
userInput = Console.ReadLine();
if (userInput == "y" || userInput == "Y")
{
Console.Clear();
}
else
dataOutput = false;
} // end of while loop
} // end of main
public static void DisplayInstructions()
{
Console.Write("In this program you will be allowed to enter any number of values the program" +
"\n will then total the values and display a report which will show the original"+
"\nentered value as a percentage of the total" +
"\n\n Please press any key to continue.") ;
Console.ReadKey();
Console.Clear();
} //end of display instructions
public static int GetInput()
{
int input;
Console.WriteLine("You will be asked to enter a integer.");
Console.WriteLine(" ");
Console.Write("Please enter a value: ");
int.TryParse(Console.ReadLine(), out input);
Console.Clear();
return input;
} // end of GetInput method
public static double Percentage(int userInteger, int userTotal, double percentage)
{
//int userInteger;
//int userTotal;
//double percentage;
Console.WriteLine("Original Value = {0} ", userInteger);
Console.WriteLine("Org. Value Percentage to the total = {0} ", percentage);
return percentage;
} //end of percentage method
public static void DisplayReport(int userInteger, int userTotal, double percentage)
{
int i;
Console.Write("\n");
Console.WriteLine("Your Results: ");
Console.WriteLine("------------- ");
Console.WriteLine("Original Value: {0}", userInteger);
Console.WriteLine("Percentage: {0}", percentage);
} // emd of display report
} // end of class
} // end of namespaceOn line 16 userTotal should start with being 0, not 1.
On line 17 Percentage will always equal 100.
Can you use arrays?
On line 16 userTotal should start with being 0, not 1. On line 17 Percentage will always equal 100. Can you use arrays?
Yes i can use arrays just didnt know how to implement them in this case for such a simple problem...guess I should start doing that
Ok so I added a array and added some code to my Percentage method is the correct method ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class intReport
{
static void Main()
{
//declare memory locations
//
//user provided value
int userInteger = 0;
int userTotal= 0; //totals the value user provides
//percentage always 100
double percentage = 100;
string userInput; //read user input
double avg;
bool dataOutput = true;
//start array
int[] integers = new int[10];
//sum the values
for (int i = 0; i < integers.Length; i++)
{
userTotal += integers[i];
}
avg = userTotal / integers.Length;
//Console.WriteLine(DisplayReport(integers));
DisplayInstructions();
while (dataOutput)
{
userInteger = GetInput();
DisplayReport(userInteger, userTotal, percentage);
Console.WriteLine("Would you like to enter additional values?");
userInput = Console.ReadLine();
if (userInput == "y" || userInput == "Y")
{
Console.Clear();
}
else
dataOutput = false;
} // end of while loop
} // end of main
public static void DisplayInstructions()
{
Console.Write("In this program you will be allowed to enter any number of values the program" +
"\n will then total the values and display a report which will show the original"+
"\nentered value as a percentage of the total" +
"\n\n Please press any key to continue.") ;
Console.ReadKey();
Console.Clear();
} //end of display instructions
public static int GetInput()
{
int input;
Console.WriteLine("You will be asked to enter a integer.");
Console.WriteLine(" ");
Console.Write("Please enter a value: ");
int.TryParse(Console.ReadLine(), out input);
Console.Clear();
return input;
} // end of GetInput method
public static double Percentage(int userInteger, int userTotal, int integers, double percentage)
{
int integers;
for (int i = 0; i < integers.Length; i++)
{
userTotal += integers[i];
}
avg = userTotal / integers.Length;
Console.WriteLine("Original Value = {0} ", userInteger);
Console.WriteLine("Org. Value Percentage to the total = {0} ", percentage);
return percentage;
} //end of percentage method
public static void DisplayReport(int userInteger, int userTotal, double percentage)
{
int i;
Console.Write("\n");
Console.WriteLine("Your Results: ");
Console.WriteLine("------------- ");
Console.WriteLine("Original Value: {0}", userInteger);
Console.WriteLine("Percentage: {0}", percentage);
} // emd of display report
} // end of class
} // end of namespaceAre you allowed to use LINQ to objects and lists? If so, these have some nice functions built in for summation.
namespace ConsoleApplication1
{
class intReport
{
static List<int> myInts = new List<int>();
static void Main()
{
DisplayInstructions();
Console.Write("Would you like to enter a value?");
while (char.ToLower(Console.ReadKey(true).KeyChar) == 'y')
{
myInts.Add(GetInput());
Console.Write("Would you like to enter additional values?");
}
if (myInts.Count != 0)
DisplayReport();
else
Console.WriteLine(Environment.NewLine + "Program exitted with no data entered");
Console.ReadLine();
}
public static void DisplayInstructions()
{
Console.Write("In this program you will be allowed to enter any number of values the program" +
"\n will then total the values and display a report which will show the original" +
"\nentered value as a percentage of the total" +
"\n\n Please press any key to continue.");
Console.ReadKey();
Console.Clear();
}
public static int GetInput()
{
int input = 0;
Console.Write(Environment.NewLine + "Please enter a value: ");
while (!int.TryParse(Console.ReadLine(), out input))
Console.Write(Environment.NewLine + "Invalid value, enter a new value: ");
Console.Clear();
return input;
}
public static void DisplayReport()
{
int tTotal = myInts.Sum();
double dAvg = (double)tTotal / myInts.Count;
int iCount = 1;
Console.Write("\n");
Console.WriteLine("Your Results: ");
Console.WriteLine("------------- ");
myInts.ForEach(cur => Console.WriteLine("Entry {0} -> Value = {1} , Percent of Total = {2}% ", (iCount++).ToString(), cur.ToString(), (((double)cur / tTotal) * 100).ToString()));
Console.WriteLine("-------------");
Console.WriteLine("Average = {0} ", dAvg);
} }
}
If this is for school and they haven't taught you about either LINQ or Lists<> then I would sugguestnot handing this in. Rather, try learning from it and post any questions you might have.
yes its for school practice. Thanks anyway for providing a solution. My method is I strip down responses like these and understand line by line