SlideShare a Scribd company logo
1 of 11
Download to read offline
Renas R. Rekany Object-Oriented-Programming
1
Arrays (declaration 1_D, 2_D, initialization)
1- Array:
Sometimes, you need to declare multiple variable of same data type. It is complex
and time consuming process. Just for an example, you have to declare 100 integer
type of variable then what would you do? Will you declare variable as follow:
int num1,num2,num3,num4,num5,num6,num7,num8.... num100;
It is not a suitable way to declare multiple variable of same data type. To overcome
this situation, array came alive. An array is a collection of same data type. If you
have to declare 100 variable of same data type in C#, then you can declare and
initialize an array as follow.
int[] num = new int[100];
2- Structure of C# array:
To understand concept of array, consider the following example. What happens in
memory when we declare an integer type array?
int[] num = new int[6];
Num Array:
Renas R. Rekany Object-Oriented-Programming
2
3- Declaration and Initialization of Array:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] array=new int [5];
array[0] = 3;
array[1] = 6;
array[2] = 2;
array[3] = 7;
array[4] = 9;
for (int i = 0; i < 5; i++)
{
textBox1.Text = textBox1.Text + array[i];
textBox1.AppendText(Environment.NewLine);
}
}
}
}
4- Storing value directly in C# program:
In your program, you can directly store value in array. You can store value in array
by two ways.
i. Inline:
int[] arr =new int[5] { 3, 6, 2, 7, 9 };
ii. Index:
int[] arr = new int[5]
arr[0] = 3;
arr[1] = 6;
arr[2] = 2;
arr[3] = 7;
arr[4] = 9;
Renas R. Rekany Object-Oriented-Programming
3
iii. Users input:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] array2 = new int[10];
for (int i = 0; i < 10; i++)
{
array2[i] = Convert.ToInt32(Interaction.InputBox("Enter The
array elements", "Input Box", "Write Here", 75, 75));
textBox1.Text = textBox1.Text + array2[i];
textBox1.AppendText(Environment.NewLine);
}
}
}
}
Hint: To define InputBox, you have to Add reference from .Net-> Microsoft.Visual Basic,
then write it's library.
Renas R. Rekany Object-Oriented-Programming
4
5- Accessing value from array:
You can access array’s value by its index position. Each index position in array
refers to a memory address in which your values are saved.
int[] arr=new int[6];
int num1, num2;
num1 = arr[1] * arr[3];
It is same as:
num1 = 23 * 9; Because, arr[1] refers to the value 23 and arr[3] refers to the value 9.
If you are required to access entire value of an array one after another, then you can
use loop constructs to iterate through array.
Renas R. Rekany Object-Oriented-Programming
5
Example3:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] id = new int[3];
string[] name = new string[3];
int i, j = 0;
string f;
for (i = 0; i < 3; i++)
{
id[i] = Convert.ToInt32(Interaction.InputBox("Enter the id",
"InputBox", "Enter Here", 75, 75));
name[i] = Interaction.InputBox("Enter the name", "InputBox",
"Enter Here", 75, 75);
}
f = Interaction.InputBox("Enter the name to find", "InputBox",
"Enter Here", 75, 75);
for (i = 0; i < 3; i++)
{
if (name[i] == f)
{
MessageBox.Show(Convert.ToString(id[i]), "The Id is:");
j++;
}
}
if (j == 0)
MessageBox.Show("Not Found");
}
}
}
Hint: To define InputBox, you have to Add reference from .Net-> Microsoft. Visual Basic,
then write it's library.
Renas R. Rekany Object-Oriented-Programming
6
6- Array dimensions:
A. One dimensional:
The one dimensional array or single dimensional array in C# is the simplest type of
array that contains only one row for storing data. It has single set of square bracket
(“[]”). To declare single dimensional array in C#, you can write the following code.
Example4:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] books = new string[5] { "C#", "VB", "JAVA", "C++", "MATLAB"
};
textBox1.Text = "First Second Third Four Five";
textBox1.AppendText(Environment.NewLine);
textBox1.Text = textBox1.Text + "_______________________________";
textBox1.AppendText(Environment.NewLine);
for (int i = 0; i < 5; i++)
{
textBox1.Text = textBox1.Text + books[i] + " ";
}
}
}
}
Renas R. Rekany Object-Oriented-Programming
7
OUTPUT:
B. Multi-dimensional array:
The multi-dimensional array in C# is such type of array that contains more than one
row to store data on it. The multi-dimensional array is also known as rectangular array
in c sharp because it has same length of each row. It can be two dimensional array or
three dimensional array or more. It contains more than one coma (,) within single
rectangular brackets (“[ , , ,]”). To storing and accessing the elements from
multidimensional array, you need to use nested loop in program. The following
example will help you to figure out the concept of multidimensional array.
Example5:
Renas R. Rekany Object-Oriented-Programming
8
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[,] books = new string[3,3]{ { "C#", "VB", "JAVA"},{".NET",
"C++", "DB" },{"SQL","HTML","XML"}};
textBox1.Text = "First Second Third ";
textBox1.AppendText(Environment.NewLine);
textBox1.Text = textBox1.Text + "___________________________";
textBox1.AppendText(Environment.NewLine);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
textBox1.Text = textBox1.Text + books[i, j] + " ";
}
textBox1.AppendText(Environment.NewLine);
}
}
}
}
OUTPUT:
Renas R. Rekany Object-Oriented-Programming
9
7- Passing array:
An array can also be passed to method as argument or parameter. A method process
the array and returns output. Passing array as parameter in C# is pretty easy as passing
other value as parameter. Just create a function that accepts array as argument and then
process them. The following demonstration will help you to understand how to pass
array as argument in C# programming.
Example4:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void printarray(int[] print)
{
int i;
for (i = 0; i < 5; i++)
{
textBox1.Text = textBox1.Text + print[i];
textBox1.AppendText(Environment.NewLine);
}
}
private void button1_Click(object sender, EventArgs e)
{
int[] num = new int[5] { 2, 4, 6, 8, 10 };
printarray(num);
}
}
}
Renas R. Rekany Object-Oriented-Programming
10
8- Most common properties of Array class
Properties Explanation Example
Length Returns the length of array. Returns integer value. int i = arr1.Length;
Rank
Returns total number of items in all the dimension. Returns
integer value.
int i = arr1.Rank;
IsFixedSize
Check whether array is fixed size or not. Returns Boolean
value
bool i = arr.IsFixedSize;
IsReadOnly
Check whether array is ReadOnly or not. Returns Boolean
value
bool k =
arr1.IsReadOnly;
H.M) Apply these properties in arrays (Max, Min, Sum, Average, First, Last).
9- Most common functions of Array class:
Function Explanation Example
Sort Sort an array Array.Sort(arr);
Clear Clear an array by removing all the items Array.Clear(arr, 0, 3);
GetLength Returns the number of elements arr.GetLength(0);
GetValue Returns the value of specified items arr.GetValue(2);
IndexOf Returns the index position of value Array.IndexOf(arr,45);
Copy Copy array elements to another elements Array.Copy(arr1,arr1,3);
Example5:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void arr_mul(int[] array1)
{
int i;
double mul = 1;
for (i = 0; i < 5; i++)
{
textBox8.Text = textBox8.Text + array1[i];
mul *= array1[i];
}
Renas R. Rekany Object-Oriented-Programming
11
for (i = 0; i < 5; i++)
{
textBox9.Text = textBox9.Text+array1[i].ToString();
}
Array.Sort(array1);
for(i=0;i<5;i++)
{
textBox6.Text = textBox6.Text + array1[i].ToString();
}
textBox11.Text = mul.ToString() ;
}
private void button1_Click(object sender, EventArgs e)
{
int[] arr1 = new int[5] { 7, 3, 12, 8, 11 };
int[] arr2 = new int[5];
int len, r,gl,gt;
bool fixedsize, read_only;
Array.Copy(arr1, arr2, arr1.Length);
len = arr1.Length;
textBox1.Text = len.ToString();
r=arr1.Rank;
textBox2.Text = r.ToString();
fixedsize = arr1.IsFixedSize;
textBox3.Text = fixedsize.ToString();
read_only = arr1.IsReadOnly;
textBox4.Text = read_only.ToString();
gl=arr1.GetLength(0);
textBox5.Text = gl.ToString();
textBox10.Text = arr1.Sum().ToString();
gt=Convert.ToInt32(textBox7.Text);
textBox8.Text = arr1.GetValue(gt).ToString();
arr_mul(arr1);
}
}
}
Q) Write a program in C# to find the following in array, (Copy, Reverse, Find, Resize).

