Array in C Program.
Hi Everyone,
Today we will discuss about Array in C . Array is very important in Program so before starting you need to be very clear about loops concept in C as in array nested for loops used mostly. Please follow my below blog for loop concepts for your reference.
https://dheeraj60.blogspot.com/2020/06/while-and-do-while-loop-lop-in-c.html
https://dheeraj60.blogspot.com/2020/06/nested-for-loop-with-simple-pattern.html
https://dheeraj60.blogspot.com/2020/06/loop-concept-in-c-with-examples-dotcmdb.html
Let’s Start with Array .
An array is a variable that can store multiple values for any data type like int , float etc , in other word int array holds the elements of int types while a float array holds the elements of float types. Arrays always start with 0 . We can access elements of an array by indices. Array has 0 has the first index not 1 as array always start with 0 . Suppose we declare any array of int data type 20 it means that o is the first element of array and 19 will be last element of Array .
Benefits of using Array in C :
Multiple data items of same data type can be accessed using single name. As in previous blogs we need to used multiple variables for int or float data types but in Array we can hold multiple data items in single data type
Arrays can be used to implement matrices
There are two Types of Array in C .
1. One Dimensional Array
2. Multi Dimensional Array that may be 2D Array , 3D Array etc.
In this blog we will discuss about One Dimensional Array(1D) and in next blog we will go through Multi Dimensional Array(2D,3D etc).
Let’s start first with syntax of 1D array then we will go with simple example of 1D array.
Declare 1D array is as below :
int i[10];
Here integer array is of 10 elements so in indices its start with 0 and end with 9.
Char ch[10]
Here Character array is of 10 element and indices start with 0 only .
Remember when we declare Array we need to put value in [] .
We can use array subscript (or index) to access any element stored in array. Subscript starts with 0, which means ar[0] represents the first element in the array ar.
Let’s take an example of 1D array where we can sum 4 numbers by taking input from user by using array no need to declare variable multiple times .
Below is the Program which gives you the sum of 4 numbers by using 1D array.
#include <stdio.h>
#include <stdlib.h>
{
int i=0;
int addition[4];
for (i=0; i<4;i++)
{
printf(" Please Enter numbers %d \n", (i+1));
scanf("%d", &addition[i]);
}
for (i=0; i<4;i++)
{
add = add+addition[i];
}
printf("The sum of four number is %d\n",add);
return 0;
}
In Above Program int addition[4]; Array- declaration length is 4 but remember elements start with 0 only . Secondly we are using a for loop to traverse through the array while storing the entered value in the Array. Here we are iterating the array from 0 to 3 because the size of the array is 4. Inside the loop we are displaying a message to the user to enter the values. All the input values are stored in the corresponding array elements by using scanf function. Below is the piece of code we use in above program.
for (i=0; i<4;i++)
{
printf(" Please Enter numbers %d \n", (i+1));
scanf("%d", &addition[i]);
}
2nd for loop code for reading data from an array
for (i=0; i<4;i++)
{
add = add+addition[i];
}
Below is the output of above Program in compiler Code block.
Hope you are able to understand 1D array and also see the benefits of array in one program . You can write several programs by using 1D array . Let’s write same program where we calculate the average of four numbers .
Note :One Important thing about Array before program .
If we declare array int i[5] we can also write this as int i[5]={12,10,20,33,36} here 12 element starts with 0 only .
Program to Calculate Average of four number by using same program which we written above.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
int avg=0;
{
printf(" Please Enter numbers %d \n", (i+1));
scanf("%d", &addition[i]);
}
for (i=0; i<4;i++)
{
add = add+addition[i];
}
avg=add/4;
printf("The Average of four number is %d\n",avg);
return 0;
}
Hope you understand this program as it’s act same as we written first program here we just declare avg and calculate the avg and through printf we displayed the average value of the number’s.
Below is the output of above program in code-block compiler.
Let’s write one more meaningful program in array where to see the elements of value stored in array and then calculate the total value given by the user and print the output of total value.
Below is the Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sub[5],i,total=0;
printf("Please Enter five numbers:\n");
for (i=0; i<5;i++)
{
scanf("%d", &sub[i]);
total=total+sub[i];
}
printf(" Elements for numbers are ");
for (i=0; i<5;i++)
{
printf("%d\n",sub[i]);
}
printf("Total value is %d\n",total);
return 0;
}
Here we declare array sub[5] which can holds five records starting with 0 and in program we use loop to calculate the total value which passed by user and also in another loop for knowing the elements we use loop to print the value and finaly we print the total value . Program act as same which we discussed in another program. but here Program to take 5 values from the user and store them in an array and Print the elements stored in the array.
Below is the output of above program in code- block compiler.
Hope you are able to understand the usage and benefits of 1D array in C program . Please write more programs by using 1D array where you can store student marks value and display the total number with percentage etc.
Please go through this blog carefully and let me know if you have any doubt writing 1D array program as in next blog I will go through multi dimensional array like 2D , 3D etc where we can play with rows and columns and will able to write more interesting program by using 2D and 3D array . so please go through this blog and practice as much as you can .
Thanks.
Comments
Post a Comment