SlideShare a Scribd company logo
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 Array
float table [50][50];
char page[50][50];



int num[3][3];
      0    1   2
    0
    1
    2
Accessing two dimensional arrays
int 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);
String library functions
• 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 %sn”, s);
           printf(“target stirng is %sn”, t);
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 ();
}
Session 4
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 ) ;
}

More Related Content

PPTX
Data Structures - Lecture 3 [Arrays]
PDF
PPTX
Arrays in Data Structure and Algorithm
PPTX
Arrays in c
PPT
02 c++ Array Pointer
PPTX
C++ lecture 04
PPT
One dimensional 2
PPTX
Array in c++
Data Structures - Lecture 3 [Arrays]
Arrays in Data Structure and Algorithm
Arrays in c
02 c++ Array Pointer
C++ lecture 04
One dimensional 2
Array in c++

What's hot (20)

PPTX
Arrays 1D and 2D , and multi dimensional
PPTX
Array and string
PPT
Array in c
PPTX
Unit 6. Arrays
PPT
Data Structure Midterm Lesson Arrays
PPT
Arrays in C++
PPTX
Array ppt
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Arrays in c language
PPTX
Array Of Pointers
PPTX
C++ programming (Array)
PPTX
PPT
1 D Arrays in C++
PPTX
Programming in c Arrays
PPTX
Arrays in C language
PPTX
PPTX
PPT
C++ Arrays
PPT
Chap09
PPTX
Introduction to Array ppt
Arrays 1D and 2D , and multi dimensional
Array and string
Array in c
Unit 6. Arrays
Data Structure Midterm Lesson Arrays
Arrays in C++
Array ppt
Array Introduction One-dimensional array Multidimensional array
Arrays in c language
Array Of Pointers
C++ programming (Array)
1 D Arrays in C++
Programming in c Arrays
Arrays in C language
C++ Arrays
Chap09
Introduction to Array ppt
Ad

Similar to Session 4 (20)

PDF
Arrays and function basic c programming notes
PPTX
COM1407: Arrays
PPTX
unit-5 String Math Date Time AI presentation
PPTX
C (PPS)Programming for problem solving.pptx
PPTX
Programming in C (part 2)
PPTX
PPTX
Functions and Arraysfgdfgdfgdfgdfgdg.pptx
PPTX
ARRAY's in C Programming Language PPTX.
PPTX
C-Arrays & Strings (computer programming).pptx
PPTX
C programming basic concepts of mahi.pptx
PPTX
Basic Concepts of C Language.pptx
PPT
Arrays in c programing. practicals and .ppt
PPTX
PPTX
C Programming Unit-3
PDF
VIT351 Software Development VI Unit2
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
PDF
Arrays and strings in c++
Arrays and function basic c programming notes
COM1407: Arrays
unit-5 String Math Date Time AI presentation
C (PPS)Programming for problem solving.pptx
Programming in C (part 2)
Functions and Arraysfgdfgdfgdfgdfgdg.pptx
ARRAY's in C Programming Language PPTX.
C-Arrays & Strings (computer programming).pptx
C programming basic concepts of mahi.pptx
Basic Concepts of C Language.pptx
Arrays in c programing. practicals and .ppt
C Programming Unit-3
VIT351 Software Development VI Unit2
Esoft Metro Campus - Certificate in c / c++ programming
Arrays and strings in c++
Ad

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Electronic commerce courselecture one. Pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Empathic Computing: Creating Shared Understanding
PDF
KodekX | Application Modernization Development
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Electronic commerce courselecture one. Pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Empathic Computing: Creating Shared Understanding
KodekX | Application Modernization Development
The AUB Centre for AI in Media Proposal.docx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
20250228 LYD VKU AI Blended-Learning.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Digital-Transformation-Roadmap-for-Companies.pptx
Big Data Technologies - Introduction.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Review of recent advances in non-invasive hemoglobin estimation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf

Session 4

  • 1. 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.
  • 2. 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];
  • 3. Declaring single dimension Array • For example, to declare an integer array which contains 10 elements we can do as follows: int values[10];
  • 4. 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]);
  • 5. 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};
  • 6. 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];
  • 7. Declaring Multi Dimensional Array float table [50][50]; char page[50][50]; int num[3][3]; 0 1 2 0 1 2
  • 8. Accessing two dimensional arrays int 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}};
  • 9. 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).
  • 10. 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
  • 11. 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);
  • 12. String library functions • 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);
  • 13. 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 %sn”, s); printf(“target stirng is %sn”, t);
  • 14. 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);
  • 15. 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
  • 16. 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); }
  • 17. 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.
  • 18. 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.
  • 19. Function Example #include <stdio.h> void printMessage () { printf ("Programming is fun.n"); } void main () { printMessage (); }
  • 21. 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);
  • 22. 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.
  • 23. 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)); }
  • 24. 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 ) ; }