Sunteți pe pagina 1din 8

LAB 06

C Language- Arrays and Functions


Objectives:

What are arrays


Array declaration, initialization, accessing array elements
Passing array elements to function
2-D array
What is Functions and why use functions
Function structure
Passing values between functions

What are arrays:


Array is collection of similar elements. These similar elements could be all ints, or all floats or
all chars, etc. usually the array of characters are called string, whereas an array of ints or floats
are called simply an array.
1 Dimensional Array:
Array declaration:

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,

int specifies the type of variable


marks specifies name of variable
10 tells how many elements of type int
Bracket [ ] tells compiler that we are dealing with an array

Accessing elements of an array:

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 can also be initialize as simple variable can,


int marks[10]={23,45,67,87,55,43,86,44,99,23};
int marks[]={23,45,67,87,55,43,86,44,99,23};
Passing array elements to function:
Array elements can be passed to a function by calling function by value or by reference, in the
call by value we pass the array elements to function.
Main()
{
Int marks[]={56,67,45,78,99,45};
For (int i=0 ; i< 6 ; i++)
{
Display(marks[i]);
}
}
Display(int m)
{
Printf(%d,m);
}
Two Dimensional Array:
2-D array declaration:

As 1-D array declared 2-D array can also be in same way.


int marks[4][2];
Here,

int specifies the type of variable


marks specifies name of variable
the first subscript is row number
the second subscript is column number

Accessing elements in 2- D array:

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:

2- D Array can also be initialize as simple 1-D array can,


int marks[4][2]={{23,45},{67,87},{55,43},{86,44}};

or even this can work


int marks[4][2]={23,45,67,87,55,43,86,44 };

or this can work. We must know column


int marks[][2]={23,45,67,87,55,43,86,44 };
whereas, this would never work

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()

User defined Functions:

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);

A function declaration consists of four parts:


a) Return type
b) Function name
c) Parameter list
d) Terminating semicolon
Function definition syntax:

Function definition contains programming codes to perform specific task.


return_type function_name(type(1) argument(1),..,type(n) argument(n))
{ //body of function }

Function definition has two major components:


a) Function declarator
b) Function body
Function declarator:

return_type function_name(type(1) argument(1),....,type(n) argument(n))


Syntax of function declaration and declarator are almost same except, there is no semicolon at
the end of declarator and function declarator is followed by function body.
Function body:

Function declarator is followed by body of function inside braces.


Function calling:

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");

no arguments and no return value


no arguments and return value
arguments but no return value
arguments and return value.

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)

Write a program to add two 6 x 6 matrices.

17)

Write a program to multiply any two 3 x 3 matrices.

18)

Write a program to obtain the determinant value of a 5 x 5 matrix.

S-ar putea să vă placă și