Table of Contents
C Language One dimension Array
In this article, we will discuss the C Language One dimension Array
One dimensional Array in C language
Knowledge Area
- What is Array
- Type of Arrays
- Declaration of one dimension Array
- Array Initialization
- Array processing
In the C Programming Language, an array is a fixed sequenced collection of the element of the same data types. an array can be used to represent a list of number(int) or name (string) or other data type(char, float, double) of similar elements. It is one of the ways of the grouping of similar types of data of single variables names.
Three types of arrays in C programming language
1. One dimensional array
2. Two-dimensional array
3. multi dimensional array
One Dimensional Array in c
Declaration of one d arrays.
data_type array_name[array_size];
for example
double salary[10]; int marks[50]; char name[200];
Here, we declared an array, marks of int type and size 10. It’s meaning it can allocate to 10 memory space for stored int (similar type) value.
The size and type cannot be changed after its declaration- it is a disadvatages of an array
- The array index is starting from 0 not 1
- The array index is ending to n-1(n is equal to array size or array length)
- Array size is starting 1 and ending to n
Ex:
I explain for you of this array
Here,
Initialize an array in C language
Standard method
you can place elements into an array by specifying the index position
marks[0]=45 //first element of an array marks[1]=78 //second element of an array marks[2]=65 //third element of an array marks[3]=68 //forth element of an array marks[4]=34 //fifth element of an array marks[5]=59 //sixth element of an array
Initializing array when the array declaration
method 1
int mark[6]={26,45,67,86,43,69};
when we initialized an array, the number of values are assigned in curly brackets {}. It can not be larger or smaller than the number of elements in the array between the given square bracket[]
method 2
int mark[]={26,45,67,86,43,69};
according to the number of an element given in the curly brackets, the size of the array is automatically allocated.
here, as six elements assign the curly brackets, it has allocated six spaces with spaces
Here Array is initialized
now,
26 is placed index marks[0]
45 is placed index marks[1]
67 is placed index marks[2]
86 is placed index marks[3]
43 is placed index marks[4]
69 is placed index marks[5]
insert element to array in c language
Normal insertion
Insertion with for loop
displaying element from the array
Normal display
display using for loop
display using while loop
i=0; while(i<10){ printf("%d",&a[i]); }
display using do-while loop
i=0; do{ printf("%d",&a[i]); }while(i<10);
Example
initialized and display elements using for loop
program 1
#include <stdio.h> #include <stdlib.h> int main() { int count; printf("How many student of your class\n"); scanf("%d",&count); //store array size takes from the user int i; int marks[20]; printf("Enter student marks\n"); for(i=0; i<count; i++){ scanf("%d",&marks[i]); //store array elements takes from the user } for(i=0; i<count; i++){ printf("you entered marks %d %d\n",i+1,marks[i]); //display data from array } getch(); return 0; }
When the above code executed it produces the following output
Program 2
initialized and display elements using while loop
#include <stdio.h> #include <stdlib.h> int main() { int count; printf("How many student of your class\n"); scanf("%d",&count); //store array size takes from the user int i; int marks[20]; printf("Enter student marks\n"); i=0; while(i<count){ scanf("%d",&marks[i]); //store array elements takes from the user i++; } i=0; while(i<count){ printf("you entered marks %d %d\n",i+1,marks[i]); //display data from array i++; } getch(); return 0; }
When the above code executed it produces the following output
Program 3
initialized and display elements using the do-while loop
#include <stdio.h> #include <stdlib.h> int main() { int count; printf("How many student of your class\n"); scanf("%d",&count); //store array size takes from the user int i; int marks[20]; printf("Enter student marks\n"); i=0; do{ //printf("%d",marks[i]); scanf("%d",&marks[i]); //store array elements takes from the user i++; }while(i<count); i=0; do{ printf("you entered marks %d %d\n",i+1,marks[i]); //display data from array i++; }while(i<count); getch(); return 0; }
When the above code executed it produces the following output
Final program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks[20],i,n,max;
printf(“enter the number of students!n”);
// enter how many student you want
scanf(“%d”,&n); //input fromuser
// array read processing
printf(“yes enter your marks!n”);
for(i=0; i<n; i++)
{ printf(“Enter the marks of students!%d “,i+1);
scanf(“%d”, &marks[i]);
}
max=marks[0];
for(i=1; i<n; i++)
{
if(max<marks[i])
{
max=marks[i];
}
}
//display
printf(“you enterd !n”);
for(i=0; i<n; i++){
printf(“t%d”,marks[i]);
}
printf(“nnThe maximum max is !%d”,max);
return 0;
When the above code executed it produces the following output
Related post
One dim Array in Java One dim Array in C++ One dim Array in C
Two dim Array in Java Two dim Array in C++ Two dim Array in C
Three dim Array in Java Three dim Array in C++ Three dim Array in C