C Language-Arrays and Functions: Objectives
C Language-Arrays and Functions: Objectives
C Language-Arrays and Functions: Objectives
To begin with like other variable an array needs to be declared so that compiler will know what
kind of an array and how large an array we want.
int marks[10];
Here,
Array elements can be referred with subscript, the number in the bracket following the array
name. this number specifies the elements position in the array. All the array elements are
numbered, starting with 0, thus marks[2] is not second element of the array, but the third.
Entering data into an array:
For(i=0;i<=9;i++)
{
Printf(enter marks);
Scanf(%d,&marks[i]);
}
The for loop causes the process of asking for and receiving a students marks from the user to be
repeated 10 times. The first time through the loop, I has a value 0, so the scanf() function will
cause the value typed to be stored in the array element marks[0]
Array initialization:
Array elements can be referred with subscript, the number in the bracket following the array
name. the first subscript tells row number and 2nd tells column number numbered. Marks[0][0]
says first number, marks[0][1] tells number stored in first row and in 2nd column.
Array initialization:
int marks[4][]={23,45,67,87,55,43,86,44 };
int marks[][]={23,45,67,87,55,43,86,44 };
Functions in C:
In programming, a function is a segment that groups code to perform a specific task. C functions
are basic building blocks in a program. All C programs are written using functions to improve reusability, understandability and to keep track on them. A C program has at least one
function main( ). Without main() function, there is technically no C program.
C program classified into two categories.
a) Library functions
b) User defined functions
Library Functions:
Library functions are built in functions which are defined by C library, example printf(), scanf(),
strcat() etc. You just need to include appropriate header file to use these functions. They are
already declared and defined in C libraries.
List of standard library functions under different header files in C
library
<stdio.h>
<conio.h>
<ctype.h>
<math.h>
Functions
prinf()
putchar()
putc()
fputc()
clrscr()
textbackground()
isalnum()
isgraph()
acos()
atan()
fprintf()
getchar()
getc()
fgetc()
getch()
gotoxy()
isalpha()
islower()
acosh()
atanh()
sprint()
puts()
fputs()
fopen()
getche()
kbhit()
iscntrl()
isprint()
asin()
pow()
scanf()
gets()
fgets()
fclose()
textcolor()
wherex()
isdigit()
isupper()
asinh()
sqrt()
C allows programmer to define their own function according to their requirement. These types of
functions are known as user-defined functions. Suppose a programmer wants to find factorial of
a number and check whether it is prime or not in same program. Then, he/she can create two
separate user-defined functions in that program: one for finding factorial and other for checking
whether it is prime or not.
#include <stdio.h>
void function_name()
{
//statements
}
int main()
{
function_name
}
Uses of Functions:
a) Writing a function avoids rewriting the same code over and over.
b) Using functions it becomes easier to write programs and keep track of what they are
doing
c) It provides modularity to program
d) Easy code reusability
e) In case of large programs, with thousand of code lines, debugging and editing become
easier
Function structure:
A function is divided into three parts:
a) Function declaration
b) Function definition
c) Function calling
Function declaration:
Every function in C programming should be declared before they are used. This type of
declaration are also called function prototype. Function prototype gives compiler information
about function name, type of arguments to be passed and return type.
Return-type function-name(type(1) argument 1, type(2) argument 2, type(n) argument n);
Function_name(argument1,argument2,argument3,.,argument(n));
Function calling make the control of program jump from that statement to function definition and
executes the codes inside that function.
Passing arguments between functions:
There are four types
a)
b)
c)
d)
Function
Function
Function
Function
with
with
with
with
Function with no
arguments and no
return type
void prime();
int main(){
prime();
return 0;
}
void prime(){
int num,i,flag=0;
printf("Enter positive
integer enter to
check:\n");
Function with no
arguments and return
type
#include <stdio.h>
int input();
int main(){
int num,i,flag = 0;
num=input();
for(i=2; i<=num/2;
++i){
if(num%i==0){
flag = 1;
break;
Function with
arguments but no
return type
#include <stdio.h>
void check_display(int
n);
int main(){
int num;
printf("Enter positive
enter to check:\n");
scanf("%d",&num);
check_display(num);
return 0;
Function with
arguments and return
type
#include <stdio.h>
int check(int n);
int main(){
int
num,num_check=0;
printf("Enter positive
enter to check:\n");
scanf("%d",&num);
num_check=check(num);
scanf("%d",&num);
for(i=2;i<=num/2;++i){
if(num%i==0){
flag=1;
}
}
if (flag==1)
printf("%d is not
prime",num);
else
printf("%d is
prime",num);
}
}
}
if(flag == 1)
printf("%d is not
prime",num);
else
printf("%d is
prime", num);
return 0;
}
int input(){
int n;
printf("Enter positive
integer to check:\n");
scanf("%d",&n);
return n;
}
}
void check_display(int
n){
int i, flag = 0;
for(i=2; i<=n/2; ++i){
if(n%i==0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not
prime",n);
else
printf("%d is
prime", n);
}
if(num_check==1)
printf("%d is not
prime",num);
else
printf("%d is
prime",num);
return 0;
}
int check(int n){
int i;
for(i=2;i<=n/2;++i){
if(n%i==0)
return 1;
}
return 0;
}
Exercise
1) Write a program that will insert an element in an array.
2) Write a program that will delete a number from array.
3) Write a program to sort all the elements of an array (in ascending order, descending order,
alphabetically).
4) N numbers are entered from the keyboard into an array. The number to be searched is entered
through the keyboard by the user. Write a program to find if the number to be searched is
present in the array and if it is present, display the number of times it appears in the array.
5) Write a program find out the largest and the smallest number in an matrix
6) Take 10 numbers input from user, calculate the sum and average of those 10 numbers.
7) Write a program to copy the content of one array into another in reverse order.
8) Write a program that takes input in two 2-D array and add these two matrices and give
addition of these two matrices in 3rd matrix
9) Twenty-five numbers are entered from the keyboard into an array. Write a program to find
out how many of them are positive, how many are negative, how many are even and how
many odd.
10) Write a program to store 10 elements in array and print them in reverse order.
11) Write a program to print the following Arithmetic Progression using array:
30, 33, 36, 39, .., 60
12) Write a program to find the inverse of matrix.
13) Write a program which finds the range, mean, median, mode, variance and standard
deviation of 10 numbers.
14) Write a program which returns the position of a number stored in an array.
15) Write a program to obtain transpose of a 4 x 4 matrix. The transpose of a matrix is obtained
by exchanging the elements of each row with the elements of the corresponding column.
16)
17)
18)