Lecture 6 CSC1261
Lecture 6 CSC1261
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.
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
• 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
16/09/2024 – 20/12/2024
Richard Mugisha