You are on page 1of 4

Arrays

It is a special type of variable, which can hold one or more


values of the same data type with reference to only one variable
name. It is a series or list of variables in computer memory, all of
which have the same name but the difference is the special numbers
called subscript, index, or element.
Syntax:
dataType arrayName[number of elements];
Note: In C language, elements starts with 0.
SINGLE DIMENSIONAL ARRAY or ONE DIMENSIONAL ARRAY
an array that represents a single list of values
Example 5.1. Assume the values
int score[7];
score[0] = 1
score[1] = 5
score[2] = 3
score[3] = 4
score[4] = 0
score[5] = 6
score[6] = 9
1. score[1] + score[2] = 8
2. score[3]*score[5] = 24
3. score[3+2] = 6
4. score[3*2] = 9
5. score[1] < score [2] = False
Example 5.2. Create a C program that will input 10 integers and
display these integers form the last entered to the first entered integer
using an array.
#include<stdio.h>
#include<conio.h>
int a[10];
int ctr;
main()
{
for(ctr=0;ctr<=9;ctr++)
{
printf("Enter Number [%d]: ",ctr+1);
scanf("%d",&a[ctr]);

}
printf(The input number in reverse:);
for(ctr=9;ctr>=0;ctr--)
printf("\n%d",a[ctr]);
getch();
}
Example 5.3. Build a program that will input 5 integers and display
them in ascending order.
#include<stdio.h>
#include<conio.h>
int a[5];
int ctr1,ctr2,temp;
main()
{
for(ctr1=0;ctr1<=4;ctr1++)
{
printf("Enter Number [%d]: ",ctr1+1);
scanf("%d",&a[ctr1]);
}
for(ctr1=0; ctr1<=3; ctr1++)
for(ctr2=ctr1+1; ctr2<=4; ctr2++)
{
if (a[ctr1]>a[ctr2])
{
temp=a[ctr1];
a[ctr1]=a[ctr2];
a[ctr2]=temp;
}
}
printf(ASCENDING ORDER);
for(ctr1=0; ctr1<=4; ctr1++)
printf("\n%d,a[ctr1]);
getch();
}
Example 5.4. Build a program that will input 10 integers and will print
the sum and average.
#include<stdio.h>
int A[10];
int num, ctr, sum=0;
float ave;
main()
{

system("cls");
for(ctr=0;ctr<=9;ctr++)
{
printf("Enter number [%d]=",ctr+1);
scanf("%d",&A[ctr]);
}
for (ctr=0;ctr<=9;ctr++)
sum=sum+A[ctr];
ave=sum/10.0;
printf("The sum = %d",sum);
printf("\nThe average = %.2f",ave);
system("pause");
}
TWO DIMENSIONAL ARRAY an array that represent values in a
table or grid that contains rows and columns instead of a single list.
Syntax:
dataType arrayName[no. of rows][no. of cols];
Example 5.5. Make a C program that will display 10 x 10
multiplication table.
#include<stdio.h>
#include<conio.h>
int a[10][10];
int ctr1, ctr2;
main()
{
for (ctr1=1; ctr1<=10; ctr1++)
for(ctr2=1; ctr2<=10; ctr2++)
a[ctr1][ctr2]=ctr1*ctr2;
for (ctr1=1; ctr1<=10; ctr1++)
for(ctr2=1; ctr2<=10; ctr2++)
{
printf("\t%d",a[ctr1][ctr2]);
}
getch();
}

Machine Problems #4:


1. Create a C program that determines the ODD numbers among 10
input values and output the list of these ODD numbers
2. Build a C program that will input 10 values and arranged it from
highest to lowest.
3. Write a code in C that will input 20 integers and display all negative
numbers entered and all positive numbers entered.
4. Using a 5x5 array, search a number, then determine and output the
list of times it occurs on the list of 25 numbers entered.
5. Using 15x15 array, create a multiplication table.

You might also like