TTS Module10-CC102
TTS Module10-CC102
MODULE
College
INFORMATION SHEET MD-5.1.1
“Arrays”
An array is a special type of variable which can contain or hold one or more values of the same
data type with reference to only one variable name. In other words, the array variable has a common
name identifier and can hold many values at the same time, provided that they have the same data
type.
An array variable can be distinguished through a pair of square brackets and on specified
number inside the square brackets;[ ]. The number inside the square brackets is called an index or
element. In the case of enumerated arrays, we can distinguish its declaration through a pair of curly
brackets: { }.
10 20 50 80 30
This example illustrates that the array variable ar[5] is an integer data type and we can store a maximum
of 5 values into it. These values to be stored or manipulated must be all integer data (whole numbers).
Here is the individual value of array variable ar[5].
ar[0] = 10;
ar[1] = 20;
ar[2] = 50;
ar[3] = 80;
ar[4] = 30;
Other example:
char name[4]
float area[3];
Example:
1. Write a program using one-dimensional array that loads or stores the 5 values into an array
variable. The values are the resulting computation from a simple equation. Then display the
stored values.
#include<stdio.h>
main()
{ int no[5]; int i; clrscr();
printf(“\n One-Dimensional Array”);
/* this first loop stores the 5 values */
for (i=0;i<5;i++)
no[i] = i + 10;
/* this second loop displays the previously stored 5 values*/
for (i=0;i<5;i++)
printf(“ %d”,no[i]);
getch();
}
2. Write a turbo c program that will print the values of the following array:
int n[5]= { 5, 10, 15, 20, 25};
#include<stdio.h>
main()
{
int n[5]={ 5, 10, 15, 20, 25};
int x;
for (x=0; x<4; x++)
printf(“Element %d is %d”, x,n[x]);
getch();
}
3. Write a Turbo C program that will compute and display the sum of the following array:
int n[5]= { 2, 1, 0, 5, 2, 7, 4, 3, 10,5};
#include<stdio.h>
main()
{
int n[5]= { 2, 1, 0, 5, 2, 7, 4, 3, 10,5};
int x, sum;
for (x=0; x<9; x++)
sum +=a[x];
printf(“The sum is %d ”,sum);
getch();
}
4. #include<stdio.h>
main()
{
int n[5]={ 5, 10, 15, 20, 25};
int x, temp, size;
size=5;
for (x=0; x<=size/2; x++)
{ temp = a[x];
a[x] = a[ ((size-1) – x)];
a[ ((size – 1) – x)] = temp;
}
for (x=0; x<=4; x++)
printf(“Element %d is %d”, x,a [x]);
getch();
}
Two-Dimensional Arrays syntax:
Datatype arrayname[arrow][acol];
Example:
int score[2][3];
Column
0 1 2
Row 0 10 80 30
1 40 50 90
Our two-dimensional array score[r][c] is an integer data type and it can hold only an integer data with a
maximum of 6 values ([2]x[3]). The row 2 and a maximum of 6 is simply called 2 by 3(2 multiplied by 3).
Using these graphical representations, we can understand the array easily.
Here is the individual value of array variable score[2][3].
score[0][0] = 10
score[0][1] = 80
score[0][2] = 30
score[1][0] = 40
score[1][1] = 50
score[1][2] = 90
Example:
1. Write a program using two-dimensional array that loads and stores 6 values into an array
variables. The values are the resulting computation from a simple equation. Then display the
previously store values.
#include<stdio.h>
main()
{
int r,c; int num[2][3];
clrscr();
printf(“\n Two Dimensional Array”);
/* loads the values */
for (r=0; r<2;r++)
for(c=0;c<3;c++)
num[r][c]=r(*4) + c+19;
/*displays the previously loaded values */
for(r=0; r<2; r++)
for(c=0;c<3;c++)
printf(“ %d”, num[r][c]);
printf(“\n”);
getch();
}
2. Write a program using two-dimensional arrays that determines the even numbers among the
twelve input values from the keyboard and prints the list of these Even numbers.
#include<stdio.h>
main()
{ int r,c, mod; int nos[3][4];
clrscr();
/* this loop scans the twelve nos */
printf(“\n Enter twelve numbers:”);
for (r=0; r<3;r++)
for(c=0;c<4;c++)
scanf(“%d “, &nos[r][c]);
/*This loop determines and prints the Even numbers */
printf(“\n Here is the list of Even numbers”);
Write the screen output of the following program segment. Write your answer in the box provided.
1. #include<stdio.h>
int a[5];
int x,y;
main()
{ clrscr();
for (x=0;x<5;x++)
a[x]=(1+x)*2-1;
for(x=0;x<6;x++)
{
for(y=0; y<=x-1; y++)
printf(“+”);
printf(“\n”);
for(y=x;y<5;y++)
printf(“ %d”, a[y]);
}getch();
}
STUDENT NAME: __________________________________ SECTION: __________________
STUDENT NAME: __________________________________ SECTION: __________________
1.
2.
3.
5 - Excellently Performed
4 - Very Satisfactorily Performed
3 - Satisfactorily Performed
2 - Fairly Performed
1 - Poorly Performed
_________________________________
TEACHER
Date: ______________________