0% found this document useful (0 votes)
97 views

Array C Language

This document discusses arrays and strings in C programming. It defines arrays as collections of elements of the same data type that are stored sequentially in memory. There are two types of arrays: single dimensional arrays which are linear lists, and multi-dimensional arrays with two or more indices. Strings are arrays of characters that are terminated with a null character. The document provides examples of declaring, initializing, and manipulating single dimensional, multi-dimensional, and string arrays using various functions from the standard library. It also gives an overview of functions in C including definition, declaration, and calls.

Uploaded by

Ankush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Array C Language

This document discusses arrays and strings in C programming. It defines arrays as collections of elements of the same data type that are stored sequentially in memory. There are two types of arrays: single dimensional arrays which are linear lists, and multi-dimensional arrays with two or more indices. Strings are arrays of characters that are terminated with a null character. The document provides examples of declaring, initializing, and manipulating single dimensional, multi-dimensional, and string arrays using various functions from the standard library. It also gives an overview of functions in C including definition, declaration, and calls.

Uploaded by

Ankush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MODULE-IV

ARRAY
• Array is a collection of elements of same data type.
• The elements are stored sequentially one after the other in memory.
• Any element can be accessed by using
→ name of the array
→ position of element in the array
• Arrays are of 2 types:
1) Single dimensional array
2) Multi dimensional array
SINGLE DIMENSIONAL ARRAY
• A single dimensional array is a linear list consisting of related elements of same type.
• In memory, all the elements are stored in continuous memory-location one after the other.

Declaration of Single Dimensional Arrays


• The syntax is shown below:
Type variable_name[size] where
data_type can be int, float etc
variable_name is name of the array
size indicates number of elements in the array it should be numeric or symbolic constant.
• For ex:
int age[5];
• The above code can be pictorially represented as shown below:

• Note that, the first element is numbered 0 and soon.

Initialization of one dimensional array

• After an array is declared, its elements must be initialized. Otherwise they will contain the garbage
values.
• Array initialization can be done in 2 ways.
1) Initialization at compile time
2) Initialization at run time
Initialization of 1D-array at compile time
The general form of initializing an array at the compile time is as follows,
Type array_name[size]={list of values};
• Example, int number[3]={1,2,3};
• The values in the list are separated by commas.
• Character arrays may be declared in the same manner
char name[]={‘J’,’O’,’H’,”N’,’\0’};
• String arrays may be declared as char
name[]=”JOHN”;

Initialization of 1D-array at run time


• Array can be explicitly initialized during run time. This technique is used to initialize large arrays.
• Example, to read and write a single dimensional array.
#include<stdio.h> OUTPUT
void main() Enter 5 numbers
{ 2 4 34 3 4
int age[5], i; Value in array
age[0]=2
printf("enter 5 numbers:\n”); age[1]=4
for(i=0;i<5;i++) age[2]=34
{ age[3]=3
scanf("%d", &age[i]); age[4]=4
}
printf("Value in array\n”);
for(i=0;i<5;i++)
{
printf("age[%d] : %d \n ", i, age[i]);
}
}

TWO DIMENSIONAL ARRAY


• Arrays with two or more dimensions are called multidimensional arrays.
• For ex:
int matrix[2][3];
• The above code can be pictorially represented as shown below:

• A two dimensional array is used when elements are arranged in a tabular fashion.
• Here, to identify a particular element, we have to specify 2 indices:
• → First index identifies the row number of the element and
• → Second index identifies the column number of the element
Declaration of Two Dimensional Arrays
• The syntax is shown below:
data_typearray_name[row_size][column_size];
• For ex:
int matrix[2][3];
• The above code can be pictorially represented as shown below:

Initialization of Two Dimensional Arrays

• Can be done by separating the values of each row and column in the braces.
int table[2][3]= {{1, 23, 11}, {44, 5, 6}};
• Two dimensional array can also be initialized in the form of matrix.
int table[2][3]= { {1, 23, 11},{44, 5, 6}};
• The above code can be pictorially represented as shownbelow:
#include<stdio.h>
void main()
{
int matrix[2][3], i, j;
printf("enter elements of 2*3 matrix: \n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("elements of the matrix are: \n”); for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d \t", matrix[i][j]);
}
printf(“\n”);
}
}

STRINGS
• Array of character are called strings.
• A string is terminated by null character/0.
• For ex:
"c string tutorial"
• The above string can be pictorially represented as shown below:

STRING VARIABLE
• There is no separate data type for strings in C.
• They are treated as arrays of type “char‟.
• So, a variable which is used to store an array of characters is called a string variable.
Declaration of String
• Strings are declared in C in similar manner as arrays. Only difference is that, strings are of “char” type.
• The general form of declaring string variable is as follows,
char string_name[size];
• For ex:
chars[5]; //string variable name can hold maximum of 5 characters including NULL character
• The above code can be pictorially represented as shown below:
Initialization of String
• C permits initialization of character array in two ways
• For ex:
char s[5]={'r', 'a', 'm', 'a',’\0’};
char s[5]="rama";
• The above code can be pictorially represented as shown below:

Reading a string from the keyboard


