Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

arrays and functions in c language

Rating
-
Sold
-
Pages
36
Uploaded on
25-03-2023
Written in
2022/2023

gives a brief and detailed intro to arrays and functions in c language

Institution
Course

Content preview

MODULE 3
Arrays
INTRODUCTION
Arrays: Array is a sequential collection of similar data items.
Pictorial representation of an array of 5 integers

10 20 30 40 50
A[0] A[1] A[2] A[3] A[4]

 An array is a collection of similar data items.
 All the elements of the array share a common name .
 Each element in the array can be accessed by the subscript(or index) and array name.
 The arrays are classified as:
1. Single dimensional array
2. Multidimensional array.

Single Dimensional Array.

 A single dimensional array is a linear list of related data items of same data type.
 In memory, all the data items are stored in contiguous memory locations.
Declaration of one-dimensional array(Single dimensional array)
Syntax:

datatype array_name[size];



 datatype can be int,float,char,double.
 array_name is the name of the array and it should be an valid identifier.
 Size is the total number of elements in array.
For example:
int a[5];
The above statement allocates 5*2=10 Bytes of memory for the array a.



a[0] a[1] a[2] a[3] a[4]

float b[5];

, The above statement allocatests 5*4=20 Bytes of memory for the array b.


 Each element in the array is identified using integer number called as index.
 If n is the size of array, the array index starts from 0 and ends at n-1.

Storing Values in Arrays

 Declaration of arrays only allocates memory space for array. But array elements are not initialized
and hence values has to be stored.
 Therefore to store the values in array, there are 3 methods
1. Initialization
2. Assigning Values
3. Input values from keyboard through scanf()

Initialization of one-dimensional array
 Assigning the required values to an array elements before processing is called initialization.


data type array_name[expression]={v1,v2,v3…,vn};


Where
 datatype can be char,int,float,double
 array name is the valid identifier
 size is the number of elements in array
 v1,v2,v3…......vn are values to be assigned.


 Arrays can be initialized at declaration time.
Example:
int a[5]={2,4,34,3,4};


2 4 34 3 4


a[0] a[1] a[2] a[3] a[4]


 The various ways of initializing arrays are as follows:
1. Initializing all elements of array(Complete array initialization)
2. Partial array initialization

, 3. Initialization without size
4. String initialization


1. Initializing all elements of array:
 Arrays can be initialized at the time of declaration when their initial values are known in advance.
 In this type of array initialization, initialize all the elements of specified memory size.
 Example:
int a[5]={10,20,30,40,50};
10 20 30 40 50



2. Partial array initialization
 If the number of values to be initialized is less than the size of array then it is called as partial
array initialization.
 In such a case elements are initialized in the order from 0th element.
 The remaining elements will be initialized to zero automatically by the compiler.
 Example:
int a[5]={10,20};

10 20 0 0 0

3. Initialization without size
 In the declaration the array size will be set to the total number of initial values specified.
 The compiler will set the size based on the number of initial values.
 Example:
int a[ ]={10,20,30,40,50};
 In the above example the size of an array is set to 5


4. String Initialization
 Sequence of characters enclosed within double quotes is called as string.
 The string always ends with NULL character(\0)

char s[5]=”SVIT”; We can observe that string length is 4,but size
is 5 because to store NULL character we need
one more location.

So pictorial representation of an array s is as follows:

, S V I T \0
S[0] S[1] S[2] S[3] S[4]




3.1.2 Assigning values to arrays
Using assignment operators, we can assign values to individual elements of arrays.
For example:
int a[3];
a[0]=10;
a[1]=20;
a[2]=30;

10 20 30



a[0] a[1] a[2]


Reading and writing single dimensional arrays.
To read array elements from keyboard we can use scanf() function as follows:

To read 0th element: scanf(“%d”,&a[0]);
To read 1st element: scanf(“%d”,&a[1]);
To read 2nd element: scanf(“%d”,&a[2]);
……
…….
To read n element : scanf(“%d”,&a[n-1]);
th

In general
To read ith element:
scanf(“%d”,&a[i]); where i=0; i<n; i++


To print array elements we can use printf() function as follows:

To print 0th element: printf(“%d”,a[0]);
To print 1st element: printf(“%d”,a[1]);
To print 2nd element :printf(“%d”,a[2]);
……..
……..

To nth element : printf(“%d”,&a[n-1]);
In general
To read ith element:
printf(“%d”,a[i]); where i=0; i<n;

Written for

Institution
Course

Document information

Uploaded on
March 25, 2023
Number of pages
36
Written in
2022/2023
Type
Class notes
Professor(s)
Dr karthik
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
hemantkumar1

Also available in package deal

Get to know the seller

Seller avatar
hemantkumar1 nmit
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
7
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions