Lab 11 ArraysWithFunctions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CSE 103: Computer Programming in C

Lab #11: Arrays with Functions


Objective:
• Practice how to use one-dimensional arrays with function.

Task 1: Arrays with Functions


Write Compile & Run the following C program in Dev-C++.
Save the source file in a new folder Lab11 as Lab11/program1.c
#include <stdio.h>
#define SIZE 8 //array size

void read_array( double a[ ] , int size ); //prototype


void print_array( double a[ ] , int size );
void find_max_min( double a[ ] , int size );

int main(void) {
double x [SIZE] = {0};
printf("Initial values: \n");
print_array( x , SIZE );
//reading values from the keyboard
read_array( x , SIZE );

printf("\nNew values: \n");


print_array( x , SIZE );

find_max_min( x , SIZE );
return 0;
}
/* This function prompts the user to enter values to array of type double */
void read_array( double a[ ] , int size ){
int i;
printf("Enter %d integer numbers separated by blanks\n> ", size);
for (i = 0; i < size; ++i)
scanf( "%lf" , &a[i] );
}

void print_array( double a[ ] , int size )


{
int i;
for (i = 0; i < size; ++i)
printf("%.2f\t", a[i]);

printf("\n\n");
}

Page 1 of 8
/* search array for max & min*/
void find_max_min( double a[ ] , int size )
{
int i;
double max , min ;

max = a[0];
min = a[0];

for ( i = 1 ; i < size ; ++i ) {


if (a[i] > max)
max = a[i];
else if (a[i] < min)
min = a[i];
}

printf("\nThe maximum element in the array is %.2f\n", max );


printf("\nThe minimum element in the array is %.2f\n", min );
}

1. Write a sample Output for the program.

2. Run the Debugger on the program several time. Observe the initial values of the array x
and how the values are assgned to each element in read_array.
Wirte down your observations
Observations:

3. What is the rule of the second parameter size shown in the three user-defined
functions? (See slide #8 of Lect #9).
Not specifying the size will allow the function to be called with any size.

4. Add usefull comments (including a short statement-of-purpose for each user-defined


functions) to Program1.c

Page 2 of 8
Task 2: Partially-filled Arrays
Note that program1.c we always ask the user to enter SIZE elements to the array.
Program2.c below is a modified version of Program2.c where we first ask the user how
many elements he has.
We declare the array big enough so that it can work in the worst-case scenario and keep track of
the actual elements in the array using the value entered by the user.

Write Compile & Run the following C program in Dev-C++.


Save the source file as Lab11/program2.c
Compile & Run the program in Dev-C++.
#include <stdio.h>
#define SIZE 100 /* maximum number of elements would be stored in the array */

void read_array( double a[ ] , int size );


void print_array( double a[ ] , int size );
void find_max_min( double a[ ] , int size );

int main(void) {
double x [SIZE] = {0};
int num_elements; // a new variable;
// keeps track of the actual elements in the array
printf("How many elements do you have? >> ");
scanf("%d" , &num_elements ) ;

read_array( x , num_elements );

printf("\n You entered: \n");


print_array( x , num_elements );

find_max_min( x , num_elements );
return 0;
}
void read_array( double a[ ] , int size ){
int i;
printf("Enter %d integer numbers separated by blanks\n> ", size);
for (i = 0; i < size; ++i)
scanf( "%lf" , &a[i] );
}
void print_array( double a[ ] , int size )
{
int i;
for (i = 0; i < size; ++i)
printf("%.2f\t", a[i]);

printf("\n\n");
}

Page 3 of 8
/* search array for max & min*/
void find_max_min( double a[ ] , int size )
{
int i;
double max , min ;

max = a[0];
min = a[0];

for ( i = 1 ; i < size ; ++i ) {


if (a[i] > max)
max = a[i];
else if (a[i] < min)
min = a[i];
}

printf("\nThe maximum element in the array is %.2f\n", max );


printf("\nThe minimum element in the array is %.2f\n", min );
}
1. Give a sample output of the program

2. Run the Debugger on the program several time.


Wirte down your observations
Observations:

3. How many bytes are reserved for the array x in the computer memory? How many of
them were used in the output you showed above?

100 (elements) * 8 (bytes) = 800 bytes = 8 * 102 computer memory 8 * 230 bytes = 8 * 109

4 (elements) * 8 (bytes) = 32 bytes

5(elements) * 8 (bytes) = 40 bytes

Page 4 of 8
Task 3:
Write Compile & Run the following C program in Dev-C++.
Save the source file as Lab11/program3.c
Compile & Run the program in Dev-C++.
#include <stdio.h>
#define SIZE 10

int search(int arr[] , int size , int number);

int main(void)
{
int a[SIZE] = { 5 , 6 , 3 , 1 , 22 , 13 , 4 , 2 , 3 , 11 };
int num ;

printf("Enter an integer you want to search in array> ");


scanf("%d",&num);

if( search(a , SIZE , num) )


printf("The number is in array");
else
printf("The number is not in array");
return 0;
}

int search( int arr[] , int s , int number ){


int i;
for(i = 0 ; i < s ; i++)
if(arr[i] == number)
return 1;
return 0;
}
1. Give a sample output of the program

Page 5 of 8
2. Modify the program as following:
Make the function seach returns the index of the first occurance of number. If number
doesnot exist, the function returns -1.
In mian, print the returned index, or NOT FOUND!

#include <stdio.h>
#define SIZE 10

int search(int arr[] , int size , int number);

int main(void)
{
int a[SIZE] = { 5 , 6 , 3 , 1 , 22 , 13 , 6 , 2 , 6 , 11 };
int num ;
int ind ;

printf("Enter an integer you want to search in array> ");


scanf("%d",&num);

ind = search(a , SIZE , num) ;


if( ind == -1 )
printf("The number is NOT in array");
else{
printf("The number is in array");
printf("Its index is %d" , ind );
}
return 0;
}

int search( int arr[] , int s , int number ){


int i;
int index = -1 ;
for(i = 0 ; i < s ; i++)
if(arr[i] == number){
index = i ;
return index;
}
return index;
}

Page 6 of 8
Exercise:
Write a program in which the main function reads an integer array data_items of size 10 and an integer
variable num. It then passes the array data_items and the num variable to a function called
count_divisors which counts the number of elements of array data_items that are divisible by
num and returns the count to the main program which prints it.

Sample Output:

Page 7 of 8
Array with Functions
You can pass individual array elements to a function that has a normal as argument. Array elements
behave like normal variables.
We can also have a function that takes the whole array as argument. Such a function does not
create a local array when called; instead it will work directly on elements of the same array used in
the call. An example of a prototype of a function that has an array as argument is:
void print_array (double a[], int n);
As noticed above, we use square brackets without specifying the size. But in addition to the array,
we need the number of elements of the array and this is why we have the second argument n. When
calling such a function, we use the name of the actual array without square brackets for the first
argument. For the second argument, we use the size of the actual array if it is full, otherwise we use
the actual number of the elements present in the array if it is partially filled.
When arrays are used as arguments, the function is able to access all the elements of the actual
array; the function can use the values of elements of the array, or it can also assign values to the
elements of the array. The function can also modify the values of the array elements; Here are a
few examples of function declarations with arrays as arguments
void print_array (double a[], int n); // The function accesses the values and print them on the
screen
double get_average (double a[], int n ); // The functions uses the array values to return average.
void find_max_min (double a[], int n); // The function used the array values to find and print
maximum and minimum.
void read_array (double a[], int n); // The function reads values from the user and assigns them
to the elements of the array.
void double_array (double a[], int n); // When calling the function, the array has already values in
its elements. The function modifies these values by multiplying them by 2.

Page 8 of 8

You might also like