Skip to main content

Array and it's Beneifts in C Program #DOTC_Mdb

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 main()

{

     int add =0;

     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 add =0;

    int i=0;

    int avg=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];

    }

       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

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...

Aggregate Functions and Group By Clause in Oracle with Examples #Diksha Oracle Training Centre

  Aggregate Functions and Group By Clause Oracle with Examples.   Hello Everyone, Today I will   discuss about   Aggregate function in SQL by using Group By Clause and different clauses with some examples . Please go tjrough my previous blogs in Archive folder for classification of SQL, Commands and   SQL joins for your understanding.   Aggregate Functions Allows us to perform a calculation on a set of values to return a single value . We can use Group by Clause to group the result-set by one or more columns. Also we can use Having clause to restrict or filter the data as per our requirement. Note: Whenever we use Aggregate function in SQL we can’t able to use where condition. To restrict or filter the record we need to use having clause instead of Where. Below is the most commonly used Aggregate function in SQL.   MAX : Max function   used to get the maximum values in a set of values. COUNT : This function used to count rows in ...