Professional Documents
Culture Documents
PROGRAMMING IN C
Name
Dr Masoud H. Mahundi
Affiliation
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;
marks[3]
▪ There representation in the memory
marks[4]
▪ 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
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:
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
▪ For example
Physics Maths
25 32
76 73
56 61
▪ Elements to be stored in this will be float: The name of the array is grade
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
#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
▪ 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