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

10 - C Basics - Arrays (Part 1)

The document provides an overview of arrays in C programming, including their definition, declaration, initialization, and usage in functions. It emphasizes the importance of arrays for storing homogenous data and demonstrates how to calculate averages using arrays. Key points about array access, passing to functions, and initialization methods are also discussed.

Uploaded by

rosahshhsh
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)
7 views

10 - C Basics - Arrays (Part 1)

The document provides an overview of arrays in C programming, including their definition, declaration, initialization, and usage in functions. It emphasizes the importance of arrays for storing homogenous data and demonstrates how to calculate averages using arrays. Key points about array access, passing to functions, and initialization methods are also discussed.

Uploaded by

rosahshhsh
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/ 15

C Basics

10 - Arrays
/AhmedHatemS

/c/AhmedHatemS
What is the content?
➢Motivational example

➢Arrays

➢Multi-dimensional arrays
Write Write a C program to calculate the average of 3 numbers.

Write a C program to calculate the average of 5 numbers and read those


Write numbers from the user.

Write a C program to calculate the average of n numbers and read those


Write numbers from the user.
Arrays
Arrays are data structures that allow us to store data of the
same type in contiguous memory locations ..

Note that array indices in C always start at 0!


ArraysWhy?
Various problems in life need arrays, for example:

o A group of grades for a student needs to be saved


together. If you save these grades – for example 5 grades
– without using an array, you have to define 5 different
variables. But if you use arrays, you can define just one
variable as an array that contains 5 values.

o Thus, when we need a way to hold different homogenous


values in order to apply some sort of processing on all of
these values. Varying processing types may be applied,
such as calculating the average of these values, or the
total summation of the values, … etc.
Arrays
Declare an array by specifying the data type it will store, its name,
as well as its size.
<data type> name[<size>];
Arraydeclaration

The number of
values in this array
are three values.
Declare that the
values in this The name of the
array are going to array is a. To read
be integers. This any value in this
means that every array, you are
value is going to going to use this
be an integer. name.
Arrays
Array declarationandinitialization:
You can set all values of the array in just one line.

The number of
values in this array
are three values.
Declare that the
values in this The name of the The values in this array.
array is going to array is a. To read You will notice that they
be integers. This any value in this are 3, and they are all
means that every array, you are integers. The first value
value is going to going to use this is 4, the second value is
be integer. name. 7, the third value is 2.
Ways to
initialize array
elements

a[3] = 4; → array-index-out-of-bound error.


This index is not included in the array.
Arrays
Accessing Array Elements

0 1 2

65 87 30

for ( i n t i = 0; i < 3; i++)


{
printf("%d\n", temperature[i]);
}
Remarks
const int x = 5;
The const keyword specifies that the object or variable is not modifiable
(i.e., specifies that a variable's value is constant and tells the compiler to
prevent the programmer from modifying it.)

#define PI 3.14
A Pre-processor directive (#define identifier replacement) ... When the pre-
processor encounters this directive, it replaces any occurrence of identifier
in the rest of the code by replacement.

What about
array’s size?
Passing arrays to functions
Passing arrays:
o To pass an array argument to a function, specify the name
of the array without any brackets:
o int myArray[ 24 ];
o myFunction( myArray, 24 );
o Array size usually passed to function
o Function prototype:
o void modifyArray( int b[], int arraySize );
o Parameter names are optional in prototypes:
o int b[] could be written int [].
o int arraySize could be simply int.
Passing arrays to functions
#include <stdio.h>

double getAverage(int arr[], int size)


{
int i, sum=0; int main()
double avg; {
for (i=0; i<size; i++) const int size=6;
{ int grades[] = {73, 35, 63, 81, 12, 70};
sum += arr[i]; printf(“%d”, getAverage(grades, size));
} return 0;
avg = (double) sum / size; }
return avg;
}
Arrays:Key
Points
Array elements are like normal variables
o c[ 0 ] = 3;
o printf(“%d”, c[ 0 ]);
o Perform operations in subscript. If x equals 3
c[ 5 - 2 ] == c[ 3 ] == c[ x ]
Arrays:Key
Points
When defining arrays, specify:
o Name
o Type of array
o Number of elements
o arrayType arrayName[ numberOfElements ];
o Examples:
o int c[ 10 ];
o float myArray[ 3284 ];
o Defining multiple arrays of same type:
o Format similar to regular variables.
o For example:
o int b[ 100 ], x[ 27 ];

You might also like