C# Lesson 10

Lesson 10 in a our series about learning C# teaches you about working with arrays.

In this lesson we are going to learn about Arrays.
Afterwards you can continue to the next lesson!
Or go back and review the previous lesson.
We also have the source code on GitHub.


using System;

namespace Lesson10
{
    class Arrays
    {
        static void Main(string[] args)
        {
            //int[] myFirstArray;
            //myFirstArray = new int[5];

            //myFirstArray[0] = 10; // 1
            //myFirstArray[1] = 20; // 2
            //myFirstArray[2] = 30; // 3
            //myFirstArray[3] = 40; // 4
            //myFirstArray[4] = 50; // 5

            int[] myFirstArray = new int[] { 10, 20, 30, 40, 50 };

            Console.Write("Using For: ");
            for (int i = 0; i < myFirstArray.Length; i++)
            {
                Console.Write(myFirstArray[i] + " ");
            }

            Console.WriteLine();
            Console.Write("Using While: ");
            int j = 0;
            while (j < myFirstArray.Length)
            {
                Console.Write(myFirstArray[j] + " ");

                j++;
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();


            Console.Write("Enter the length of the array: ");
            int arrayLength = Convert.ToInt32(Console.ReadLine());

            string[] userArray = new string[arrayLength];

            for (int x = 0; x < userArray.Length; x++)
            {
                Console.Write($"Enter value {x}: ");
                userArray[x] = Console.ReadLine();
            }

            Console.WriteLine();
            Console.Write("Entered values: ");

            for (int x = 0; x < userArray.Length; x++)
            {
                Console.Write(userArray[x] + " ");
            }



        }
    }
}


/*
* TASK1:
*   Create the following int array and name it "shiftArray":
*       {91, 92, 93, 94, 95}
*       
*   Make a program that shifts every value in the "shiftArray" array right by
*   N, a value entered by the user.
*   
*   The last value in the array becomes the first when it gets shifted by 1.
*   
*   
*   Example Output 1:
*       Array Values: 91 92 93 94 95
*       Shift by: 1
*       
*       New Positions: 95 91 92 93 94
*   
*   Example Output 2:
*       Array Values: 91 92 93 94 95
*       Shift by: 2
*       
*       New Positions: 94 95 91 92 93
*       
*   Example Output 3:
*       Array Values: 91 92 93 94 95
*       Shift by: 5
*       
*       New Positions: 91 92 93 94 95
*       
*   
*/

/*
* TASK2:
*   Make a program asks the user to enter 5 double values.
*   Put the values into a double array.
*   
*   After all the values have been entered, 
*   calculate the average of all the numbers and display it to the user.
*   
*   Then ask if the user wants to run the program again. If they enter "y" run the program again.
*   
*   Example Output:
*       Enter 5 doubles:
*       Value 1: 4
*       Value 2: 4.5
*       Value 3: 5
*       Value 4: 5.5
*       Value 5: 6
*       
*       The average is 5
*       
*       Run again? (y/n)
*   
*/

using System;

namespace Lesson10Task
{
    class ArraysTask
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Recommended posts

We have similar articles. Keep reading!

C# Lesson 12

Lesson 12 in a our series about learning C# teaches you about working with methods.

C# Lesson 2

Learn how to read input from the user in the second lesson from our series on learning C# for beginners.

C# Project 1

Test out your knowledge on basic C# programming with a challenging project doing conversions. Covers lessons 1-6 from the beginner series.

C# Lesson 7

You're familiar with "while" loops in C#, but but it's time to learn about "for" loops. Let's get loopy in the 7th lesson in our series about learning C#.

Comments

Log in or sign up to leave a comment.