Table of Contents
In this article, we will discuss the C Language One dimension Array
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
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
Ex:
I explain for you of this array
Here,
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
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]
Normal insertion
Insertion with for loop
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.