← C# from Scratch
Lesson 21 of 39 · 10 XP
Arrays
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
An array is a fixed-length set of items of the same type:
int[] scores = { 10, 25, 7 };
string[] names = new string[3]; // three nulls
scores[0] = 15; // indices start at 0
Console.WriteLine(scores.Length); // 3
foreach (int s in scores) Console.WriteLine(s);
Going out of bounds throws an IndexOutOfRangeException (rather than silently corrupting memory, as in C).
Useful things from System:
Array.Sort(scores); // ascending, in place
Array.Reverse(scores);
int max = scores.Max(); // needs using System.Linq;
int[,] grid = new int[3, 4]; // two-dimensional: grid[y, x]
The length can't be changed — for that there's List<T> (using System.Collections.Generic;).
Task. The first line is n, the second is n whole numbers separated by spaces. Print them in ascending order,
separated by spaces, then on a new line their average with one decimal place.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.