Module - 3 C Programming
Module - 3 C Programming
Module - 3 C Programming
Introduction
Classification of arrays
One Dimensional Array
Declaration of One Dimensional Array
Initialization of One Dimensional Array
Processing an One Dimensional Array
Accessing Elements of an Array
Two Dimensional Arrays
Declaration of Two dimensional Arrays
Initialization of Two dimensional Arrays
Processing a Two Dimensional Array
Multi-Dimensional arrays
Advantages of Arrays
INTRODUCTION:
An array is a group of related data items that share a common name
and each individual data items in an array is referenced by a subscript or
index enclosed in a pair of square brackets. This subscript indicates the
position of an individual data item in an array and the subscript must be
either an unsigned integer constant or integer variable or integer expression.
All these elements are stored in consecutive memory locations.
CLASSIFICATION OF ARRAYS:
Arrays which have elements with single subscript are known as one
dimensional array or single subscripted variable.
data_type ⭢ specifies the data type of elements to be stored in the array such
as int, float, char or double
array_name ⭢ name of the array which follows the rules of forming an
identifier.
size ⭢ The maximum number of elements that can be in the array.
Examples
int marks[50];
flaot height[20];
char name[30];
Here, the first declaration creates an array name “marks” 50 integers
elements. The second declaration creates “height” as an array of 20 real
elements and third declaration creates “name” as an array of 30 characters
long.
Note
1) The subscript of an array can be positive integer constant or integer
variable or integer expression.
2) In C, the subscript value ranges from 0 to maximum size-1
3) C does not perform bound checking. Therefore, the maximum subscript
appearing in a program for an array should not exceed the declared one.
data_type ⭢ specifies the data type of elements to be stored in the array such
as int, float, char or double
array_name ⭢ name of the array which follows the rules of forming an
identifier.
size ⭢ The maximum number of elements that can be in the array.
list of values ⭢ The values of the array elements separated by commas.
All these initial values must be constants.
Examples
int marks[5]={56,35,49,70,88};
In this declaration, marks [0] in initialized to 56, marks [1] is initialized to 35
and so on.
Note
1) There must be at least one initial value between the braces.
2) If you specify too few values, the remaining array elements are initialized
to zero.
3) If you specify too many values, an error message will be displayed.
4) There is no convenient way to initialize only selected array elements.
5) There is no short cut method for initializing a large number of array
elements.
#include<stdio.h>
main( )
{
int a[100], i, n;
printf(“\nEnter total number of elements in array :”);
scanf(“%d”,&n);
printf(“\nEnter the array elements :\n”);
for(i=0; i<n; i++)
scanf(“%d”,&a[i]);
printf(“\nThe array elements are \n”);
for(i=0; i<n; i++)
printf(“%d\t”,a[i]);
}
Ex: C program to find maximum, minimum and average weight of all the newly
born babies in a hospital
#include<stdio.h>
main( )
{
int i, n;
flaot w[20], max, min, average, sum;
clrscr( );
printf(“\nEnter number of babies :”);
scanf(“%d”,&n);
printf(“\nEnter weight of babies in kgs\n”);
for(i=0; i<n; i++)
scanf(“%f”,&w[i]);
max = w[0];
min = w[0];
sum = w[0];
for(i=1; i<n; i++)
{
if(w[i] > max)
max = w[i];
else if(w[i] < min)
min = w[i];
sum = sum + w[i];
}
average = sum/n;
printf(“\nMaximum weight = %f kgs”,max);
printf(“\nMinimum weight = %f kgs”,min);
printf(“\nAverage weight = %f kgs”,average);
getch( );
}
Ex: Write a C program to generate N Fibonacci series using one dimensional
array.
#include<stdio.h>
main()
{
int a[50],n,i ;
clrscr();
printf(“Enter the value of N\n”);
scanf(“%d”, &n);
a[0]=0;
a[1]=1;
for(i=0; i<n; i++)
{
a[i]=a[i-2]+a[i-1];
}
printf(“Fibonacci series is:\n”);
for(i=0; i<n; i++)
{
printf(“%d\t”, a[i]);
}
getch();
}
Output:
Enter the value of N 6
Fibonacci series is 11235
TWO-DIMENSIONAL ARRAYS:
Arrays which have elements with two subscripts are known as two
dimensional array or two subscripted variable. or
where,
type ⭢ Specifies the data type of elements to be stored in the
array such as int, float etc
array_name ⭢ Name of the array, which follows the rules of the identifiers.
rowsize ⭢ Maximum number of rows
colsize ⭢ Maximum number of columns
Examples: -
Where,
follows:
in matrix[2][3] = {{10,20,30},{40,50,60}};
Note: -
1) The number of sets of initial values must be equal to the number of rows
in the array.
2) If the values are minimum in initializing set, they are automatically set to
zero.
3) If the number of initial values in each initializing set is more than the
number of the corresponding row elements then there will be a
compilation error.
#include<stdio.h>
main()
{
int a[10][10],m,n,i,j;
clrscr();
printf(“Enter matrix order\n”);
scanf(“%d%d”, &m, &n);
printf(“Enter matrix elements\n”);
for(i=0; i<m; i++)
{
For(j=0; j<m; j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“The matrix elements are :\n”);
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
printf(“%d\t”, a[i][j]);
printf(“\n’);
}
}
getch();
}
Output:
Enter matrix order 22
Enter matrix elements 05
07
The matrix elements are
:
15
27
#include<stdio.h>
main()
{
int a[10][10], b[10][10], c[10][10] m,n,i,j;
clrscr();
printf(“Enter matrix orders\n”); scanf(“%d
%d”, &m, &n);
printf(“Enter first matrix elements\n”);
for(i=0; i<m; i++)
{
For(j=0; j<m; j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“Enter second matrix elements\n”);
for(i=0; i<m; i++)
{
For(j=0; j<m; j++)
{
scanf(“%d”, &b[i][j]);
}
}
}
printf(“\n”);
}
getch();
}
Output:
Enter matrix orders 22
Enter first matrix elements 24
35
Enter second matrix elements
05
11
The Resultant matrix is 29
86
#include<stdio.h>
main()
{
int a[10][10], b[10][10], c[10][10] m,n,i,j;
clrscr();
printf(“Enter matrix orders\n”); scanf(“%d
%d”, &m, &n);
printf(“Enter first matrix elements\n”);
for(i=0; i<m; i++)
{
For(j=0; j<m; j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“Enter second matrix elements\n”);
for(i=0; i<m; i++)
{
For(j=0; j<m; j++)
{
scanf(“%d”, &b[i][j]);
}
}
printf(“The Resultant matrix is :\n”);
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
C[i][j]=a[i][j]-b[i][j];
printf(“%d\t”, c[i][j]);
}
printf(“\n”);
}
getch();
}
Output:
Enter matrix orders 22
Enter first matrix elements 64
45
Enter second matrix elements 33
11
The Resultant matrix is 31
34
MULTI-DIMENSIONAL ARRAYS:
data_type array_name[s1][s2][s3]..........[sn] ;
ADVANTAGES OF ARRAYS:
1. Arrays are use d to store similar data elements.
2. Arrays are used to represent matrices in memory.
Chapter Checklist
String Manipulation
String Declaration
String Initialization
Reading and Writing Strings
Reading Strings
Writing Strings
Putting Strings Together
Comparison of Two strings
String Handling Functions (String Operations)
String Length (Strlen)
String Copy (Strcpy)
String Reverse (Strrev)
String Lower (Strlwr)
String Upper (Strupr)
INTRODUCTION:
A string is an array of characters. Any group of characters defined
between pair of double quotation marks is a string constant.
Ex: “My Name is Raju”
Definition:
Sequence of characters enclosed between two quote marks is
called string or string constant.
STRING MANIPULATION:
P R O G R A M M I N G \0
STRING DECLARATION:
Like integer and float variables, we can also declare string variables for
storing string values. The syntax for string declaration is:
char str_var[size];
STRING INITIALIZATION:
Once you declare an array, you can also assign the array initial values
when you declare it.
Case – I:
char name[9]=”COMPUTER” ;
Will create nine elements with the following
values. C O M P U T E R \0
Case –II:
char name[ ]=”COMPUTER” ;
Case –III:
char name[9]={‘C’ , ’O’ , ’M’ , ‘P’ , ‘U’ ,’T’ , ‘E’ , ‘R’ , ‘\0’ } ;
A string or group of strings can be read from input device using the
following functions.
1. scanf() with %s format .
2. gets() function.
3. getchar() function.
WRITING STRINGS:
strcat(string1, string2) ;
Ex: strcpy(string1,”Dinesh”);
strcpy(string2,” Singanamala”);
printf(“%s”, strcat(string1,string2));
Here the value of string1 becomes Dinesh Singanamala , but the value of
string2 Sinaganamala remains same.
There for the output of the above program segment
#include<stdio.h>
#include<string.h>
main()
{
char str1[20]=”Data” ;
char str2[20]=”Base”
; char str3[20] ;
clrscr();
str3=strcat(str1,str2);
printf(“After string concatenation : %s”, str3);
getch();
}
Output:
After string concatenation : DataBase
The syntax is :
strcmp (string1,string2)
Where string1 and string2 may be string variables or string constants.
Note:
Some compliers returns a negative value if string1 is less than string2
alphabetically and a positive value if string1 is greater than string2
alphabetically.
Ex:
1. strcmp(“ BALLARI” , “BALLARI”)
will return 0 because two strings are equal.
2. strcmp(“Their” , “There”)
will return -9 which is the numeric difference between Ascii i
and Ascii r
3. strcmp(“There” , “Their”)
will return 9 which is the numeric difference between Ascii r
and Ascii i
4. strcmp(“The” , “the”)
will return -32 which is the numeric difference between Ascii T
and Ascii t .
Ex: Write a C program to compare two string variable values using library
function strcmp().
#include<stdio.h>
#include<string.h>
main(()
{
char str1[20], str2[20];
clrscr();
printf(“Enter string1 \n”);
gets(str1);
printf(“Enter string2 \n”);
gets(str2);
if (strcmp(str1,str2)==0)
{
printf (“ Given two strings are equal\n”);
}
else
{
printf (“ Given two strings are not equal\n”);
}
getch();
}
Output 1:
Enter string1 Ballari
Enter string2 Ballari
Given two strings are equal
Output 2:
Enter string1 Ballari
Enter string2 Gadag
Given two strings are not equal
Where variable is any integer variable which receives the value of the length of
the string.
Ex: n=strlen(“Dinesh”);
Here the value of n is 6.
Ex: write a C program to find length of a given string using strlen() function.
#include<stdio.h>
#include<string.h>
main()
{
char name[20];
int length;
clrscr();
printf(“Enter given string\n”);
gets(name);
length=strlen(name);
printf(“Length of string=%d\n”, length);
getch();
}
Output:
Enter given string Computer
Length of string=8
Ex : char name[30];
strcpy(name, “ballari”);
In the above example, the characters “ballari”are assigned to the string called
name.
Ex: Write a C program to copy the content of one string to another string using
strcpy function.
#include<stdio.h>
#include<string.h>
main()
{
char orgnl[20],dupli[20];
clrscr();
printf(“ Enter a string\n”);
gets (orgnl);
srtcpy (dupli , orgnl);
printf(“ Given string =%s\n”, orgnl);
printf(“ Copied string=%s\n”, dupli);
getch();
}
Output:
Enter a string Ballari
134
Given string = Ballari
Copied string= Ballari
STRING REVERSE (STRREV):
strrev(string);
Ex: strrev(“ballari”);
#include<stdio.h>
#include<string.h>
main()
{
char str1[20], str2[20];
printf(“Enter Given word\n”);
gets(str1);
13
5
strcpy(str2,str1);
strrev(str2);
if(strcmp(str1,str2)==0)
printf(“ The given word is polindrom\n”);
else
printf(“The given word is not polindrom\n”);
getch();
}
Output 1:
Enter Given word malayalam
The given word is polindrom
Output 2:
Enter Given word ballari
The given word is not polindrom
13
6
This function converts all characters in a string from lowercase to
uppercase. The syntax is:
strupr(string);
Ex : strupr(“ballari”);
13
7
Exam Practice
13
8
III. Essay Answer Type Questions [15 Marks]
13
9
14
0