You are on page 1of 23

ARRAY C PROGRAMMING

Prepared: ENGR. JONICIO A. DACUYA


An array is a variable that can store multiple values. For example, if you want to store 100
integers, you can create an array for it.

Instead of declaring individual variables, such as number0, number1, ...,


and number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual
variables. A specific element in an array is accessed by an index.
In C, we have to declare the array like any other variable before using it. We can declare an array by
specifying its name, the type of its elements, and the size of its dimensions. When we declare an
array in C, the compiler allocates the memory block of the specified size to the array name.
© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.
© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.
© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.
© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.
© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.
C Array Initialization
Initialization in C is the process to assign some initial
value to the variable. When the array is declared or
allocated memory, the elements of the array contain
some garbage value. So, we need to initialize the
array to some meaningful value. There are multiple
ways in which we can initialize an array in C.

© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.


Array Initialization with Declaration
In this method, we initialize the array along with its declaration. We use an
initializer list to initialize multiple elements of the array. An initializer list is
the list of values enclosed within braces { } separated b a comma.

© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.


SAMPLE ARRAY
PROGRAM

© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.


SAMPLE ARRAY
PROGRAM
SAMPLE ARRAY
PROGRAM
#include <stdio.h>

void main()
{
int arr1[100];
int i, mx, mn, n;
printf("\n\nFind maximum and minimum element in an array :\n");
printf("--------------------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);

SAMPLE ARRAY for(i=0;i<n;i++)


{
printf("element - %d : ",i);
PROGRAM scanf("%d",&arr1[i]);}
mx = arr1[0]; mn = arr1[0];
for(i=1; i<n; i++)
{
if(arr1[i]>mx)
{mx = arr1[i];
{if(arr1[i]<mn)
{mn = arr1[i];
}
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
#include <stdio.h>
void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
printf("\n\nSeparate odd and even integers in separate arrays:\n");
printf("------------------------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
SAMPLE ARRAY for(i=0;i<n;i++)
{

PROGRAM
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
Write a program in C to }else
{ arr3[k] = arr1[i]; k++;
separate odd and even integers }
into separate arrays. }
printf("\nThe Even elements are : \n");
for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}
printf("\nThe Odd elements are :\n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
Write a program that can
read three integers from the
user and then determines
the smallest value among
the three integers. Using
Array

May use while for another


process
Example of Array In C programming to find out the
average of 4 integers

#include <stdio.h>
int main()
{
int avg = 0;
int sum =0;
int x=0;
int num[4];

for (x=0; x<4;x++)


{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}

for (x=0; x<4;x++)


{
sum = sum+num[x];
}
avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}

© OXFORD UNIVERSITY PRESS 2013. ALL RIGHTS RESERVED.


ACTIVITY

In the recent IIEE convention, the unity dance was participated by the EE 1st – 4th year students. The
judges were allowed to input score from 1-10 out of 8 criteria for judging. Each contestant’s score is
calculated by dropping the lowest and highest scores and then adding the remaining scores. Please
create a program that allows the user to enter eight judges’ scores and then outputs the point received
by the contestant. A judge awards point between 1 and 10, with 1 being the lowest and 10 being the
highest. For example, if the scores are:
9.2, 9.3, 9.0, 9.9, 9.5, 9.5, 9.6 and 9.8, then the contestant receives a total of
Total points: 56.9 points.
Highest: 9.8
Lowest: 9.0
Average: 7.1125
After inputting the score to the different contestants, will display the winner of the competition with a
highest average points
(Note: You must use array in your solution for this problem)

You might also like