Storing Values and Printing Arrays in C

Written by

Pooja Rao

So far we have seen how to declare and access the array elements. Now to perform any operation on arrays, the element need to hold some value , right ?

C provides 3 methodologies to allocate values to the elements and they are:

1.) Initializing an Array:

  • When declaring an array, you can simultaneously initialize it just as you normally initialize any variable.
  • In this case, we need to provide the data of all the elements of the array previously itself.
  • Syntax:
data_type_name array_name[size] = { value1, value2, ..... };
  • Example:
int rollno[6] = {10, 11, 12, 13, 14, 15};
  • Here, you are initializing an array of int which is ‘rollnno’ , having 6 elements. 
    Firstly, always comply to the size you have specified in the array. In the above example, since we have mentioned array size of 6 elements we have given only 6 values. Even if we exceed by one value you will get a compile time error as the declaration and value assignment do not match.
  • If you declare that the size is n , but give values may be n-2 or less, the ending elements for which value was not specified are initialized to 0.
  • Example 1:
int rollno[6] = {10, 11, 12};

https://www.studymite.com/wp-content/uploads/2018/11/array-in-C-03.png

  • Example 2:
int rollno[6] = {};

So, in order to avoid both the cases; usually programmers do not mention the size; they skip it as shown below. It is very much appropriate and will not throw an error.

  • Example:
int rollno[] = {10, 11, 12, 13, 14, 15};

Lastly, remember to specify a comma separated list of values for your elements.

2.) Assigning Values of an Array:

  • The second way is to use assignment operator in order to give elements their respective values.
  • Any value that is compatible with the data type of the array ( recollect operator precedence chart ) , will be an acceptable value.
  • Example:
rollno[0] = 1;

Here, we are assigning value 1 to 0th index / 1 st element of the array.

  • The following assignment operations are valid , however observe them carefully:
#include <stdio.h>

int main()
{
  int rollno[2];
  rollno[0] = 1;
  rollno[1] = 2.5;  //since array is of type int, and value assigned is decimal,
                    //while storing the value, rollno[1] = 2 ; decimal part will be
                    //truncated. Recall operator precedence to know the reason.

  float temp[2];
  temp[0] = 25.63;
  temp[1] = 28;  //since array is of type float, and value assigned is int,
                 //temp[1] = 28.00000 ; i.e. int data type is typecasted as float
                 //and stored.

  int rollno[2];
  rollno[0] = 1;
  rollno[1] = 'a';  //since array is of type int, and value assigned is char type,
                    //while storing the value, rollno[1] = 97 ; 97 is the ASCII value of
                    //letter 'a'.

  return 0;
}

  • Consider you have a large class of 80 people; now to assign the roll nos it would be tedious to assign each of 80 element individually. Hence, we use loops to assign value as follows:
#include <stdio.h>

int main()
{
  int i, rollno[80];

  for(i=0; i<80; i++ )
  {
    rollno[i]= i+1;
  }

  return 0;
}
  • Note that you cannot assign an array to the other array directly. In order to copy values from one array to the other we have to access each element and do so, iterating in a loop. We shall see the code for copying values from one array to the other in the coming sections related to array in C.

3.Inputting Values in an Array:

  • It is not always possible for us to know the values of the data-sets used in the program. Suppose, a student wants to check average marks secured by him/her for a particular semester. In this case, you would require the student to enter the marks, then process on it and give the result.
  • We do not know the marks previously, in such cases inputting values is required and is the most common way of assigning values to elements of the array.
  • We use a loop, in order to scan the values from the command line input as follows:

Example:

#include <stdio.h>

int main()
{
  int i, marks[5];

  for (i=0; i<5; i++)
  {
    scanf("%d", &marks[i]);
  }

  return 0;
}

In this loop, the iteration starts from 0 to 4 that is first till last element  to capture values of those elements.

Now we have understood how to assign values . Let us move ahead and see how to print an array.

Simple Program to read and display n numbers:

#include <stdio.h>
#include <conio.h>

int main()
{
  clrscr();

  int i, n, a[10];

  printf("Enter the size of your dataset \n");
  scanf("%d",&n);

  printf("Enter the elements of your dataset \n");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }

  printf("The elements of your dataset / array are: \n");
  for(i=0;i<n;i++)
  {
    printf("%d\n",a[i]);
  }

  getch();
  return 0;
}

Output:

Enter the size of your dataset
5

Enter the elements of your dataset
10
20
30
40
50

The elements of your dataset / array are:
10
20
30
40
50

Try finding the average of n numbers or first n natural numbers in order to get used to arrays.

Uptil now we have only seen declaration, access and assignment pertaining to a single dimensional array i.e possessing value corresponding to a single parameter.

Going ahead we shall have an insight on how to use and operate on multi-dimensional array.

 
Learn C Programming

Storing Values and Printing Arrays in C