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

EST 102 - Programming in C Lecture notes

Rating
-
Sold
-
Pages
24
Uploaded on
23-04-2022
Written in
2021/2022

EST 102 - Programming in C Lecture notes

Institution
Course

Content preview

EST102 Programming in C


MODULE III

Arrays and strings-Arrays Declaration and Initialization, 1-Dimensional Array, 2-
Dimensional Array, String processing: In built String handling functions (strlen, strcpy,
strcat and strcmp, puts, gets), Linear search program, bubble sort program, simple
programs covering arrays and strings

ARRAY
An array in C is a collection of items stored at contiguous memory locations and
elements can be accessed randomly using indices of an array. They are used to store
similar type (homogeneous) of elements as the data type must be the same for all
elements. They can be used to store collection of primitive data types such as int, float,
double, char, etc of any particular type.
Indices 0 1 2 3
A
Array elements 10 20 30 40


Why do we need arrays?
We can use normal variables (v1, v2, v3) when we have a small number of
objects, but if we want to store a large number of instances, it becomes difficult to
manage them with normal variables. The idea of an array is to represent many instances
in one variable.
Different ways of declaring an Array
1. Array declaration by specifying size
Eg: int A[10];
2. Array declaration by initializing elements
Eg: int A[] = { 10, 20, 30, 40 };
3. Array declaration by specifying size and initializing elements
Eg: int A[6] = { 10, 20, 30, 40 };
Advantages of an Array
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of code.


IIPE 1

, EST102 Programming in C


Disadvantages of an Array
• It allows us to enter only fixed number of elements into it. We cannot alter the size of
the array once array is declared. Hence if we need to insert more number of records
than declared then it is not possible. We should know array size at the compile time
itself.
• Inserting and deleting the records from the array would be costly since we add /
delete the elements from the array, we need to manage memory space too.
• It does not verify the indexes while compiling the array. In case there is any indexes
pointed which is more than the dimension specified, then we will get run time errors
rather than identifying them at compile time.
Example:
int a[3]={1,2,3}
printf("%d",A[3]);
Output: 3245431 (Garbage Value)
1. Write a program to read and display an array of size n.
#include<stdio.h>
void main()
{
int n,A[20],i;
printf("Enter the size of Array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the number:");
scanf("%d",&A[i]);
}
for(i=0;i<n;i++)
{
printf("%d\t",A[i]);
}
}
OUTPUT
Enter the size of Array: 5
Enter the number:10
Enter the number:20
Enter the number:30
Enter the number:40
Enter the number:50

IIPE 2

, EST102 Programming in C

10 20 30 40 50

2. Write a program to read and display an array of size n in reverse order.
#include<stdio.h>
void main()
{
int n, A[20],i;
printf("Enter the size of Array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the number");
scanf("%d",&A[i]);
}
for(i=n-1;i>=0;i--)
{
printf("%d\t",A[i]);
}
}
OUTPUT
Enter the size of Array: 5
Enter the number:10
Enter the number:20
Enter the number:30
Enter the number:40
Enter the number:50
50 40 30 20 10

3. Write a program to find sum and average of an array of size n.
#include<stdio.h>
void main()
{
int n,A[20],i,sum; float avg;
printf("Enter the size of Array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the number");
scanf("%d",&A[i]);
}
sum=0;
for(i=0;i<n;i++)


IIPE 3

Written for

Institution
Course

Document information

Uploaded on
April 23, 2022
Number of pages
24
Written in
2021/2022
Type
Class notes
Professor(s)
Sajitha m
Contains
All classes

Subjects

$7.99
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
ashiqrahman

Get to know the seller

Seller avatar
ashiqrahman APJ Abdul Kalam Technological University
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
4 year
Number of followers
0
Documents
6
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