More Related Content

What's hot

Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
Vince Vo
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
Princess Sam
 

What's hot (20)

Generics
GenericsGenerics
Generics
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
C arrays
C arraysC arrays
C arrays
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Intake 38 5 1
Intake 38 5 1Intake 38 5 1
Intake 38 5 1
 
Intake 38 5
Intake 38 5Intake 38 5
Intake 38 5
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
C# Generics
C# GenericsC# Generics
C# Generics
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Java arrays
Java   arraysJava   arrays
Java arrays
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Arrays
ArraysArrays
Arrays
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 

Similar to C# p9

Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
sotlsoc
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
ShahinAhmed49
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 

Similar to C# p9 (20)

Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Arrays
ArraysArrays
Arrays
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
 
Collection
CollectionCollection
Collection
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.ppt
 
Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 

More from Renas Rekany

Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 

More from Renas Rekany (20)

decision making
decision makingdecision making
decision making
 
Artificial Neural Network
Artificial Neural NetworkArtificial Neural Network
Artificial Neural Network
 
AI heuristic search
AI heuristic searchAI heuristic search
AI heuristic search
 
AI local search
AI local searchAI local search
AI local search
 
AI simple search strategies
AI simple search strategiesAI simple search strategies
AI simple search strategies
 
C# p8
C# p8C# p8
C# p8
 
