.net - Sum Average Min Max without using inbuild functions or Methods in C# -


i wrote program calculating sum, average, min & max without inbulit functions or methods. found many techniques , locked in below one. problem is, while doing in normal ways. result came. while transferring windows form, unable output. throws error.

private void button1_click(object sender, eventargs e) { int n = int.parse(textbox7.text); int[] numbers = new int[n]; int sum = 0; float average;  for(int i=0; i<n; i++) { numbers[i] = int.parse(textbox1.text); } array.sort(numbers);  for(int i=0; i<n; i++) { sum += numbers[i]; }  average = ((float)sum / n);  textbox4.text = numbers[0].tostring(); textbox5.text = numbers[-1].tostring(); textbox2.text = sum.tostring(); textbox3.text = average.tostring(); } 

the output should like,

sum : 45 avg : 15 min : 8 max : 10 

i think, problem in code here:

textbox5.text = numbers[-1].tostring(); 

there no entry in array having -1 index.

if want show max value in textbox5, need that:

textbox5.text = numbers[n-1].tostring(); 

if want show min value in textbox5, need that:

textbox5.text = numbers[0].tostring(); 

thanks!


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -