0% found this document useful (0 votes)
18 views6 pages

Lab 3

Uploaded by

mrb4jw42002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Lab 3

Uploaded by

mrb4jw42002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

GIFT School of Engineering

and Applied Sciences

Fall 2024

CS-349: Operating System- Lab

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.

Theory: An array is a collection of variables of the same type.

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 :-

type arrayName [ arraySize ];

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 )

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.

Sample Output:

myArray Elements: 3 4 6 19 34 12 1

Enter Number to search: 19

Output: The number you have entered is present in myArray at position 4

Enter Number to search: 44

Output: The number you have entered is not present in myArray

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)

Objectives: Study about arrays-one dimensional and functions- in C language.

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.

1. Write a function called fillArray that takes a two-dimensional integer array as


argument and fills the array with the user entered numbers.

You may use the following header for this function:


void fillArray(int row , int col, int array[][col])

2. Write a function called printArray that takes a two-dimensional integer array as


argument and prints all elements of array. The function should print each row line by
line.

You may use the following header for this function:


void printArray(int row , int col, int array[][col])

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.

Objectives: Study about arrays-two dimensional and functions- in C language.

Tools/Equipents: Editors like gedit,vi editor in linux with gcc in desktop or laptop computer.

THEORY

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional


array is, a list of one-dimensional arrays. To declare a two-dimensional integer array of size
[x][y], you would write something as follows :-

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.

1. Write a function called printSumAverage which will take a two-dimensional integer


array as argument and prints the total sum and average of the array.

You may use the following header for this method:

void printSumAverage (int row , int col, int array[][col])

Objectives: Study about arrays-two dimensional and functions- in C language.

Tools/Equipents: Editors like gedit,vi editor in linux with gcc in desktop or laptop computer.

You can initialize a two-dimensional array as follows:

int array [][]= {{4,5,9}, {1,2,3}, {9,10,11},


{19,25,26}};

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:

Total Sum is: 124


Average is: 10.33

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.

1. Write a function called replaceNegativeWithZeroes which will take a two-


dimensional integer array as argument and replace all negative values from the array with
zeroes.

You may use the following header for this function:

void replaceNegativeWithZeroes(int row ,


int col, int array[][col])

Objectives: Study about arrays-two dimensional and functions- in C language.

Tools/Equipents: Editors like gedit,vi editor in linux with gcc in desktop or laptop computer.

You might also like