0% found this document useful (0 votes)
6 views

Lecture 6 CSC1261

Uploaded by

muaiadahmed81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 6 CSC1261

Uploaded by

muaiadahmed81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CSC1261 Computer Programming in C

Class: Year 1 CEGE (Civil Engineering)

Lecture #6: Arrays

Department of Computer and Software Engineering

16/09/2024 – 20/12/2024

Richard Mugisha
Course Plan (Provisional)
• Week 1: No Lecture due EPT
• Week 2: No Lecture due to EPT
• Week 3: Lecture #0 + Lecture #1
• Week 4: Lecture #2
• Week 5: Lab #1
• Week 6: Lecture #3 + Lecture #4
• Week 7: Lecture #5 + Lecture #6
• Week 8: Lab #2
• Week 9: Lecture #7
• Week 10: Lecture #8 + Lecture #9
• Week 11: Lab #3
• Week 12: Lecture #10 (Course Summary/Repetition)
• Week 13: Self Study
• Week 14: Self Study
Richard Mugisha
ARRAYS

Chapter 6
Introduction
• Arrays are data structures that hold multiple variables of the
same data type, stored in a consecutive memory location in
common heading.
• Array is a set of similar data (homogeneous data items)
that shares the common name.
• The individual values in the array are called as elements.
• An array lets you declare and work with a collection of values
of the same type.
For Example
• For example, you might want to create a collection of five
integers.

• One way to do it would be to declare five integers directly:


• int a, b, c, d, e;

• This is okay, but what if you needed a thousand integers?


• An easier way is to declare an array of five integers:
• int a[5];
• The five separate integers inside this array are accessed by
an index.
• All arrays start at index zero and go to n-1 in C.
For Example
For example
int a[5];

a[0] = 12;
a[1] = 9;
a[2] = 14;
a[3] = 5;
a[4] = 1;
Declaring arrays
• Arrays are declared along with all other
variables in the declaration section of the
program.
• Defining the type of array,
➢ Name of the array,
➢ Number of subscripts (whether is one or multi-
dimensional)
For example

/* Introducing array's */
#include <stdio.h>
main()
{
int numbers[100];
numbers[2] = 10;
//numbers[2];
printf("The 3rd element of array numbers is %d\n",
numbers[2]);
}
Assigning initial values to arrays

• The declaration is preceded by the word


"static". (Initialization is preceded by the
word "static" but this one is optional)
• The initial values are enclosed in braces
For example
E.g:
#include <stdio.h>
main()
{
int x;
static int values[] = { 1,2,3,4,5,6,7,8,9 };
//static char word[] = { 'H','e','l','l','o' };
for( x = 0; x < 9; x ++ )
printf("Values [%d] is %d\n", x, values[x]);
}

• Sample Program Output


• Values[0] is 1
• Values[1] is 2 ....
• Values[8] is 9
Average of age of 6 students
• #include<stdio.h>

• main()
• {
• int age[6];
• float sum;
• int count;
• float avg;

• for (count =0; count <6; count++)
• {
• printf("Enter age of the student %d:", count);
• scanf("%d", &age[count]);
• }
• sum=0;
• {
• for (count=0; count < 6; count ++)
• sum = sum+age[count];
• }
• avg=sum/6;
• printf("The average age of the class is %f. \n", avg);

• }
• N.B: -
✓ Array whose elements are specified by one subscript
are called one dimensional array or single
dimensional array.
✓ The maximum size of the array is 200 elements. If you
avail more than the declared size then the compiler will
treat only the first n elements as significant.
✓ The subscript used to declare an array is sometimes
called a dimension and the declaration for the array is
often referred to as dimensioning.
✓ The dimension used to declare an array must always
be a positive integer constant, or an expression that
can be evaluated to a constant when the program is
compiled
Multi Dimensioned Arrays

• Multi-dimensional arrays have two or more


index values, which specify the element in the
array.
➢ multi[i][j] // Two dimensional Array

• The first index value i specifies a row


index, whilst j specifies a column index.
Declaration and Calculations
• Declaration and initializing of two dimensional Array
– int m1[10][10];
– static int m2[2][2] = { {0,1}, {2,3} };
– sum = m1[i][j] + m2[k][l];

• NOTE: The strange way that the initial values have


been assigned to the two-dimensional array m2. Inside
the braces are,
– { 0, 1 },{ 2, 3 }

• Looking at the initial values assigned to m2, they are,


– m2[0][0] = 0 m2[0][1] = 1
– m2[1][0] = 2 m2[1][1] = 3
• Now, consider the following array declaration:
int values[3][4] ={1,2,3,4,5,6,7,8,9,10,11,12};
• The result of this initial assignment is as follows:
values[0][0]=1 values[0][1]=2
values[0][2]=3 values[0][3]=4
values[1][0]=5 values[1][1]=6
values[1][2]=7 values[1][3]=8
values[2][0]=9 values[2][1]=10
values[2][2]=11 values[2][3]=12
• This, can be initialized by forming groups of initials
values enclosed within braces
• int values[3][4] ={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
#include<stdio.h>
main(){
int a[4][4];
int i,j;
printf("Enter the 4*4 Matrix\n");
printf(" \n");
for(i = 0 ; i < 4 ; i++)
for(j = 0 ; j < 4 ; j++)
scanf("%d", &a[i][j]);
printf("The matrix entered is:\n");
for(i = 0 ; i < 4 ; i++){
for(j = 0 ; j < 4 ; j++){
printf("%d", a[i][j]) ;
printf("\t");
}
printf("\n");
}
}
• While initializing a two dimensional array, it
is necessary to mention the second (column)
dimension,
• The first dimension (row) is optional.
• Thus, the declarations given below are
perfectly acceptable.
➢ int arr[3][4]
={1,2,3,4,2,3,4,5,5,6,4,5};
➢ int arr[ ][4] ={1,2,3,4,2,3,4,5,5,6,4,5};
Write a program to sort the numbers in
Example: ascending order by comparison method
»

#include <stdio.h> for(j=i+1;j<n;j++){


main() if(a[i]>a[j]){
{ temp=a[i];
int i,j,temp,a[10],n;
a[i]=a[j];
printf("\n Enter how
many numbers to a[j]=temp;
sort:"); }
scanf("%d",&n);
}
printf("Enter
numbers: \n",a[i]); }
for(i=0;i<n;i++) printf("\n The
scanf("%d",&a[i]); numbers in
for(i=0;i<n-1;i++) ascending order:
{ ",a[i]);
for(i=0;i<n;i++)
printf("%d \t",a[i]);
}
CSC1261 Computer Programming in C

Class: Year 1 CEGE (Civil Engineering)

Lecture #6: Arrays

Department of Computer and Software Engineering

16/09/2024 – 20/12/2024

Richard Mugisha

You might also like