Lab 3
Lab 3
Fall 2024
Lab-3 Manual
Functions and Arrays in C
v1.0
12/06/2022
Objective
To get familiar with static arrays and how to write functions in C
Task #1
Write a function in C that takes input from a user in an array of size 10. It then needs to
compute average of all the elements of this array. Note that the avg must be in float data type.
You may use the following header for this function:
void avgFun(int array[], int size)
Objectives: Study about arrays-one dimensional and functions- in C language.
Tools/Equipents: Editors like gedit,vi editor in linux with gcc in desktop or laptop computer.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows :-
An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example :-
double salary = balance[9];
Task # 2
Write a function in C that takes a number from the user and find if that number is present in
myArray. Also display at which position it is present
You may use the following header for this function:
void findNumber(int myArray[] , int size, int number )
Tools/Equipents: Editors like gedit,vi editor in linux with gcc in desktop or laptop computer.
Sample Output:
myArray Elements: 3 4 6 19 34 12 1
Task # 3
Write a function that accepts two arguments: an array and a number n. Assume, that the array
contains integers. The method should display all of the numbers in the array that are greater
than the number n.
You may use the following header for this method:
void largerThanNumber(int array[], int size, int number)
Tools/Equipment’s: Editors like gedit, vi editor in linux with gcc in desktop or laptop computer.
Task # 4
In this task, you are being asked to write functions that manipulate arrays in C.
Take two numbers from the user as the size of rows and columns of the array, call the function
fillArray and then call the function printArray.
Tools/Equipents: Editors like gedit,vi editor in linux with gcc in desktop or laptop computer.
THEORY
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A two
dimensional array can be considered as a table which will have x number of rows and y number
of columns.
Task # 5
In this task, you are being asked to write functions that manipulate arrays in C.
Tools/Equipents: Editors like gedit,vi editor in linux with gcc in desktop or laptop computer.
The above line of code will create a two-dimensional array having 4 rows and three columns.
If the above array is sent as an input to the printSumAverage function, then the following output
should be displayed:
NOTE: Declare and initialize the array without taking input from user.
Task # 6
In this task, you are being asked to write functions that manipulate arrays in C.
Tools/Equipents: Editors like gedit,vi editor in linux with gcc in desktop or laptop computer.