Practical Application for C Programming: Arrays

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In this practical application lesson, you will write C code to create, initialize, and display arrays. You will create one dimensional and multidimensional arrays in C.

Lesson Overview & Knowledge Required

After successfully completing this lesson, you will be able to define and create an array in C programming. You will be able to explain the index of an array, and describe how a two-dimensional array is defined and constructed in C. As a prerequisite, you should have a C compiler installed (such as Dev C++) in order to build and test your code.

Program Code

An array in C is a single object that holds multiple values, each of the same data type. Once you declare an array, you cannot change the size and type. For example, the following code creates an integer array with 50 integers:


int buckets[50];


Recall that the elements of the array are noted by an index, which starts counting at 0. Therefore, to access the fifth element in the previous array, you reference the index of 4.


int x;
x = buckets[4];


There are a couple of ways to initialize an array. The following lines are perfectly valid:


int high_scores[] = {200, 300, 400, 500, 9000};
int high_scores[5] = {200, 300, 400, 500, 9000};


The following code builds upon the previous code but shows how you can loop through the array to display the values.


#include <stdio.h>
int main() {
 //array to store 5 high scores
 //initialize with values
 int high_scores[] = {200, 300, 400, 500, 9000};
 //get the number of elements in the array
 int len = sizeof(high_scores) / sizeof(int);
 int i;
 int score;
 printf("High Scores: \n");
 //loop through the array
 for(i = 0; i < len; i++) {
  printf("Score %d", i+1);
  printf(" %d\n", high_scores[i]);
 }
 return 0;
}


Creating But Not Initializing

So far we have filled our arrays right away. This isn't always necessary. You can declare an array:


int scores[10];


At some point later in the program, you can then fill the elements of the array, either via user input or some other means. It doesn't matter when you add the values to the array, but you must initialize the array to begin with.

Arrays with Two or More Dimensions

You can also create multidimensional arrays in C. This is helpful if you want to create a matrix or table of data. A two-dimensional (2D) array can be thought of in terms of rows and column. A three-dimensional (3D) array is like a cube or 3D structure. Dimensions beyond three are difficult to conceptualize, let alone code!

The following code creates a 3x3 2D array; the table that follows is a display of how the data aligns to the table structure.


int tracker[3][3];


Column 1 Column 2 Column3
Row 1 tracker[0][0] tracker[0][1] tracker[0][2]
Row 2 tracker[1][0] tracker[1][1] tracker[1][2]
Row 3 tracker[2][0] tracker[2][1] tracker[2][2]

Code Application

Now that we have covered one- and two-dimensional arrays, it's your turn to write C code for arrays!

Code Application 1: User Input

Write a program that accepts user input to fill a one-dimensional array that stores batting averages (you can use integer values such as 346; or you can use float values, such as .346). Users should enter the top 5 batting averages; the program should then display the results. Recall the syntax for using the scanf function to get integer values:


scanf("%d", &int_variable);
scanf("%f", &float_variable);


Code Application 2: 2-Dimensional Array

Next, create three 2-dimensional arrays. Two should have pre-filled values, while the third should be empty. These are matrices, and you will be adding the first two together, and inserting the results into the third.


 ing x[2][2] = {
  {25, 30},
  {40, 10}
 };
 int y[2][2] = {
  {100, 50},
  {200, 5}
 };
 int z[2][2];


To add matrices, you work across the columns. The first column in the left matrix is added to the first column in the second. Then add the second column in the first to the second column from the second matrix. Repeat for all columns. Then repeat for rows.

Figure 1 shows an example:


Figure 1: Adding Matrices
Adding Matrices


You will need a nested loop to perform this math.

Follow-Up Questions

Question 1

Create an array called nums and give it 8 elements. Run the following code. Why is the result 32?


int nums[8];
printf("Size of nums %d", sizeof(nums));


Question 2

Consider the following 3D array. How would you write a loop to display all elements in the array?


int cube[2][3][2];


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support