Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to work with one dimension int array in C#

1 Answer

0 votes
using System;
 
public class Program
{
    static void Main(string[] args)
    {
        int[] arr = new int[10];
        int[] arr3, arr4;
        int[] arr7;
        int len;
        Random rnd = new Random();
 
        arr4 = new int[5];
        Console.Write("arr4: ");
        PrintArray(arr4);
 
        for (int i = 0; i < arr4.Length; i++)
            arr4[i] = rnd.Next(1, 10);
 
            Console.Write("arr4: ");
            PrintArray(arr4);
 
            for(int i = 0; i < 10; i++)
                arr[i] = i * i;
 
            Console.Write("1) arr: ");
            PrintArray(arr);
             
            ChangeArray(arr);
            Console.Write("2) arr: ");
            PrintArray(arr);
             
            int[] arr1 = new int[]{1,2,3,4};
            Console.Write("3) arr1: ");
            PrintArray(arr1);
 
            int[] arr2 = {2, 4, 6, 8};
            Console.Write("4) arr2: ");
            PrintArray(arr2);
 
            arr3 = arr2; // shallow copy - point at the same location in memory
            // arr3 and arr2 are same array different name
            Console.Write("5) arr3: ");
            PrintArray(arr3);
 
            arr3[1] = 900; // arr2[1] will also be 900 (point at the same location in memory)
            Console.Write("6) arr3: ");
            PrintArray(arr3);
            Console.Write("7) arr2: ");
            PrintArray(arr2);
 
            int[] RetArr;
            Console.Write("8) RetArr: ");
            RetArr = ReturnArray();
            PrintArray(RetArr);
 
            arr2[0] = 1200; // arr3[0] will also be 1200 (point at the same location in memory)
            Console.Write("9) arr2: ");
            PrintArray(arr2);
            Console.Write("10) arr3: ");
            PrintArray(arr3);
 
            Console.WriteLine("11) arr size: {0}", arr.Length);
 
            Console.Write("Enter array len: ");
            len = int.Parse(Console.ReadLine());
            arr7 = new int[len]; // dynamic array size
            InitArray(arr7);
            PrintArray(arr7);
        }
 
    public static void InitArray(int[] arr)  {
        Random rnd = new Random();
        for (int i = 0; i < arr.Length; i++) {
            arr[i] = rnd.Next(1, 10);
        }
    }
 
    public static void ChangeArray(int[] arr) {
        for (int i = 0; i < 10; i++) {
            arr[i] = i;
        }
    }
 
    public static int[] ReturnArray() {
        int[] arr = {100, 200, 300};
        
        return arr;
    }
 
    public static void PrintArray(int[] a) {
        foreach (int element in a) {
            Console.Write(" {0} ", element);
        }
        Console.WriteLine();
    }
}


 
/*
run:

arr4:  0  0  0  0  0 
arr4:  6  9  5  7  5 
1) arr:  0  1  4  9  16  25  36  49  64  81 
2) arr:  0  1  2  3  4  5  6  7  8  9 
3) arr1:  1  2  3  4 
4) arr2:  2  4  6  8 
5) arr3:  2  4  6  8 
6) arr3:  2  900  6  8 
7) arr2:  2  900  6  8 
8) RetArr:  100  200  300 
9) arr2:  1200  900  6  8 
10) arr3:  1200  900  6  8 
11) arr size: 10
Enter array len: 4
 7  1  8  9 
     
*/



answered Jun 1, 2014 by avibootz
edited Mar 4, 2025 by avibootz
...