Skip to main content

C Language: How to Store Multiple values in Array

One data item can only be stored in one variable, in the context of computer programming. If programmer want to store more information in the memory, he/she should have more variables. Programmer can choose another option that is an Array.

Suppose that a student has scored 90 marks. These marks can be stored in a variable in c programming language as shown below:
int marks=90;

After executing this statement, the value 90 will be stored in the variable marks. Suppose there programmer needs to store marks of 10 students. In such case, we have to use 10 variables like marks1, marks2, marks3, …, marks10. But, if it is required to store the marks of 100 students, definitely it is not feasible to use 100 variables marks1, marks2, marks3, …, marks100. In mathematics, we use sets to group the items of similar kind. For example, consider the set shown below:

Marks = {75, 76, 78, 79, 86, 56, 98, 89, 56, 89}

This is a set of 10 students. Note that every item can be accessed by prefixing marks along with the position of marks in the sheet. The first item 75 corresponds to the marks of first student, 76 corresponds to the marks of second student and so on i.e., marks1=75, marks2=76, marks3=78, ….., marks10=89. In C language, this is where; the concept of arrays is used. Since all the marks are of the same type, we can group and refer all the marks with a common name using arrays.

An array is defined as an ordered set of similar data items. All the data items of an array are stored in consecutive memory locations in main memory. The elements of an array are of same data type and each item can be accessed using the same name. e.g., consider an array of marks of 5 students as shown below:

How to Store Multiple values in Computer Programming: Array

To refer an item in the array, we specify the name of the array along with position of the item. The position of the item must be written within square brackets '[ ]'. The position of the item enclosed within square brackets is called 'subscript' or 'index'. For example, the above figure represents an integer array called marks where marks of 5 students are stored. The marks of each student can be accessed as shown below:

  • Marks[0] i.e. 75 – represents marks of first student
  • Marks[0] i.e. 76 – represents marks of second student
  • Marks[0] i.e. 78 – represents marks of third student
  • Marks[0] i.e. 79 – represents marks of fourth student
  • Marks[0] i.e. 86 – represents marks of fifth student

Note: Using Marks[0] through Marks[n-1] we can access the marks of n students in general.

In an array it is not possible to have a group of items with different data types. For example,

How to Store Multiple values in Computer Programming: Array

This is an invalid way of storing the elements in an array. This is because, it is a collection of multiple datatypes, so we have to classify the array. The classification will be done in next article.

Comments

Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video



Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:


      Suppose next node is allocated at an address 506, so the list becomes,



  Suppose next node is allocated with an address with an address 10,s the list become,


The other way to represent the linked list is as shown below:




 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …