Cmodule 3
Cmodule 3
C Array
C array is beneficial if you have to store similar elements. For example, if we want to store
the marks of a student in 6 subjects, then we don't need to define different variables for
the marks in the different subject. Instead of that, we can define an array which can store
the marks in each subject at the contiguous memory locations.
Properties of Array
Each element of an array is of same data type and carries the same size.
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
Disadvantage of C Array
Declaration of C Array
data_typearray_name[array_size];
example
int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size.
Initialization of C Array
The simplest way to initialize an array is by using the index of each element. We
can initialize each element of the array by using the index. Consider the following
example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
2
C Array: Declaration with Initialization
We can initialize the c array at the time of declaration. Let's see the code.
int marks[5]={20,30,40,50,60};
If we initialize an array using an initializer list, we can skip declaring the size of the
array as the compiler can automatically deduce the size of the array in these cases.
#include<stdio.h>
voidmain(){
int i=0;
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
getch();
Output
20
30
40
3
50
60
Program to take values from the user and store them in an array
#include <stdio.h>
int main() {
int marks[10], i, n;
scanf("%d", &n);
scanf("%d", &marks[i]);
printf("%d\n", marks[i]);
getch(); }
4
Declaration of Two-Dimensional Array in C
The basic form of declaring a 2D array with x rows and y columns in C is shown
below.
Syntax:
data_typearray_name[x][y];
where,
Example:int x[10][20];
First Method:
The above array has 3 rows and 4 columns. The elements in the braces from left
to right are stored in the table also from left to right. The elements will be filled in
the array in order: the first 4 elements from the left will be filled in the first row,
the next 4 elements in the second row, and so on.
5
This type of initialization makes use of nested braces. Each set of inner braces
represents one row. In the above example, there is a total of three rows so there
are three sets of inner braces. The advantage of this method is that it is easier to
understand.
We can use any C loop to initialize each member of a 2D array one by one as
shown in the below example.
Example:
int x[3][4];
x[i][j] = i + j;
This method is useful when the values of each element have some sequential
relation.
Elements in 2D arrays are accessed using row indexes and column indexes. Each
element in a 2D array can be referred to by:
Syntax:
array_name[i][j]
where,
Example:
6
int x[2][1];
The above example represents the element present in the third row and second
column.
The two-dimensional array can be declared and defined in the following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
program:
#include<stdio.h>
voidmain(){
int i=0,j=0;
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++)
} }getch(); }
String is a sequence of characters that are treated as a single data item and
terminated by a null character '\0'.
A string is actually a one-dimensional array of characters in C language.
These are often used to create meaningful and readable programs.
The C String is stored as an array of characters.
7
C String Declaration Syntax
char string_name[size];
In the above syntax string_name is any name given to the string variable and size is
used to define the length of the string, i.e the number of characters strings will
store.
C String Initialization
String literals can be assigned without size. Here, the name of the string str acts as
a pointer because it is an array.
String literals can be assigned with a predefined size. But we should always account
for one extra space which will be assigned to the null character.
We can also assign a string character by character. But we should remember to set
the end character as ‘\0’ which is a null character.
We can assign character by character without size with the NULL character at the
end. The size of the string is determined by the compiler automatically.
8
C String Example:
Read a String Input From the User(C Input Output (I/O)-printf and scanf
example:)
#include<conio.h>
#include<string.h>
void main()
{ char name[20];
scanf("%s", name);
getch();}
Example:
#include<stdio.h>
#include <string.h>
void main(){
char name[50];
getch();}
C language supports a large number of string handling functions that can be used to
carry out many of the string manipulations. These functions are packaged in the
string.h library. Hence, you must include string.h header file in your programs to
use these functions.
The following are the most commonly used string handling functions.
Method Description
Syntax of srtlen():
Length=strlen(Stringname);
Example of strlen():
#include <stdio.h>
#include <string.h>
void main() {
int len;
10
char string[] = "Rekha";
len = strlen(string);
getch();
Syntax of srtcpy():
strcpy(DestinationString,Sourcetring);
Example :
#include <stdio.h>
#include <string.h>
void main() {
char str2[20];
strcpy(str2, str1);
puts(str2);
getch();}
Output:C programming
11
strcat(): This function is used to concatenate two strings.
Syntax of strcat():
strcat(String1,String2);
Example Code:
#include<stdio.h>
#include <string.h>
int main() {
strcat(str1, str2);
Syntax of strcmp():
Example Code:
#include <stdio.h>
#include <string.h>
void main() {
if (result == 0){
12
printf("Strings are equal");}
} else {
} getch();}
Syntax of strrev():
char strrev(str);
Example Code:
#include <stdio.h>
#include<string.h>
void main()
{ char s1[50];
gets(s1);
getch();
Output:llew
13
5. strupr(): It takes a string as input and converts all the letters in the string to
uppercase.
Syntax of strupr():
strupr(str);
Example Code:
#include <stdio.h>
#include <string.h>
int main() {
strupr(str);
return 0;}
6. strlwr(): It takes a string as input and converts all the letters in the string to
lowercase.
Syntax of strlwr():
strlwr(str);
Example Code:
#include <stdio.h>
#include <string.h>
voidmain() {
14
char str[] = "Hello”;
strlwr(str);
void main(){
int i=0;
int marks[5]={10,30,40,20,50};
for(i=0;i<5;i++){
printf("%d",marks[i]);
getch();
2. Program to take N values from the user and store them in an array
#include <stdio.h>
int main() {
int marks[10], i, n;
15
printf("Enter number of elements: ");
scanf("%d", &n);
scanf("%d", &marks[i]);
printf("%d\n", marks[i]);
getch();
void main() {
scanf("%d", &n);
printf("enter elements");
for(i=0;i<n;++i)
scanf("%d", &marks[i]);
16
printf("Displaying integers: ");
for(i= 0 i<n;++i) {
printf("%d\n", marks[i]);
sum += marks[i];
getch();
void main() {
double average;
scanf("%d", &n);
printf("enter elements");
for(i=0;i<n;++i)
scanf("%d", &marks[i]); }
for(i= 0 i<n;++i) {
17
printf("%d\n", marks[i]);
sum += marks[i];
getch();
5. Sorting an array
#include<stdio.h>
void main ()
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
temp = a[i];
a[i] = a[j];
a[j] = temp;
18
}
printf("%d\n",a[i]);
#define N 1000
void main() {
int arr[N];
int ni,j;
scanf("%d", &n);
scanf("%d", &arr[i]);
19
// Printing the reverse of the array
getch();
void main()
scanf("%d", &n);
scanf("%d", &arr[i]);
small = arr[0];
large = arr[0];
20
if (arr[i] > large)
large = arr[i];
else
small = arr[i];
getch();
void main ()
int arr[3][3],i,j;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
21
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
for(i=0;i<3;i++)
printf("\n");
for (j=0;j<3;j++)
printf("%d\t",arr[i][j]);
} getch();
int main() {
int c[3][3];
int i, j;
22
c[i][j] = a[i][j] + b[i][j];
printf("\n"); }
getch();
#include <stdio.h>
int main()
23
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
scanf("%d", &second[c][d]);
printf("%d\t", sum[c][d]);
printf("\n");
return 0;
1. Start.
24
3. Enter the value of p and q (or) order of the second matrix.
6. If the number of columns of the first matrix is not equal to the number of
rows of the second matrix, print matrix multiplication is not possible and exit. If
not, proceed to the next step.
9. Set an inner loop for the above loop from j=0 to j=q.
10. Initialise the value of the element (i, j) of the new matrix to 0.
11. Set an inner loop inside the above loop from k=0 to k=p.
12. Using the add and assign operator (+=) store the value of a[i][k] * b[k][j] in
the third matrix, c[i][j].
14. Stop.
#include<stdio.h>
void main() {
c[i][j] = 0;
printf("%d\t", c[i][j]);
printf("\n"); }getch();}
26