Professional Documents
Culture Documents
Lecture 6 - CS 174 Arrays
Lecture 6 - CS 174 Arrays
PROGRAMMING IN C
ARRAYS
Masoud H. Mahundi
mmahundi97@gmail.com or mahundi.masoud@udsm.ac.tz
0713832252 or 0768832424
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?
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
Array Initialisation
1. Initialisation as a set
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]);
7. } a different memory storage
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++){
Every iteration takes the next empty
9. printf("Number %d --> ",i);
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)
65 87
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
N K O M O
"Nkomo",
A M I L K A
"Amilka",
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 1 2
B
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
1 2 3 4 5 6 7 8 9 10 D
2