Call a Method Which Will Read a File of Data and Load the Array in C#

An array is a variable that tin store multiple values. For case, if you desire to store 100 integers, you can create an array for it.
int information[100];
How to declare an assortment?
dataType arrayName[arraySize];
For example,
float mark[5];
Hither, we declared an assortment, marker, of floating-point type. And its size is five. Significant, it can hold 5 floating-point values.
Information technology's important to note that the size and type of an array cannot be changed in one case it is declared.
Access Array Elements
You tin can access elements of an array past indices.
Suppose you declared an array marking as above. The first chemical element is mark[0], the second element is mark[one] and and so on.

Few keynotes:
- Arrays have 0 as the first index, not 1. In this case, mark[0] is the first element.
- If the size of an array is north, to access the last element, the
n-1
index is used. In this example, marking[4] - Suppose the starting address of
mark[0]
is 2120d. Then, the address of themark[1]
will exist 2124d. Similarly, the accost ofmark[two]
will be 2128d and so on.
This is considering the size of afloat
is four bytes.
How to initialize an array?
It is possible to initialize an assortment during declaration. For example,
int mark[v] = {nineteen, x, eight, 17, 9};
You lot can also initialize an assortment like this.
int mark[] = {19, 10, eight, 17, nine};
Here, we haven't specified the size. Nevertheless, the compiler knows its size is 5 equally nosotros are initializing it with 5 elements.

Hither,
mark[0] is equal to nineteen mark[1] is equal to 10 marker[ii] is equal to 8 mark[3] is equal to 17 mark[four] is equal to ix
Change Value of Array elements
int marking[v] = {19, ten, viii, 17, 9} // brand the value of the third chemical element to -1 mark[2] = -1; // brand the value of the fifth chemical element to 0 marking[4] = 0;
Input and Output Array Elements
Hither'southward how you can accept input from the user and store information technology in an array element.
// take input and store it in the tertiary element scanf("%d", &mark[ii]); // take input and store it in the ith element scanf("%d", &mark[i-one]);
Hither's how you tin print an individual element of an array.
// impress the showtime element of the array printf("%d", mark[0]); // print the third chemical element of the array printf("%d", mark[2]); // print ith element of the array printf("%d", marking[i-1]);
Example 1: Array Input/Output
// Program to accept 5 values from the user and store them in an array // Impress the elements stored in the array #include <stdio.h> int main() { int values[five]; printf("Enter 5 integers: "); // taking input and storing information technology in an array for(int i = 0; i < 5; ++i) { scanf("%d", &values[i]); } printf("Displaying integers: "); // printing elements of an array for(int i = 0; i < five; ++i) { printf("%d\due north", values[i]); } return 0; }
Output
Enter 5 integers: 1 -iii 34 0 3 Displaying integers: i -3 34 0 3
Hither, we have used afor
loop to accept 5 inputs from the user and store them in an array. And then, using some otherfor
loop, these elements are displayed on the screen.
Example ii: Summate Average
// Program to detect the average of n numbers using arrays #include <stdio.h> int main() { int marks[10], i, n, sum = 0, average; printf("Enter number of elements: "); scanf("%d", &due north); for(i=0; i < northward; ++i) { printf("Enter number%d: ",i+1); scanf("%d", &marks[i]); // adding integers entered by the user to the sum variable sum += marks[i]; } average = sum / northward; printf("Average = %d", average); return 0; }
Output
Enter n: v Enter number1: 45 Enter number2: 35 Enter number3: 38 Enter number4: 31 Enter number5: 49 Average = 39
Here, nosotros have computed the average of north numbers entered by the user.
Access elements out of its bound!
Suppose you declared an array of x elements. Let's say,
int testArray[10];
You can access the array elements from testArray[0]
to testArray[ix]
.
Now let's say if you try to access testArray[12]
. The element is not available. This may cause unexpected output (undefined behavior). Sometimes you lot might go an mistake and some other time your program may run correctly.
Hence, you should never admission elements of an array outside of its spring.
Multidimensional arrays
In this tutorial, y'all learned about arrays. These arrays are called 1-dimensional arrays.
In the next tutorial, y'all will acquire about multidimensional arrays (array of an array).
Source: https://www.programiz.com/c-programming/c-arrays
ارسال یک نظر for "Call a Method Which Will Read a File of Data and Load the Array in C#"