INSTITUTE - UIE
DEPARTMENT- ACADEMIC UNIT-2
Bachelor of Engineering (Computer Science & Engineering)
Subject Name and Code: Problem Solving with Programming
20CST-111
DISCOVER . LEARN . EMPOWER
Array & Strings
1
OBJECTIVES
The course aims to provide exposure to problem solving with
programming
The course aims to raise the programming skills of students
OBJECTIVES via logic building capability
& With the knowledge of C language students would be able to
model real world problems
LEARNING CO No. TITLE LEVEL
CO1 Identify situations where Understand
OUTCOMES computational methods would be
useful
CO2 Approach the programming tasks Remember
using learned techniques and write
pseudocode
CO3 Choose the right data representation Understand
format based on problem
requirement
CO4 Compare and check the limitations Understand
2
of programming constructs for the
•Array
•Types of Array
Content •Declaration of one Dimensional Array
•Initialization of one Dimensional Array
•Examples
•References
3
In C programming, one of the frequently arising problem is to handle
similar types of data.
For example: If the user want to store marks of 100 students. This can be
done by creating 100 variable individually but, this process is rather
tedious and impracticable. These type of problem can be handled in C
programming using arrays.
An array is a sequence of data item of homogeneous value(same type).
All the data items of an array are stored in consecutive memory locations
Array in RAM.
The elements of an array are of same data type and each item can be
accessed using the same name.
4
Array is of three types:
Types
of
Array
5
Declaration of one-dimensional array: We know that all the
variables are declared before they are used in the program.
Similarly, an array must be declared before it is used.
During declaration, the size of the array has to be specified.
The size used during declaration of the array informs the
compiler to allocate and reserve the specified memory
locations.
Declaration of 1-
D data_type array_name[array_size];
For example:
Array
int age[5];
Here, the name of array is age. The size of array is 5,i.e., there
are 5 items(elements) of array age. All element in an array are
of the same type (int, in this case).
6
Each element of array can be accessed and used by user according to
the need of program.
For example:
int age[5];
Array Elements
Note that, the first element is numbered 0 and so on.
Here, the size of array age is 5 times the size of int because there are
5 elements.
Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes
Then, the next address (address of a[1]) will be 2124d, address of a[2] will be
2128d and so on.
7
Initialization of one-dimensional array:
Arrays can be initialized at declaration time in this source code as:
int age[5]={2,4,34,3,4};
It is not necessary to define the size of arrays during initialization.
Initialization int age[]={2,4,34,3,4};
1-D Array In this case, the compiler determines the size of array by
calculating the number of elements of an array
8
1. Program to take 5 values from the user and store them in an array.
#include<stdio.h>
int main()
{
int values[5];
Examples of printf("Enter 5 integers: ");
for(int i = 0; i < 5; ++i)
1-D Array {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i)
{
printf("%d\n", values[i]);
}
return 0;
}
Here, we have used a for loop to take 5 inputs
from the user and store them in an array. Then,
using another for loop, these elements are
displayed on the screen
9
2. Program to find the average of n numbers using arrays
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter number of elements: ");
Examples of scanf("%d", &n);
for(i=0; i<n; ++i)
1-D Array {
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;
} Here, we have computed the average
of n numbers entered by the user.
10
1.
Array is defined as an
ordered set of similar data
items
SUMMARY 2. 3.
During declaration of an Arrays can be initialized at
array the size of the array declaration time in this
has to be specified. source code
PROGRAMS
1. Write a C Program to Addition of All Elements of the Array
2. Write a C Program to insert an element in an array.
3. Write a C Find Smallest Element in Array .
4. Write a C program to reversing an element in an array.
FREQUENTLY 5. C program to find the sum and calculate percentage of marks of n
students using arrays
ASKED
QUESTIONS
12
1.What is the right way to initialize an array?
A.int num[6] = { 2, 4, 12, 5, 45, 5 };
B.int n{} = { 2, 4, 12, 5, 45, 5 };
UTILISE C.int n{6} = { 2, 4, 12 };
YOUR D.int n(6) = { 2, 4, 12, 5, 45, 5 };
KNOWLEDGE
TO ANSWER 2. An array element is always stored in …………………..memory location?
A. Sequential
Let us see how much you have
learned from the lecture and B. Random
how effectively you can apply
your knowledge…!! C. Sequential and Random
D. None of the above
13
3. Let x be an array. Which of the following operation are illegal?
I ++X
II X+1
III X++
UTILISE IV X*2
YOUR A.I and II
KNOWLEDGE B.I, II and III
TO ANSWER C.II and III
D.I, III and IV
Let us see how much you have
learned from the lecture and E.III and IV
how effectively you can apply
4 Size of an array need not be specified, when
your knowledge…!!
A. Initialization is a part of definition
B. It is a declaratrion
C. It is a formal parameter
D. All of these 14
DISCUSSION
FORUM
Q1 C Program to Delete duplicate elements from an array
15
Book References:
1. Kanetkar, Y. (2010). Let us C. 15th ed.
2. Thareja Reema (2014) Programming in C. 2nd ed.
3. Zed A. Shaw, Learn C the Hard Way’
4. https://en.wikibooks.org/wiki/C_Programming
REFERENCES Video Lecture:
1.https://nptel.ac.in/courses/106/106/106106127/
BOOKS 2.https://nptel.ac.in/courses/106/105/106105171/
WEBSITES 3.https://study.com/academy/lesson/declaring-one-dimensional-arrays-definition-example.html
4.https://www.youtube.com/watch?v=XZGZm-0wUC0
COURSES
Websites:
1.https://www.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html
2.https://beginnersbook.com/2014/01/c-arrays-example/
3.https://www.codesdope.com/c-array/
16
THANK YOU
17