• The strings can be read from the keyboard and can be displayed onto the monitor using following 2
formatted functions:
Formatted input function: scanf()
Formatted output function: printf()
Example:
#include<stdio.h>
void main()
{
char name[20];
printf(“enter your name: \n”);
scanf(“%s”,name);
printf(“welcome:”);
printf(“%s”,name);
}
Writing a string to the terminal
• We can use the printf() function to display the string of text on the output screen.
• The below example program stores the string “United kingdom” in the array country and display the
string under various format specifications.
• The following points are to be considered while using the printf statements,
1) when the field width is less than the length of the string, the entire string is printed.
2) The integer value on the right side of the decimal point specifies the number of character to be
printed.
3) When the number of characters to be printed is specified as zero, nothing is printed.
4) the – sign in the specification causes the string to be printed left justified.
5) The specification % .ns prints the first n characters of the string.
#include<stdio.h> output:
void main() united kingdom
{ united kingdom
char country[15]=”united kingdom”; united
printf(“\n\n”);
united
printf(“%15s\n”,country);
printf(“%5s\n”,country);
uni
printf(“%15.6s\n”,country); united kingdom
printf(“%-15.6s\n”,country);
printf(“%15.0s\n”,country);
printf(“%.3s\n”,country);
printf(“%s\n”,country);
}

STRING MANIPULATION FUNCTIONS FROM THE STANDARD LIBRARY


• Strings are often needed to be manipulated by programmer according to the need of a problem.
• There are numerous functions defined in <string.h> header file.
strlen()
• This function calculates the length of string. It takes only one argument, i.e.,string-name.
• The syntax is shown below:
temp_variable = strlen(string_name);
strcpy()
• This function copies the content of one string to the content of another string. It takes 2arguments.
• The syntax is shown below:
strcpy(destination,sourc);
where source and destination are both the name of the string.
strcat()

•This function joins 2 strings. It takes two arguments, i.e., 2 strings and resultant string is stored in
the first string specified in the argument.
• The syntax is shown below:
strcat(first_string,second_string);
strcmp()
• This function compares 2 string and returns value 0, if the 2 strings are equal. It takes 2 arguments,
i.e., name of two string to compare.
• The syntax is shown below:
temp_varaible=strcmp(string1,string2);

FUNCTIONS:
• A function is a block of code to perform a specific task.

• The elements of user defined functions are


➢ Function definition
➢ Function declaration
➢ Function call
Function Definition
• Function definition contains programming codes to perform specific task.
• function_type function_name(formal parameter list)
{
local_variable declaration;
executable statements;
executable statements;
-
-
-
return statements;
}

• Function declaration consists of 4 parts


➢ Function type
➢ Function name
➢ Parameter list
➢ Terminating semicolon
• The format can be as follows function_type function_name(parameter list);
• The syntax is shown below:
return_type function_name(type(1) argument(1),..,type(n) argument(n))
{
//body of function
}

Function Call
• Control of the program cannot be transferred to user-defined function unless it is called/invoked.
• The syntax is shown below:
function_name(argument(1),. ..argument(n));

Example:
#include<stdio.h>

void display(); //function declaration

void main()
{
display(); // function call
}

void display() //function definition


{
printf(“C program\n”);
return;
}

ACTUAL AND FORMAL ARGUMENTS


Argument (or parameter) refers to data that is passed to function (function definition) while calling
function.
Arguments listed in function calling statements are referred to as actual arguments.
The arguments used in the function declaration are referred as formal arguments.
#include <stdio.h>
int add(int a, int b); Output:
int main() Enters two number
{ to add 2 3
int a,b,sum; sum=5
printf("Enters two number to add \n");
scanf("%d %d", &a,&b);
sum=add(a,b); //actual arguments
printf("\n sum=%d", sum);
return 0;
}
int add(int a,int b) //formal arguments
{
int sum; sum=a+b; return sum;
}
CLASSIFICATION OF USER-DEFINED FUNCTIONS

Function with no arguments and no return value


Function with no arguments and return value
Function with arguments but no return value
Function with arguments and return value
Example: Program to illustrate function with no arguments and no return value.

#include <stdio.h>
void add();
void main()
{
add();
}
void add()
{
int a, b, sum;
printf("Enters two number to add : ");
scanf("%d %d", &a, &b);
sum=a+b;
printf("\n sum=%d", sum);
return;
}

Example: Program to illustrate function with no arguments and return value


#include <stdio.h>
int add();
void main()
{
int sum;
sum=add();
printf("\n sum=%d", sum);
}
int add()
{
int a, b, sum;
printf("Enters two number to add \n");
scanf("%d %d", &a, &b);
sum=a+b;
return sum;
}

Example: Program to illustrate function with arguments but no return value

#include <stdio.h>
void add(int a, int b);
void main()
{
int a, b, sum;
printf("Enters two number to add \n");
scanf("%d %d", &a, &b);
add(a, b);
}
void add(int a, int b)
{
sum= a+ b;
printf("\n sum=%d", sum);
return;
}

Example: Program to illustrate function with arguments and return value


#include <stdio.h>
int add(int a, int b);
int main()
{
int a, b, sum;
printf("Enters two number to add \n");
scanf("%d %d", &a, &b);
sum=add(a,b);
printf("\n sum=%d", sum);
return 0;
}
int add(int a, int b)
{
int sum;
sum=a+b;
return sum;
}

RECURSION
• A function that calls itself is known as recursive function.

#include <stdio.h>
int fib(int n) Output:
{ Enter the series size: 10
The Fibonacci series are
if(n==0||n==1) 0 1 1 2 3 5 8 13 21 34
return n;
else
return (fib(n-1)+fib(n-2)); /*self call to function
}
void main()
{
int series_size,i;
printf("Enter the series size: ");
scanf("%d",&series_size);
printf(“the Fibonacci series are\n”);
for(i=0;i<series_size;i++)
printf(“%d\t”,fib(i));
return 0;
}
Example: To find the factorial of a number using recursion

#include <stdio.h>
float fact(int n) Output:
{ Enter positive integer 3
if(n==0) factorial of 3=6.000000
return 1;
else
return n*fact(n-1); /*self call to function
}
void main()
{
int num;
float res;
printf("Enter a positive integer: ");
scanf("%d",&num);
res=fact(num);
printf("factorial of %d=%f", num,res);
}

You might also like