ARRAY in C • Array is a collection or group of similar data type elements stored in contiguous memory. • The individual data items can be characters, integers, floating points numbers and so on . • Here contiguous memory allocation means array occupies contiguous bytes as needed in the memory.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
ARRAY in C • Array is a collection or group of similar data type
elements stored in contiguous memory.
• The individual data items can be characters, integers, floating points numbers and so on .
• Here contiguous memory allocation means array occupies contiguous bytes as needed in the memory.
Declaring single dimension Arrays
• We can declare an array by specify its data type, name and the number of elements the array holds between square brackets immediately following the array name.
• Here is the syntax:data_type array_name[size];
Declaring single dimension Array• For example, to declare an integer array which
contains 10 elements we can do as follows:
int values[10];
Accessing Single Dimension Array• We can access array elements via
indexes array_name[index]. Indexes of array starts from 0
• With each subscript enclosed in square brackets.
• For example,values[0]=5;values[1]=10;printf(“%d”,values[1]);
Initializing Arrays• It is like a variable, an array can be initialized.
• To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name.
• Here is an example of initializing an integer array.
int list[5] = {2,1,3,7,8};
Multi dimensional Arrays:• Often there is a need to store and manipulate two
dimensional data structure such as matrices & tables.
• Here the array has two subscripts. One subscript denotes the row & the other the column.
• The declaration of two dimension arrays is as follows:data_type array_name[row_size][column_size]; int m[10][20];
Declaring Multi Dimensional Arrayfloat table [50][50]; char page[50][50];
int num[3][3]; 0 1 2012
Accessing two dimensional arraysint values[2][2];values[0][0]=1; values[0][1]=2;values[1][0]=3; values[1][1]=4;
int num[2][2]={1,2,3,4};int matrix[3][3] ={{11,12,13}, {21,22,23}, {32,31,33}};
ARRAY AND STRINGS • The gets and puts functions:
• The gets and puts functions facilitate the transfer of strings between the computer and the standard input/output devices.
• Each of these functions accepts a single argument. The argument must be a data item that represents a string(e.g. character array).
Example of using gets and puts#include<stdio.h> void main() {
char inne[80]; gets(line); puts(line);
} The gets and puts functions offer simple alternatives to the use of scanf and printf for reading and displaying strings
String library functions• strlen() • This function counts a number of characters
present in a string while giving a call to the function.
char msg[] = “Lord krishna”; int n; n=strlen(msg); printf(“length of string =%d”, n);
• strcpy() • This function copies the contents of one string
to another. • The base address of source and target strings
are supplied to the function.
char source[] = “Lord Krishna”; char target[15]; strcpy(target, source); printf(“Source string is : %s”, source); printf(“target string is :%s”,target);
String library functions
• strcat() • This function concatenates the source string
at the end of target sting. char s[] = “Lord”; char t[] = “Krishna”; strcat(t,s); printf(“source string is %s\n”, s); printf(“target stirng is %s\n”, t);
String library functions
String library functions• strcmp() • This function compares two strings and returns some
integer value. • If both the strings are equal then it returns 0 otherwise
it returns some other integer value.void main() {
char s1[]=”Jerry”; char s2[]=”Ferry”; int j= strcmp(s1, s2); printf(“%d”, j);
}
Understanding Functions• What is Function?• A function in C language is a block of code that
performs a specific task.
• It is reusable i.e. it can be executed from as many different parts in a C Program as required.
• It also optionally returns a value to the calling program
Structure of a Function<return type> FunctionName (Argument1, Argument2, Argument3……){
Statement1;Statement2;
}int sum (int x, int y){
int result;result = x + y;return (result);
}
Types of functions:A function may belong to any one of the following categories:
• Functions with no arguments and no return values.
• Functions with arguments and no return values.• Functions with arguments and return values.• Functions that return multiple values.• Functions with no arguments and return values.
Advantages of using functions:• It makes possible top down modular
programming.
• The length of the source program can be reduced.
• It becomes uncomplicated to locate and separate a faulty function for further study.
• c programmer can use function written by others, instead of starting over from scratch.
Function Example#include <stdio.h>void printMessage (){
printf ("Programming is fun.\n");}void main (){
printMessage ();}
Function Which Return Values#include <stdio.h>int add (int x,int y){
int result; result=x+y;return(result);
}void main (){
int total;total=add(10,12);
}
Recursive Function• Recursive function is a function which
contains a call to itself.
• Recursive function allows you to divide your complex problem into identical single simple cases which can handle easily.
• This is also a well-known computer programming technique: divide and conquer.
Example of recursive function# include<stdio.h>int factorial(int number){
if(number <= 1) return 1; return number * factorial(number - 1);}void main(){ int x = 5; printf("factorial of %d is %d",x,factorial(x));}
Scope Rule of Functions void main( ) {
int i = 20 ; display ( i ) ;
} void display ( int j ) {
int k = 35 ; printf ( "\n%d", j ) ; printf ( "\n%d", k ) ;