R23 CP Unit-3
R23 CP Unit-3
Array:
An array is defined as the collection of similar type of data items stored at contiguous
memory locations. Arrays are the derived data type in C programming language which can
store the primitive type of data such as int, char, double, float, etc.
Syntax: datatype arrayname[size];
The array contains the following properties.
➢ Each element of an array is of same data type and carries the same size, i.e., int = 2
bytes.
➢ Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
➢ Elements of the array can be randomly accessed.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn
later.
Declaration of C Array:
We can declare an array in the c language in the following way.
Syntax: data_type array_name[array_size];
Now, let us see the example to declare the array.
Example: int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size.
Initialization of array:
int marks[ ] = { 80, 60, 70, 85, 75 };
or
int marks[5] = { 80, 60, 70, 85, 75 };
or
int marks[5];
marks[0] = 80;
marks[1] = 60;
marks[2] = 70;
marks[3] = 85;
marks[4] = 75;
80 60 70 85 75
marks[0] marks[1] marks[2] marks[3] marks[4]
Example:
#include<stdio.h>
void main( )
{
int i=0;
int marks[5] = { 80, 60, 70, 85, 75 };
for(i=0;i<5;i++)
{
printf("%d " , marks[ i ] );
}
}
Page 1
Programming for Problem Solving using C UNIT-3
Example:
#include<stdio.h>
void main()
{
int i=0;
int marks[5];
marks[0] = 80;
marks[1] = 60;
marks[2] = 70;
marks[3] = 85;
marks[4] = 75;
for(i=0;i<5;i++)
{
printf("%d " , marks[ i ] );
}
}
Example:
#include<stdio.h>
void main()
{
int i=0;
int marks[5] = {80, 60, 70, 85, 75};
printf("%d ",marks[0]);
printf("%d ",marks[1]);
printf("%d ",marks[2]);
printf("%d ",marks[3]);
printf("%d ",marks[4]);
}
Page 2
Programming for Problem Solving using C UNIT-3
#include <stdio.h>
int main()
{
int a[10], i, n, big;
Page 3
Programming for Problem Solving using C UNIT-3
Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the declaration and
initialization are being done simultaneously. However, this will not work with 2D arrays. We
will have to define at least the second dimension of the array. The two-dimensional array can
be declared and defined in the following way.
int arr[2][3]={{1,2,3},{2,3,4}};
Example:
#include<stdio.h>
void main( )
{
int i,j;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf(" %d" , arr[ i ][ j ]);
}
printf("\n");
}
}
Page 4
Programming for Problem Solving using C UNIT-3
Example:
#include<stdio.h>
void main( )
{
int i,j;
int arr[4][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 2;
arr[1][1] = 3;
arr[1][2] = 4;
arr[2][0] = 3;
arr[2][1] = 4;
arr[2][2] = 5;
arr[3][0] = 4;
arr[3][1] = 5;
arr[3][2] = 6;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",arr[i][j]);
}
printf("\n");
}
}
#include<stdio.h>
void main()
{
int a[10][10], b[10][10], c[10][10];
int i, j, m1, n1, m2, n2;
Page 5
Programming for Problem Solving using C UNIT-3
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j] = a[i][j] + b[i][j] ;
}
}
printf("Addition is: \n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
}
Page 6
Programming for Problem Solving using C UNIT-3
if( n1!=m2)
printf("Multiplication is Not Possible");
else
{
printf("Enter matrix A value: \n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d", &a[i][j]);
}
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
c[i][j]=0;
for(k=0;k<n1;k++)
{
c[ i ][ j ] = c[ i ][ j ] + ( a[ i ][ k ] * b[ k ][ j ] );
}
}
}
Page 7
Programming for Problem Solving using C UNIT-3
Page 8
Programming for Problem Solving using C UNIT-3
Strings:
In C programming, a string is a sequence of characters terminated with a null character \0.
Declaration of Strings:
To declare Strings we must use one dimensional array of characters terminated by null
character \0.
Syntax:
char str_name [ size ] ;
In the above syntax str_name is any name given to the string variable and size is used
define the length of the string, i.e., the number of characters strings will store. Please keep in
mind that there is an extra terminating character which is the Null character („\0‟) used to
indicate termination of string which differs strings from normal character arrays.
Initialization of Strings:
There are many number of ways to initialization of strings.
char c[] = "abcd";
char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '\0'};
char c[5] = {'a', 'b', 'c', 'd', '\0'};
Index 0 1 2 3 4
Value a b c d \0
Address 100 101 102 103 104
Display Strings:
To display strings we can use %s as format Specofier.
Example:
#include<stdio.h>
void main()
{
char c[5] = "abcd"; Output:
printf("The String is %s", c); The String is abcd
}
Page 9
Programming for Problem Solving using C UNIT-3
Page 10
Programming for Problem Solving using C UNIT-3
In Example-2, we have used fgets ( ) function to read a string from the user.
fgets ( name, sizeof(name), stdin );
The sizeof(name) results to 20. Hence, we can take a maximum of 20 characters as input
which is the size of the name string.
String Functions in C library:
There are so many predefined functions regarding string operations. To use these functions
we must include “string.h” header file. And these functions should be write in lowercase.
Page 11
Programming for Problem Solving using C UNIT-3
Example-1:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "WeLcOmE";
printf (" String is %s", mystr);
printf ("\n Length is %d", strlen(mystr));
printf ("\n Uppercase is %s", strupr(mystr));
printf ("\n Lowercase is %s", strlwr(mystr));
return 0;
}
Example-2:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "welcome";
char mystr2[30] = "WELCOME";
if(strcmp(mystr,mystr2)==0)
{
printf("Both are Equal");
}
else
{
printf("Both are Not Equal");
}
return 0;
}
Example-3:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30];
char mystr2[30] = "welcome";
strcpy(mystr,mystr2);
printf("%s",mystr);
return 0;
}
Example-4:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30]="welcome";
char mystr2[30] = " to cse";
strcat(mystr,mystr2);
printf("%s",mystr);
return 0;
}
Page 12
Programming for Problem Solving using C UNIT-3
Example-5:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30]="welcome to cse";
printf("%s",strchr(mystr,'c'));
return 0;
}
Example-6:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30]="welcome to cse";
printf("%s",strrchr(mystr,'c'));
return 0;
}
Example-7:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30]="welcome to cse";
printf("%s",strrev(mystr));
return 0;
}
MORSE CODE:
Morse code is a method of transmitting text information as a series of on-off tones,
lights, or clicks that can be directly understood by a skilled listener or observer without
special equipment. It is named for Samuel F. B. Morse, an inventor of the telegraph.
The algorithm is very simple. Every character in the English language is substituted
by a series of „dots‟ and „dashes‟ or sometimes just singular „dot‟ or „dash‟ and vice versa.
Every text string is converted into the series of dots and dashes. For this every
character is converted into its Morse code and appended in encoded message. Here we have
copied space as it is. And we have not considered numbers, only alphabets.
Page 13
Programming for Problem Solving using C UNIT-3
Program:
#include<stdio.h>
#include<string.h>
void main()
{
char name[50],morse[250];
int i;
printf("Input: ");
gets(name);
printf("\nOutput : ");
for(i=0;i<strlen(name);i++)
{
switch(name[i])
{
case 'A': printf(".-");break;
case 'B': printf("-...");break;
case 'C': printf("-.-.");break;
case 'D': printf("-..");break;
case 'E': printf(".");break;
case 'F': printf("..-.");break;
case 'G': printf("--.");break;
case 'H': printf(". .. ");break;
case 'I': printf("..");break;
case 'J': printf(".---");break;
case 'K': printf("-.-");break;
case 'L': printf(".-..");break;
case 'M': printf("--");break;
case 'N': printf("-.");break;
case 'O': printf("---");break;
case 'P': printf(".--.");break;
case 'Q': printf("--.-");break;
case 'R': printf(".-.");break;
case 'S': printf("...");break;
case 'T': printf("-");break;
case 'U': printf("..-");break;
case 'V': printf("...-");break;
case 'W': printf(".--");break;
case 'X': printf("-..-");break;
case 'Y': printf("-.--");break;
case 'Z': printf("--..");break;
}
}
}
Page 14
Programming for Problem Solving using C UNIT-3
Enumerated types:
The enumerated type is a user defined type based on the standard integer type. In this
each integer is given as identifier called an enumeration constant.
Declaration of enumerated type:
In enumerate type; we declare its identifier and its values. Because it is derived form
the integer type, its operations are the same as for integers.
Syntax: enum typeName {identifier list};
Example: enum color {red, blue, green, white};
Assigning values to enumerated types:
An enumerated variable can hold only declared values for the type.
Example:
#include<stdio.h>
enum week { Mon, Tue, Wed, Thur, Fri, Sat, Sun };
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Output: 2
Structures:
Structure is a collection of related elements, possibly of different types, having a
single name.
Structure Definition in C
Keyword struct is used for creating a structure.
Page 15
Programming for Problem Solving using C UNIT-3
Syntax of structure
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
Page 16
Programming for Problem Solving using C UNIT-3
Page 17
Programming for Problem Solving using C UNIT-3
struct
{
member 1;
member 2;
}name2;
member m;
}name1;
For accessing the member 1 if inner structure we write as, name1.name2.member1;
Size of a Structure:
We normally use structures, and arrays to create variables of large sizes. The actual size
of these variables in terms of bytes may change from machine to machine. We may use the
unary operator sizeof to tell us the size of a structure (or any variable).
Sizeof (struct x)
Will evaluate the number of bytes required to hold all the members of the structure x.
Page 18
Programming for Problem Solving using C UNIT-3
Program: #include<stdio.h>
main()
{
struct stinfo
{
char name[20];
char pin[18];
char address[15];
int total_marks;
}a;
printf(“ The size of the structure is %d:”,sizeof(a));
}
member n;
}
Page 19
Programming for Problem Solving using C UNIT-3
Page 20