C # -- 2nd week experiment -- Task 8 -- write a console application -- enter 10 numbers to store the array, and calculate the maximum value, minimum value, and average value.

Source: Internet
Author: User

/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: enter 10 to store the data in the array, and calculate the maximum, minimum, and average values.
* Author: Lei hengxin
* Completion date: January 1, September 09, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input description:
* Problem description:
* Program output:
* End the comment in the program Header
*/
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace ConsoleApplication_do_while
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("this is a program that inputs 10 numbers into an array to calculate the maximum, minimum, and average values ");
Double [] c = new double [10];
For (int number = 0; number <10; ++ number)
{
Console. Write ("Enter the number of {0}:", number + 1 );
// String s = Console. ReadLine ();
C [number] = double. Parse (Console. ReadLine ());
}

// Use the foreach statement to output every element in the array cyclically
Console. Write ("You enter 10 :");
Foreach (double var in c)
{
Console. Write (var); // read the elements in the string in sequence
Console. Write ("");
 
}
Console. WriteLine ();
Double min1 = min (c );
Console. WriteLine ("the minimum value of the input 10 is {0}", min1 );
Double max1 = max (c );
Console. WriteLine ("the maximum number of input 10 is: {0}", max1 );
Double average1 = average (c );
Console. WriteLine ("the average value of the input 10 is {0}", average1 );

Console. ReadKey ();
}
Static double max (double [] c)
{
Double max = c [0];
For (int I = 1; I <10; ++ I)
{
If (max <c [I])
Max = c [I];
}
Return max;
}
Static double min (double [] c1)
{
Double min = c1 [0];
For (int I = 1; I <10; ++ I)
{
If (min> c1 [I])
Min = c1 [I];
}
Return min;
}
Static double average (double [] c1)
{
Double average;
Double all = 0;
/* Foreach (char var in c1)
{
All = all + var;
}*/
For (int I = 0; I <10; ++ I)
{
All = all + c1 [I];
}
 
Average = all/c1.Length;
Return average;
}
}
}

 
Running result:
 

 
 
 
Attachment:
The following program is incorrect. It is the maximum ASCII value corresponding to the calculated number.
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace ConsoleApplication_do_while
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("this is a program that inputs 10 numbers into an array to calculate the maximum, minimum, and average values ");
Console. Write ("Please enter 10 numbers (separated by spaces ):");
String s = Console. ReadLine ();
Char [] c = s. ToCharArray ();
Int I = c. Length;
Int j = 0;
Int [] c1 = new int [c. Length];
// Use the foreach statement to output every element in the array cyclically
Foreach (char var in c)
{
// C1 [j] = Convert. ToInt32 (var); // converts the string to an ASCII code.
C1 [j] = var;
++ J; // string reading
}
Console. Write ("You enter 10 :");
Foreach (char var in c1)
{
Console. Write (var); // read the elements in the string in sequence
 
}
Console. WriteLine ();
Int min1 = min (c1 );
Console. WriteLine ("the minimum value of the input 10 is {0}", min1 );
Int max1 = max (c1 );
Console. WriteLine ("the maximum number of input 10 is: {0}", max1 );
Double average1 = average (c1 );
Console. WriteLine ("the average value of the input 10 is {0}", average1 );

Console. ReadKey ();
}
Static int max (int [] c1)
{
Int n = c1.Length;
Int max = c1 [0];
For (int I = 2; I <n; I = I + 2)
{
If (max <c1 [I])
Max = c1 [I];
}
Return max;
}
Static int min (int [] c1)
{
Int n = c1.Length;
Int min = c1 [0];
For (int I = 2; I <n; I = I + 2)
{
If (min> c1 [I])
Min = c1 [I];
}
Return min;
}
Static double average (int [] c1)
{
Double average;
Int all = 0;
/* Foreach (char var in c1)
{
All = all + var;
}*/
For (int I = 0; I <c1.Length; I = I + 2)
{
All = all + c1 [I];
}
 
Average = all/c1.Length;
Return average;
}
}
}

Running result:
 

 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.