C Programming

Arrays in C

An array is a group of data objects of the same kind kept nearby in ram. Inside the C programming, arrays are indeed a derivative type of data that may hold primitive data types like int, char, double, float, etc. It may also hold a group of derivative data types like pointers, structures, and so forth. Whenever you wish to record a student’s grades in six courses, we shouldn’t need to create separate variables for each subject’s grades. Alternatively, we may create an array that could hold the marks for every topic in shared memory regions. We may simply retrieve the items by utilizing the array. To retrieve the array’s members, just a few other lines of the C script are necessary. Let’s take a glance at some instances to see the working of arrays in C language. When writing this tutorial, we have been using the Ubuntu 20.04 operating System to elaborate arrays.

Example 01:

Let’s start with opening the terminal shell in Ubuntu 20.04 operating system. Use “Ctrl+Alt+T” to open the shell console within 5 seconds. Let’s begin with creating a C language file using the “touch” query as below.

Open it in some editor to add code to it. Hence, we have been using the “nano” query to open this file in the GNU.

Now the file has been opened in the GNU Editor; it’s time to write C code to elaborate arrays. It would help if you recognized that there are diverse ways to declare an array and initialize it. We will be discussing the first and simplest way to declare and initialize an array in C language in this example. So, add the input-output standard library first. After this, start the main function in the code. We have affirmed an array of magnitude 5. After this, we have added values to the array at every index separately. After that, we have used the “for” loop to print all 5 elements of an array “arr” in the shell. This is one simplest way to state and formerly make ready an array. Save your code and quit it using “Ctrl+S” and “Ctrl+X” one after another.

Now the file has been saved, we have to compile the code to make it an executable file. So, use the “gcc” c++ compile to compile it as below.

Let’s just run the file to see the output of a C code of array. We have to use the represented instruction in the console and tap on the “Enter” key to do that. The output shows all 5 values of the array one after another.

Example 02:

Let’s look at a new instance to declare and initialize an array with a different method this time. Hence, open your file “test.c” again.

As the file has been opened in the editor, add the input-output standard library first. The main function has been used as same as above. In this example, we have declared two arrays “odd” and “even” to initialize odd and even values in arrays consequently. The array “odd” has no size defined and has 5 elements initialized with odd values. The print message has been displayed to show the message before printing odd elements. For loop is used to collect the values from the index of array “odd” and print one by one. The array “even” contains 5 even numbers and a defined size of 5. The print message will be displayed before the “for” loop to show that the even numbers will be shown. Then, the “for” loop has been used to print the values of even arrays one by one.

Compile the “test.c” file yet again to execute it further.

After the compilation of a code, execute it using the below command. The output will be as revealed in the lower image. It will print odd array elements first, then even array elements and simple text messages before both arrays.

Example 03:

Let’s see an example of an array taking value from user input and then display those values in the terminal. So, open the same file once more.

The file is opened in the GNU nano editor now. We have added the below C script in it to take user input. After including the standard input-output header and the main function, we have declared an array “store” of size 7. The print statement has been utilized to print a message for the user to add only integer values. The “for” loop has been used to collect and add the user inputted values to the array “store” via & sign in the scanf function. After the “for” loop has been ended, another display message has been printed out. The second “for” loop has been used to display the array values on the screen via the “printf” statement. The main function closes with a return 0 phrase. Save this code and leave.

Now use the “gcc” query to compile this code.

As the code has been compiled, run via the below-shown query. The consumer has been requested to pass in the integer type values. You can see we have added 7 values and pressed Enter. After the display message, all the values added by the user would be saved into an array and then printed out.

Example 04:

Let’s take an example to calculate sum and average from the array contents. Open your C file first.

After the opening of a file, add the below code same to same in it. Again we have used the standard C library and main function first. Array “Arr” has been affirmed of size 10. Two integer variables: sum and avg, have been declared to count total and average from array elements. The print statement is used to ask the user to add integer values. The “for” loop is used to add the user-inputted values to the array “Arr” using the scanf method. Meanwhile, all the values added by the user will be summed up together and saved to variable “sum”. After this sum is displayed, and the average will be displayed after calculation.

Compile your code.

The output is shown below.

Example 05:

In this illustration, we will discuss how to sort an array. Open file to do that.

An integer variable “temp” and array “Arr” of size 10 have been initialized. The first “for” loop is used to print the current order of an array. The second and third “for” loops have been used to sort the existing array in ascending order using swapping. If the statement contains the condition that if the “i+1” index value is greater than the index value, add the index “I” value to variable temp. Add the values of index “i+1” to index “I”. Now add the variable temp value to index “i+1”. The fourth “for” loop is used to print the sorted array.

Compile the file.

The output below shows the unsorted array first; then, the array has been sorted and displayed.

Conclusion:

The fundamental data structure would be an array that allows every data object to be retrieved arbitrarily by using an index value. If you need to save comparable items, a C array is useful

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.