1
2
3
4
5
6
7
8
9
10
11
12
13
14
Passing ‘One dimensional
Array’ {
PRAMEESHA
I CSBS
C Programming Language
[As an ARGUMENT]
}
C PROGRAMMING 28/12/2024
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
What is {ARRAY}
●Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
●To create an array, define the data type (like int) and specify the name of the array
followed by square brackets [].
●To insert values to it, use a comma-separated list inside curly braces, and make sure all
values are of the same data type:
● 1. One Dimensional Array
● 2. Multi-Dimensional Array
● 3. Two Dimensional Array
}
C Programming Language
PRAMEESHA - CSBS 28/12/2024
1
2
3
4
5
6
7
8
9
10
11
12
13
14
One Dimensional Array
C Programming Language
PRAMEESHA - CSBS 28/12/2024
●An array where data is arranged in a single dimension is called one
dimensional array.
Syntax : datatype array_name[size];
Here, array_name is an array of type datatype and size is the number of
elements in that array.
For example,
int a[5]; // declares an integer array with 5 elements
float f[5]; // declares an float array with 5 elements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Two Dimensional Array
C Programming Language
PRAMEESHA - CSBS 28/12/2024
●An array where data is arranged in two dimensions is called two dimensional array.
Syntax : datatype array_name[d1][d2];
Here, array_name is an array of type datatype and it has 2 dimensions. The
number of elements in array_name is equal to d1*d2.
For example,
int i[5][5]; // declares an integer array with 25 elements
float f[5][10]; // declares an float array with 50 elements
char n[5][10]; // declares an character array with 50 elements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Accessing elements of array
C Programming Language
PRAMEESHA - CSBS 28/12/2024
●Elements of an array can be accessed by using the name of array and index of
element to be accessed
For example, consider a one dimensional array
int x[10];
Now, fifth element of this array can be accessed as x[4].
Note: First element of array has index 0, so the index of nth element is
n-1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Functions in C-Programming
C Programming Language
PRAMEESHA - CSBS 28/12/2024
●we can divide a large program into the basic building blocks known as function.
User-defined functions: are the functions which are created by the C programmer, so
that we can use it many times. It reduces the complexity of a big program and optimizes the
code.
Elements of user defined function
• Function declaration (or) Function prototype
• Function call
• Function definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Function declaration / Function prototype
C Programming Language
PRAMEESHA - CSBS 28/12/2024
●All identifiers in C need to be declared before they are used.
●This is true for functions as well as variables.
●For functions declaration needs to be before the first call of the function.
●A function declaration is also known as function prototype.
●It consists of four parts
1. Return type
2. Function name
3. Parameter list
4. Terminating semicolon
return_typefunction_name(arguments list);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Function Definition
C Programming Language
PRAMEESHA - CSBS 28/12/2024
Function Definition
Function definition consists of two parts
1. Function header
2. Function body
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Array as function parameter
C Programming Language
PRAMEESHA - CSBS 28/12/2024
Declaration of function parameter
• return_type function_name (datatype [], [other arguments type]);
• return_type function_name (datatype [size], [other arguments type]);
Declaration of function named parameter
return_type function_name (datatype array_name[ ], [other arguments])
{
body of function;
}
return_type function_name (datatype array_name[size], [other arguments])
{
body of function;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Passing an array as function parameter
C Programming Language
PRAMEESHA - CSBS 28/12/2024
#include<stdio.h>
void func(int[], int); // function declaration
int main()
{
int a[5]={1,2,3,4,5}; func(a,5); // function call
return 0;
}
void func(int x[], int size) // function definition
{
int i;
for(i=0;i<5;i++)
printf("%d",x[i]);
}
In this program an array x is passed as parameter from main() to a function func(). The contents of
array are printed using a for loop. OUTPUT – 1 2 3 4 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
C Programming Language
PRAMEESHA - CSBS 28/12/2024
#include<stdio.h>
int avg(int [],int);///Function Declarationint
main(){
int marks[5]={10,20,30,40,50};
int size,average;
size=sizeof(marks)/sizeof(marks[0]);///Number of elements average=avg(marks,size);
///Function call
printf("Average of marks = %dn",average);
printf("Number of Elements = %dn",size);
return 0;
}
int avg(int marks1[],int size)///Function Definion
{
int sum=0,average_mark=0;
for (int i=0;i<size;i++){
sum=sum+marks1[i];
}
average_mark=sum/size;
return average_mark;}
OUTPUT
Average of marks = 30
Number of Elements = 5
Process returned 0 (0x0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
THANK YOU
C C Programming Language
{
[Any Questions?]
}
C PROGRAMMING 28/12/2024
}

PASSING ONE DIMENSIONAL ARRAY AS ARGUMENT PPT.pptx

  • 1.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 Passing ‘One dimensional Array’{ PRAMEESHA I CSBS C Programming Language [As an ARGUMENT] } C PROGRAMMING 28/12/2024 }
  • 2.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 What is {ARRAY} ●Arraysare used to store multiple values in a single variable, instead of declaring separate variables for each value. ●To create an array, define the data type (like int) and specify the name of the array followed by square brackets []. ●To insert values to it, use a comma-separated list inside curly braces, and make sure all values are of the same data type: ● 1. One Dimensional Array ● 2. Multi-Dimensional Array ● 3. Two Dimensional Array } C Programming Language PRAMEESHA - CSBS 28/12/2024
  • 3.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 One Dimensional Array CProgramming Language PRAMEESHA - CSBS 28/12/2024 ●An array where data is arranged in a single dimension is called one dimensional array. Syntax : datatype array_name[size]; Here, array_name is an array of type datatype and size is the number of elements in that array. For example, int a[5]; // declares an integer array with 5 elements float f[5]; // declares an float array with 5 elements
  • 4.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 Two Dimensional Array CProgramming Language PRAMEESHA - CSBS 28/12/2024 ●An array where data is arranged in two dimensions is called two dimensional array. Syntax : datatype array_name[d1][d2]; Here, array_name is an array of type datatype and it has 2 dimensions. The number of elements in array_name is equal to d1*d2. For example, int i[5][5]; // declares an integer array with 25 elements float f[5][10]; // declares an float array with 50 elements char n[5][10]; // declares an character array with 50 elements
  • 5.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 Accessing elements ofarray C Programming Language PRAMEESHA - CSBS 28/12/2024 ●Elements of an array can be accessed by using the name of array and index of element to be accessed For example, consider a one dimensional array int x[10]; Now, fifth element of this array can be accessed as x[4]. Note: First element of array has index 0, so the index of nth element is n-1.
  • 6.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 Functions in C-Programming CProgramming Language PRAMEESHA - CSBS 28/12/2024 ●we can divide a large program into the basic building blocks known as function. User-defined functions: are the functions which are created by the C programmer, so that we can use it many times. It reduces the complexity of a big program and optimizes the code. Elements of user defined function • Function declaration (or) Function prototype • Function call • Function definition
  • 7.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 Function declaration /Function prototype C Programming Language PRAMEESHA - CSBS 28/12/2024 ●All identifiers in C need to be declared before they are used. ●This is true for functions as well as variables. ●For functions declaration needs to be before the first call of the function. ●A function declaration is also known as function prototype. ●It consists of four parts 1. Return type 2. Function name 3. Parameter list 4. Terminating semicolon return_typefunction_name(arguments list);
  • 8.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 Function Definition C ProgrammingLanguage PRAMEESHA - CSBS 28/12/2024 Function Definition Function definition consists of two parts 1. Function header 2. Function body
  • 9.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 Array as functionparameter C Programming Language PRAMEESHA - CSBS 28/12/2024 Declaration of function parameter • return_type function_name (datatype [], [other arguments type]); • return_type function_name (datatype [size], [other arguments type]); Declaration of function named parameter return_type function_name (datatype array_name[ ], [other arguments]) { body of function; } return_type function_name (datatype array_name[size], [other arguments]) { body of function; }
  • 10.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 Passing an arrayas function parameter C Programming Language PRAMEESHA - CSBS 28/12/2024 #include<stdio.h> void func(int[], int); // function declaration int main() { int a[5]={1,2,3,4,5}; func(a,5); // function call return 0; } void func(int x[], int size) // function definition { int i; for(i=0;i<5;i++) printf("%d",x[i]); } In this program an array x is passed as parameter from main() to a function func(). The contents of array are printed using a for loop. OUTPUT – 1 2 3 4 5
  • 11.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 C Programming Language PRAMEESHA- CSBS 28/12/2024 #include<stdio.h> int avg(int [],int);///Function Declarationint main(){ int marks[5]={10,20,30,40,50}; int size,average; size=sizeof(marks)/sizeof(marks[0]);///Number of elements average=avg(marks,size); ///Function call printf("Average of marks = %dn",average); printf("Number of Elements = %dn",size); return 0; } int avg(int marks1[],int size)///Function Definion { int sum=0,average_mark=0; for (int i=0;i<size;i++){ sum=sum+marks1[i]; } average_mark=sum/size; return average_mark;} OUTPUT Average of marks = 30 Number of Elements = 5 Process returned 0 (0x0)
  • 12.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 THANK YOU C CProgramming Language { [Any Questions?] } C PROGRAMMING 28/12/2024 }