An array is a linear data structure that contains a particular number of data of the same type. The data structure is a system of any programming language that deals with the storing, organizing, and processing of data. In our today’s C programming tutorial we are going to learn everything about the array in c. This will be our first tutorial about the data structure. After learning the array in detail you can start solving problems and will be ready to participate in contest programming on any of your favorite online judge platforms for programming.
The following things will be covered in detail throughout this post :
- An Array And Its Types In C
- Initialization Of An Array Data Structure In C
- What Is int Array Data Structure?
- String Array And How To Use This In C
- Pros And Cons Of An Array
- Why Is Array Used In C?
Array and its types in C:
An array is a homogeneous data structure that contains same type of data and stores them in contiguous memory allocations. If you need to store and access a series of data you can use an array for that task.
Suppose, you have 5 data 11, 22, 33, 44, and 55. Now you are asked to store them. For this kind of task, you need to use an array data structure. The memory allocation for these 5 data will be continuous in the CPU. If we consider the memory location of 11 as 1004 then the memory location for 22 will be 1008.
So, these 5 data will be stored in the memory as below:
Data Memory location
11 —–> 1004
As the size of an integer data is 4 bytes that so why each of the data are taking 4 bytes of space in memory. And these data are stored in a contiguous memory location.
That is how the array stores data in the memory location. If you have a character type of array. The memory location for that array will be 1001, 1002, 1003, 1004, and 1005 as the size of the character type of data is 1 byte.
In terms of dimension, there are 2 types of arrays in programming language and they are:
- 1 Dimensional array
- Multidimensional array
A 1-dimensional array is the type of array that contains data in 1 dimension you can say in one row.
In a multidimensional array, data can be accessed in more than one row and column.
Throughout this post, we will discuss the 1D array and in the next post, we will learn about the Multidimensional array. In a 1D array, you can take a series of the same type of data in a single row or in a single column. The above memory allocation for 5 integer data is an example of 1D array allocation.
But In terms of data type array can be so many types. For example:
- Integer type array
- Character type array or string
- Bool type array…and so on.
To get basic more details about arrays you can check this wikipedia post.
Initialization Of An Array Data Structure In C:
Syntax to initialize an array data structure in C is : data_type array_name[size_of_the_array]
You can declare any type of array using the above syntax. Suppose, you want to use an integer type of array having a length of 1000. Let’s give the array name arr (you can use any name by following variable naming rules). So, the initialization for that array will be.
The index for this array will be from 0 to 999. This means the first element for this array will be at index 0 and the second element at index 1 and so on till 999. So, the memory allocation for the n length of the array will be from index 0 to n-1.
What Is int Array In C?
Int array data structure is an integer type of array which contains only a series of integer types of data. You can’t take character type of data or any other types of data instead of integer data. The initialization of the integer type of array we have already discussed above. So, the syntax for an integer type of array will be : int array_name[array_size];
Consider the following example of an integer array data structure.
Suppose, your teacher asks you to calculate and print the final marks for all of the students in your class in his subject. He told you that the final mark of a student will be 25% of his first term exam, 30% of his second term exam, and the rest 45% of his final term exam. You have to print the results for all of them and also have to store their final marks for future purposes.
If he asks you to calculate and store only 1 student mark then you can easily do this task using 4 integer type variables. 3 variables to take input for the 3 terms mark and the 4th variable to store the final mark. I think you can do it by yourself easily.
But if your teacher tells you that he has 40 students then what will you do?
Of course, you aren’t going to take 40 integer variables to store the final marks for those 40 students. You must use a single variable to store the marks for those 40 students. For this kind of situation, we need to use an array data structure. Let’s solve this problem step by step using an array in C.
We will make the program efficient and easier for the teacher so that he can use this code to determine the final mark for his future students too where the number of students and their marks can be different.
First, we will ask for the number of students and take the input in an integer variable n. Then we will declare an array of size n+1 for storing the final result of n number of students. Now, your question is why our size of the array is n+1 instead of n. Just keep reading carefully the rest of the solution you will find the answer to your question later. Suppose we give the name of the array as stud.
So declaration for that array will be: int studt[n+1];
Now, let’s use three integers a, b, and c to take input the three-term exam results. We will use a for loop and run it n times. For each time the loop will take 3 terms exam marks in a,b, and c of each student and will calculate their final marks and store the final mark in the array. Finally, we will print their roll and results using the printf function and the array.
Now at least take some time and try to write the code on your own. Whatever you wrote without watching my code comment below. After trying your own look at my solution for this problem.
printf(“Write the number of studentsn “);
for(i=1; i<=n; i++)
printf(“Give the marks for 3 term exam of the student of roll %dn”,i);
scanf(“%d %d %d”, &a, &b, &c);
stud[i]=((25*a)/100 +(30*b)/100 +(45*c)/100) ; //Calculating the final result and storing it on the
//i-th location of the array student
printf(“Final result of the %d students are belown”,n);
for(i=1; i<=n; i++)
printf(“Roll %d marks: %dn”,i,stud[i]);
50 60 55
40 52 63
27 34 76
56 72 88
90 50 60
Final result of the 5 students are below
Roll 1 marks: 54
Roll 2 marks: 53
Roll 3 marks: 50
Roll 4 marks: 74
Roll 5 marks: 64
I hope you understood the code. Now if you exactly calculate their mark for the given input you will find that the exact mark for some students is different than the output. Find out the reason and must comment below on how you can solve this problem and print the exact mark for each student. [Hint: Integer data can’t consider the fractional part so 5.8 will be counted as 5 for integer data and the fractional part will be rejected]
One more thing here our elements on the array started counting from 1 to n. But the array started from the 0 positions. As we already know the array stores elements from 0 to n-1 position for an n length of array, but we want to place our all student mark at the roll no. position that’s why we take the array length n+1 to access elements from 1 to n no.
If you declare the array size n but try to access the element from 1 to n-th index then for the n-th index it will show you an unexpected value. Suppose, your array length is 5 like int arr;
Now you want to print arr. Then it will show you an unexpected value which is called garbage value in the programming language. Because the array contains only 0 to 4th index total 5 positions for 5 data but you are trying to access the 6th data that is arr which is out of the array length. So, the compiler won’t find anything in the array that’s why it will show you garbage value as output.
To solve this problem we have used for loop. If you don’t know about for loop you can read this article about loop statements in c.
String Array And how To Use This In C:
A string is a character type array that contains only character types of data in the contiguous memory location.
Syntax for string array:
To take an string array as input you can use for loop and manually take each character as input like :
For this case, the string will take 10 characters as input. If you want to take a word as input that means the input-taking process will continue until putting a space that is a null character as input. Then we just have to change the for loop condition.
Here ‘�’ means null character. ‘0’ and NULL is also called null operator in c.
We can take an entire word without space as input in a string without for loop using %s input command. Also, we can print a string without space without using any loop using the same command. To read and print a string our code will be :
printf(“The string is= %s”,st);
The string is= helloguys
However, you can take a series of characters without space using the above way. Now if you wish to take an entire sentence containing multiple spaces but the size of the string array is unknown then you can’t take input on the string by the above method. We can’t take this kind of input in string in c. But we can take this kind of input in string in C++. I will upload that topic and show you how to use this kind of string in my future post. So, don’t forget to read my future posts about the C programming tutorials.
In C you can use some built-in function to perform several operations in string. They are shown below:
strlen(string_name); –> It will give the size of the string.
strcpy(string1,string2);—> It will copy string 2 to string 1.
strcat(string1,string2)—> it will merge string1 and string2 into one string.
strcmp()—> It will compare two string.
strlwr()—> It will convert the entire string into lowercase characters.
strupr()—> It will convert the entire string characters in uppercase.
Pros And Cons Of An Array Data Structure:
At first, we will discuss the cons of an array and then the pros of an array.
- We can use at most 64-kilobyte data in an array. So, we can access a maximum of 6.5k integer data or an equivalent amount of other data. So, the memory allocation is fixed and limited for an array.
- We must define first the length so we can’t take input or access data more than its length. Also, if you declare an array of size 100 but you need to take only 10 data as input then the rest of the 90 allocated memory for that array will be wasted.
- Insertion at any position of the array is difficult and time-consuming.
- Deletion at any position of the array is also much more difficult and time-consuming.
- The traversal of the array is much easier compared to the other data structure.
- Searching any element on the array is also easier and less time-consuming.
- You can find any particular positions element by writing ar[i] to get the i-th indexed element.
- Sorting among array elements is much easier by using the sort function (example: sort(ar,ar+n) where n is the length of the array ar.)
Why Is Array Used In C?
The array is used in C for a particular number of data and for the operations having complexity O(n).
As we already discussed the pros and cons of an array. So, the pros array is useful for those particular situations. In array traversal, Searching, and sorting have the complexity of just O(n). So, if you have a series of elements and you want to traverse it only once and during the traversal, you can perform some change among the elements, for this kind of situation array is used in C. Accessing any particular indexed element array has a complexity of O(1). So, direct access to any element among a series of element arrays would be the best data structure.
When you need to check the array over and over and perform operations among the element during the traversal then your complexity won’t be O(n). If you need to use 2 nested loops on the n size of the array then the complexity would be O(n2). If you need to use multiple nested for loops then the complexity of that problem will be more complicated. For K number of nested loops, complexity will be O(nk). If you have a small array size you can perform some nested for loops on that array. But you can’t use a large length of the array to perform nested for loop operation. If your nk<65000 then you can use array. For the nk>65000 conditions you can’t use array data structure.
So, I think you got the complete concept about array and specially 1D array data structure in C. Hopefully, from today you will be able to use array data structure in C. Array is one of the popular and most useful data structure for computer programmers. After getting clear concept about array you can move to problem solving level and ready to prepared yourself for contest programming. In the next tutorial I will show you how to insert an element in any position of the 1D array and also how to delete elements from array. After then I will teach you about multidimensional array in C with example. So, don’t forget to check that article too. To get my all article about C programming click here.