unit 4 fpl
unit 4 fpl
Pimpri
Syllabus:
Arrays: One Dimensional Arrays, Declaration of One-dimensional Arrays,
Initialization of One- dimensional Arrays, Two –dimensional Arrays, Initialization of
Two- dimensional Arrays.
Character Arrays and Strings: Declaration and Initialization String Variables,
Reading Strings from Terminal, Writing Strings to Screen, Putting Strings Together,
Comparison of Two Strings, Introduction to String handling Functions
Introduction
Arrays in C are a kind of data structure that can store a fixed-size sequential collection
of elements of the same data type. Arrays are used to store a collection of data, but it is
often more useful to think of an array as a collection of variables of the same type.
Examples:
List of employees in an organization
List of products and their cost sold by a store
Score of class of the students
List of customers and their address
Types of Arrays in C
There are two array types in C based on the number of dimensions.
1. One-dimensional Arrays in C
Also known as 1-D arrays or vectors, the one-dimensional arrays in C language have
just one dimension. They are represented by a single row or column.
1
FPL Unit-IV DIT,
Pimpri
Fig: 1D-Array
Initialization of One-Dimensional Arrays
After an array is declared, its elements must be initialized. An array can be initialized
at either of the following stages:
At compile time
At run time
Compile Time Initialization
When an array is declared, it can be initialized. This is also referred to as initialization
at build time.
When an array is declared, its elements can be initialized in the same manner as
regular variables. Initializing an array often has the following form or syntax:
type array_name[size]={list of values};
Example:
#include <stdio.h>
int main(){
int nums[3]={0,1,2};
printf(" Compile-Time Initialization Example:\n");
2
FPL Unit-IV DIT,
Pimpri
printf(" %d ",nums[0]);
printf("%d ",nums[1]);
printf("%d ",nums[2]);
}
Output:
012
Example:
Input
Run-Time Initialization Example:
Enter array elements: 10 20 30 40 50
Output:
Accessing array elements after dynamic Initialization: 10 20 30 40 50
3
FPL Unit-IV DIT,
Pimpri
2. Multi-dimensional Arrays in C
Dimension of an array refers to a particular direction in which array elements can be
varied. An array with a single dimension is known as a one dimensional array. An
array that has a dimension greater than one, is known as a multidimensional array.
Two-Dimensional Arrays in C
First Method
In the first method we can write all the elements together as shown in the below
example.
int a[4][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
Second Method
This method is comparatively better as we have clearly specified the elements in each
row. Look at the example shown below.
int a[4][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}};
In the above example we have nested brackets which shows the element of each row
individually.
Three-Dimensional Arrays in C
3-D arrays have three dimensions and can be visualized as a collection of 2-D arrays
organized one on the other to create a third dimension.
Syntax of 3-D Array
1D Array Programs
START
Step 1 → Take an array A and define its values
Step 2 → Loop for each value of A
Step 3 → Display A[n] where n is the value of current iteration
STOP
5
FPL Unit-IV DIT,
Pimpri
Source Code
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int i;
1234567890
Algorithm
Step-by-step procedure of this program −
START
Step 1 → Take an array A and define its values
Step 2 → Loop for each value of A in reverse order
Step 3 → Display A[n] where n is the value of current iteration
STOP
Source Code
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int i;
for(i = 9; i >= 0; i--)
printf("%d ", array[i]);
return 0;
}
0987654321
6
FPL Unit-IV DIT,
Pimpri
Algorithm
Step-by-step procedure of this program −
START
Step 1 → Take an array A and define its values
Step 2 → Loop for each value of A
Step 3 → Add each element to 'sum' variable
Step 4 → After the loop finishes, display 'sum'
STOP
Source Code
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, i;
sum = 0;
for(i = 9; i >= 0; i--) {
sum = sum + array[i];
}
printf("Sum of array is %d.", sum);
return 0;
}
START
Step 1 → Take an array A and define its values
Step 2 → Loop for each value of A
Step 3 → Display A[n] where n is the value of current iteration
STOP
Source Code
#include <stdio.h>
int main() {
7
FPL Unit-IV DIT,
Pimpri
1234567890
START
Step 1 → Take an array A and define its values
Step 2 → Loop for each value of A
Step 3 → Add each element to 'sum' variable
Step 4 → After loop finishes, divide sum with number of array elements
Step 5 → Store that result to avg variable and display.
STOP
Source Code
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, i;
float avg;
sum = avg = 0;
Algorithm
Step-by-step procedure of this program −
START
Step 1 → Take an array A and define its values
Step 2 → Declare largest as integer
Step 3 → Set 'largest' to 0
Step 4 → Loop for each value of A
Step 5 → If A[n] > largest, Assign A[n] to largest
Step 6 → After loop finishes, Display largest as largest element of array
STOP
Source Code
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int i, largest;
largest = array[0];
START
9
FPL Unit-IV DIT,
Pimpri
Source Code
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int i, smallest;
smallest = array[0];
2D Array Programs
Creating a Matrix in C Programming Language
1. Using an Array
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("Matrix created using an array:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
10
FPL Unit-IV DIT,
Pimpri
}
return 0;
}
Output
Matrix created using an array:
123
456
789
Output
Result of addition:
10 10 10
10 10 10
10 10 10
11
FPL Unit-IV DIT,
Pimpri
t=0;
for(k=0;k<c;k++)
{
t+=(a[i][k]*b[k][j]);
}
m[i][j]=t;
}
}
printf("\nResult Matrix : \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",m[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter No of Rows: 2
Enter No of Columns: 2
Enter A Matrix: 2
3
4
5
2 3
4 5
Enter B Matrix: 5
6
7
8
5 6
7 8
Result Matrix:
31 36
55 64
13
FPL Unit-IV DIT,
Pimpri
Syntax
There are many syntaxes for creating a character array in c. The most basic syntax is,
char name[size];
The name depicts the name of the character array and size is the length of the
character array.
The size can be greater than the length of the string but cannot be lesser. If it is
lesser then the full string can't be stored and if it is greater the remaining spaces
are just left unused.
#include <stdio.h>
#include<string.h>
int main(){
//initialization
char char_array[] = {'c', 'h', 'a', 'r', '$','*'};
14
FPL Unit-IV DIT,
Pimpri
Output
char$*
printed successfully
length of character array = 6
String
String is a sequence of characters that are treated as a single data item and terminated
by a null character '\0'. Remember that the C language does not support strings as a
data type. A string is actually a one-dimensional array of characters in C language.
These are often used to create meaningful and readable programs.
For example: The string "home" contains 5 characters including the '\0' character
which is automatically added by the compiler at the end of the string.
15
FPL Unit-IV DIT,
Pimpri
char s[5];
16
FPL Unit-IV DIT,
Pimpri
The gets() function can also be used to read character string with white
spaces
char str[20];
printf("Enter a string");
scanf("%[^\n]", &str);
printf("%s", str);
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Output
#include <stdio.h>
int main()
{
17
FPL Unit-IV DIT,
Pimpri
char name[20];
return 0;
}
18
FPL Unit-IV DIT,
Pimpri
strcpy(): This function is used to copy one string to another. It takes two
arguments, the first argument is the destination string where the source string
will be copied and the second argument is the source string.
strcat(): This function is used to concatenate two strings. It takes two
arguments, the first argument is the destination string where the source string
will be appended and the second argument is the source string.
strcmp(): This function is used to compare two strings. It takes two
arguments, the first argument is the first string and the second argument is the
second string. It returns an integer value that indicates the result of the
comparison.
strrev(): This function reverses a given string. It is a built-in function that is
defined in the string.h header file. The function takes a string as input and
modifies the original string by reversing it.
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
return 0;
}
Output
Length of string a = 7
Length of string b = 7
Note that the strlen() function doesn't count the null character \0 while calculating the
length.
19
FPL Unit-IV DIT,
Pimpri
strcat() function in C:
source: This is a pointer to the string that is to be concatenated. It is the string that will
be appended to the destination string. The source string is not modified by the
function. It is specified as a constant pointer to indicate that the function will not
modify the source string.
20
FPL Unit-IV DIT,
Pimpri
Output
int main() {
char str1[50] = "Hello, ";
const char str2[] = "how are you";
const char str3[] = "?";
strcpy() function:
int main()
{
char s1[50], s2[50];
21
FPL Unit-IV DIT,
Pimpri
strcpy(s1, "StudyTonight");
strcpy(s2, s1);
printf("%s\n", s2);
return(0);
}
strrev() function:
Example: strrev()Function
#include <stdio.h>
int main()
{
char s1[50];
printf("Enter your string: ");
gets(s1);
printf("\nYour reverse string is: %s",strrev(s1));
return(0);
}
Programs on String:
22
FPL Unit-IV DIT,
Pimpri
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
Output
Enter a string: This website is awesome.
Enter a character to find its frequency: e
Frequency of e = 4
int main()
{
char *str1 = "science";
char *str2 = "technology";
swap(str1, str2);
printf("str1 is %s, str2 is %s", str1, str2);
getchar();
23
FPL Unit-IV DIT,
Pimpri
return 0;
}
int main()
{
char str[] = { "abbba" };
return 0;
24
FPL Unit-IV DIT,
Pimpri
Output
abbba is a palindrome
Question Bank
1) What is an Array in C Programming?
2) How to create an Array?
3) What are the properties of an array?
4) Advantages and disadvantages of Array?
5) Can we change the size of an array at run time?
6) What is the default value of Array?
7) What is the two-dimensional array?
8) What is need of multidimensional array in c?
9) Write a program to sum values of given array.
10) Write a program to insert an element and in the specific position in the array?
11) What is a Character Array?
12) How to declare and initialize a character array?
13) What is a string in C programming?
14) How to declare a string in C?
15) What are the string functions? List them and explain with example.
16) How to compare the strings in C?
17) Program to reverse words in a given string.
25