C# p7
C# p7C# p7
C# p7
 
C# p6
C# p6C# p6
C# p6
 
C# p5
C# p5C# p5
C# p5
 
C# p4
C# p4C# p4
C# p4
 
C# p3
C# p3C# p3
C# p3
 
C# p2
C# p2C# p2
C# p2
 
C# p1
C# p1C# p1
C# p1
 
C# with Renas
C# with RenasC# with Renas
C# with Renas
 
Object oriented programming inheritance
Object oriented programming inheritanceObject oriented programming inheritance
Object oriented programming inheritance
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
 
Renas Rajab Asaad
Renas Rajab AsaadRenas Rajab Asaad
Renas Rajab Asaad
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

C# p9

  • 1. Renas R. Rekany Object-Oriented-Programming 1 Arrays (declaration 1_D, 2_D, initialization) 1- Array: Sometimes, you need to declare multiple variable of same data type. It is complex and time consuming process. Just for an example, you have to declare 100 integer type of variable then what would you do? Will you declare variable as follow: int num1,num2,num3,num4,num5,num6,num7,num8.... num100; It is not a suitable way to declare multiple variable of same data type. To overcome this situation, array came alive. An array is a collection of same data type. If you have to declare 100 variable of same data type in C#, then you can declare and initialize an array as follow. int[] num = new int[100]; 2- Structure of C# array: To understand concept of array, consider the following example. What happens in memory when we declare an integer type array? int[] num = new int[6]; Num Array:
  • 2. Renas R. Rekany Object-Oriented-Programming 2 3- Declaration and Initialization of Array: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int[] array=new int [5]; array[0] = 3; array[1] = 6; array[2] = 2; array[3] = 7; array[4] = 9; for (int i = 0; i < 5; i++) { textBox1.Text = textBox1.Text + array[i]; textBox1.AppendText(Environment.NewLine); } } } } 4- Storing value directly in C# program: In your program, you can directly store value in array. You can store value in array by two ways. i. Inline: int[] arr =new int[5] { 3, 6, 2, 7, 9 }; ii. Index: int[] arr = new int[5] arr[0] = 3; arr[1] = 6; arr[2] = 2; arr[3] = 7; arr[4] = 9;
  • 3. Renas R. Rekany Object-Oriented-Programming 3 iii. Users input: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.VisualBasic; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int[] array2 = new int[10]; for (int i = 0; i < 10; i++) { array2[i] = Convert.ToInt32(Interaction.InputBox("Enter The array elements", "Input Box", "Write Here", 75, 75)); textBox1.Text = textBox1.Text + array2[i]; textBox1.AppendText(Environment.NewLine); } } } } Hint: To define InputBox, you have to Add reference from .Net-> Microsoft.Visual Basic, then write it's library.
  • 4. Renas R. Rekany Object-Oriented-Programming 4 5- Accessing value from array: You can access array’s value by its index position. Each index position in array refers to a memory address in which your values are saved. int[] arr=new int[6]; int num1, num2; num1 = arr[1] * arr[3]; It is same as: num1 = 23 * 9; Because, arr[1] refers to the value 23 and arr[3] refers to the value 9. If you are required to access entire value of an array one after another, then you can use loop constructs to iterate through array.
  • 5. Renas R. Rekany Object-Oriented-Programming 5 Example3: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.VisualBasic; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int[] id = new int[3]; string[] name = new string[3]; int i, j = 0; string f; for (i = 0; i < 3; i++) { id[i] = Convert.ToInt32(Interaction.InputBox("Enter the id", "InputBox", "Enter Here", 75, 75)); name[i] = Interaction.InputBox("Enter the name", "InputBox", "Enter Here", 75, 75); } f = Interaction.InputBox("Enter the name to find", "InputBox", "Enter Here", 75, 75); for (i = 0; i < 3; i++) { if (name[i] == f) { MessageBox.Show(Convert.ToString(id[i]), "The Id is:"); j++; } } if (j == 0) MessageBox.Show("Not Found"); } } } Hint: To define InputBox, you have to Add reference from .Net-> Microsoft. Visual Basic, then write it's library.
  • 6. Renas R. Rekany Object-Oriented-Programming 6 6- Array dimensions: A. One dimensional: The one dimensional array or single dimensional array in C# is the simplest type of array that contains only one row for storing data. It has single set of square bracket (“[]”). To declare single dimensional array in C#, you can write the following code. Example4: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string[] books = new string[5] { "C#", "VB", "JAVA", "C++", "MATLAB" }; textBox1.Text = "First Second Third Four Five"; textBox1.AppendText(Environment.NewLine); textBox1.Text = textBox1.Text + "_______________________________"; textBox1.AppendText(Environment.NewLine); for (int i = 0; i < 5; i++) { textBox1.Text = textBox1.Text + books[i] + " "; } } } }
  • 7. Renas R. Rekany Object-Oriented-Programming 7 OUTPUT: B. Multi-dimensional array: The multi-dimensional array in C# is such type of array that contains more than one row to store data on it. The multi-dimensional array is also known as rectangular array in c sharp because it has same length of each row. It can be two dimensional array or three dimensional array or more. It contains more than one coma (,) within single rectangular brackets (“[ , , ,]”). To storing and accessing the elements from multidimensional array, you need to use nested loop in program. The following example will help you to figure out the concept of multidimensional array. Example5:
  • 8. Renas R. Rekany Object-Oriented-Programming 8 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string[,] books = new string[3,3]{ { "C#", "VB", "JAVA"},{".NET", "C++", "DB" },{"SQL","HTML","XML"}}; textBox1.Text = "First Second Third "; textBox1.AppendText(Environment.NewLine); textBox1.Text = textBox1.Text + "___________________________"; textBox1.AppendText(Environment.NewLine); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { textBox1.Text = textBox1.Text + books[i, j] + " "; } textBox1.AppendText(Environment.NewLine); } } } } OUTPUT:
  • 9. Renas R. Rekany Object-Oriented-Programming 9 7- Passing array: An array can also be passed to method as argument or parameter. A method process the array and returns output. Passing array as parameter in C# is pretty easy as passing other value as parameter. Just create a function that accepts array as argument and then process them. The following demonstration will help you to understand how to pass array as argument in C# programming. Example4: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void printarray(int[] print) { int i; for (i = 0; i < 5; i++) { textBox1.Text = textBox1.Text + print[i]; textBox1.AppendText(Environment.NewLine); } } private void button1_Click(object sender, EventArgs e) { int[] num = new int[5] { 2, 4, 6, 8, 10 }; printarray(num); } } }
  • 10. Renas R. Rekany Object-Oriented-Programming 10 8- Most common properties of Array class Properties Explanation Example Length Returns the length of array. Returns integer value. int i = arr1.Length; Rank Returns total number of items in all the dimension. Returns integer value. int i = arr1.Rank; IsFixedSize Check whether array is fixed size or not. Returns Boolean value bool i = arr.IsFixedSize; IsReadOnly Check whether array is ReadOnly or not. Returns Boolean value bool k = arr1.IsReadOnly; H.M) Apply these properties in arrays (Max, Min, Sum, Average, First, Last). 9- Most common functions of Array class: Function Explanation Example Sort Sort an array Array.Sort(arr); Clear Clear an array by removing all the items Array.Clear(arr, 0, 3); GetLength Returns the number of elements arr.GetLength(0); GetValue Returns the value of specified items arr.GetValue(2); IndexOf Returns the index position of value Array.IndexOf(arr,45); Copy Copy array elements to another elements Array.Copy(arr1,arr1,3); Example5: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void arr_mul(int[] array1) { int i; double mul = 1; for (i = 0; i < 5; i++) { textBox8.Text = textBox8.Text + array1[i]; mul *= array1[i]; }
  • 11. Renas R. Rekany Object-Oriented-Programming 11 for (i = 0; i < 5; i++) { textBox9.Text = textBox9.Text+array1[i].ToString(); } Array.Sort(array1); for(i=0;i<5;i++) { textBox6.Text = textBox6.Text + array1[i].ToString(); } textBox11.Text = mul.ToString() ; } private void button1_Click(object sender, EventArgs e) { int[] arr1 = new int[5] { 7, 3, 12, 8, 11 }; int[] arr2 = new int[5]; int len, r,gl,gt; bool fixedsize, read_only; Array.Copy(arr1, arr2, arr1.Length); len = arr1.Length; textBox1.Text = len.ToString(); r=arr1.Rank; textBox2.Text = r.ToString(); fixedsize = arr1.IsFixedSize; textBox3.Text = fixedsize.ToString(); read_only = arr1.IsReadOnly; textBox4.Text = read_only.ToString(); gl=arr1.GetLength(0); textBox5.Text = gl.ToString(); textBox10.Text = arr1.Sum().ToString(); gt=Convert.ToInt32(textBox7.Text); textBox8.Text = arr1.GetValue(gt).ToString(); arr_mul(arr1); } } } Q) Write a program in C# to find the following in array, (Copy, Reverse, Find, Resize).