You are on page 1of 15

UNIVERSITY OF DAR ES SALAAM

PROGRAMMING IN C

Name

Dr Masoud H. Mahundi

Affiliation

Department of Computer Science and Engineering


University of Dar es Salaam

Emails
mmahundi97@gmail.com, mahundi.masoud@udsm.ac.tz

Phone Numbers
+255713832252, +255768832424
ARRAYS
Introduction, One-Dimensional, Two-Dimensional
Arrays
▪ Suppose we had a set of students grades

▪ We wished to read into the computer and perform some operations on each grade

▪ We cannot perform such an operation until each and every grade has been committed into memory
▪ Declare each and every student grade as a variable?
▪ float james; float juma; float bahati;

▪ What if they are 200?

▪ Arrays come in handy

▪ They are meant for jobs like this

▪ Having a variable that store a series of data with properties


1. Same data type (example float as above)
2. Referenced through the same name (marks, in the above example)
Arrays
▪ A different way of declaration, then data_type array_name[array_length];

▪ For example float marks[5];


▪ float is the data type of the elements marks[0]

▪ marks is the name of the array marks[1]

▪ 5 means this array will have 5 elements marks[2]

marks[3]
▪ There representation in the memory
marks[4]

▪ Arrays starts at position (index) 0

▪ Any item in the array can be accessed through its index, and it can be accessed any where
from with in the program
Arrays
▪ The position of an array is called an index or subscript

▪ The value held by an array under that index is called an element


▪ Array Initialisation

1. Initialisation as a set
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; // Elements with missing values will be initialized to 0:

int myArray[10] = { 1, 2 }; //initialize to 1,2,0,0,0...

int myArray[10] = { 0 }; //all elements will be 0

2. Elements without size


int myArray[] = {1,4,12,7};
3. Initialising each element individually

myArray[0] = 45;

myArray[1] = 71;
Arrays
1. #include<stdio.h>
2. main(){
3. int weights[5]; ✓ Every index holds one
4. printf("Weight 1:\t"); element
5. scanf("%d", &weights[0]); ✓ All elements bear the same
6. printf("Weight 2:\t"); name
7. scanf("%d", &weights[1]);
✓ Indices start from 0
8. printf("Weight 3:\t");
9. scanf("%d", &weights[2]);
10. printf("Weight 4:\t");
11. scanf("%d", &weights[3]);
12. printf("Weight 5:\t");
13. scanf("%d", &weights[4]);
14. }
Arrays

1. #include<stdio.h>
2. main(){
3. int ages[7]; int i; ▪ The loop helps in allocating multiple storages
4. for(i=0; i<7;i++){
▪ In this loop the control is “i”
5. printf("Enter age %d _", i);
▪ &ages[i] – as i changes it means
6. scanf("%d",&ages[i]);
a different memory storage
7. }
8. for(i=0; i<7;i++){
9. printf(" %d\n",ages[i]);
10. }
11. }
1. #include <stdio.h>
2. main() {
3. int i, arrLen;
4. printf("Please enter the number of Students ");
5. scanf("%d",&arrLen); ▪ An Array can also be initialised
6. float marks[arrLen];// Length is given by user through an iterative statement – a
7. printf("\n");
common way
8. for(i=0;i<arrLen;i++){
9. printf("Number %d --> ",i);
▪ Every iteration takes the next empty
10. scanf("%f",&marks[i]); } space
11. printf("\n"); ▪ Describe each line in the programm
12. for(i=0;i<arrayLim;i++){
13. printf("Number %d was --> ",i);
14. printf("%f", marks[i]);
15. printf("\n");
16. } }
Multidimensional Arrays
▪ Multi-dimensional

▪ When we want to store data in a table or matrix format

▪ For example
Physics Maths
25 32
76 73
56 61

▪ For a two-dimensional array: There are two indices


data_type array_name[row_size][column_size]

▪ For example; float grade[4][3];

▪ Elements to be stored in this will be float: The name of the array is grade

▪ It has 4 rows and 3 columns


Arrays
▪ Pictorial presentation/matrix is as shown

grade [0] [0] grade [0] [1] grade [0] [2]


grade [1] [0] grade [1] [1] grade [1] [2]
grade [2] [0] grade [2] [1] grade [2] [2]
grade [3] [0]

▪ Indices always start from zero

▪ Let the rows guide;

▪ one row till the end then you go to another row

▪ This is not necessarily how data is stored in a computer memory,

▪ but it is a way a programmer visualizes it


Arrays
float grade[7][2]; int i,j;

for(i=0;i<2;i++){ Outer loop (i < 2)

if(i==0)
Physics Maths
printf(" Physics\n");else printf(" Mathematics\n");
25 32
for(j=0;j<7;j++) 76 73
scanf("%f", &grade[j][i]); 56 61
45 76
} //printing values
Inner loop (j < 7) 67 52
for(i=0;i<2;i++){ 87 78
if(i==0) 65 87
printf(" Physics\n");else printf(" Mathematics\n");

for(j=0;j<7;j++)

printf("%f\n", grade[j][i]);

}
Arrays
❑ Array of Strings

❑ Elements of the array as rows

❑ Length of the array as columns

#include<stdio.h>
main(){
char names[5][10] = {
"Joshua", J O S H U A
"Nkomo", N K O M O
"Amilka", A M I L K A
C A B R A R
"Cabral"};
printf("Name is %s %s %s", names[0], names[1], names[2]);
}
Arrays
❑ Array of Strings

❑ Elements of the array as rows

❑ Length of the array as columns

❑ Can also be entered by users #include<stdio.h>


main() {
❑ Through scanf or gets functions
char names[5][10];
printf("Name 1\t");
scanf("%s", names[0]);
printf("Name 2\t");
scanf("%s", names[1]);
printf("Name is %s %s", names[0], names[1]);
}
Arrays
Array Limitations

▪ C has no range checking, so if you index past the end of the array, it will not tell you about it.
It will eventually crash or give you garbage data.

▪ There is no single operation on all the elements of the array. They have to be accessed one
after another. LOOPS are used to traverse the array
Arrays – Try and declare the following arrays
Physics Chemistry Maths CS 231 Persons Dependants
C
1 2 3 1 B 1 2
2 2 2
3 3 3
4 4 4
5 5
6 6
7
A 8
9

Jan Feb Mar Apr May Jun Jul Aug Sep Oct
D
1 2 3 4 5 6 7 8 9 10
2

You might also like