0% found this document useful (0 votes)
68 views7 pages

I2204 Imperative Programming: DR Siba Haidar

The document discusses testing functions in C programming. It provides examples of writing test functions with the name of the original function followed by "Test" to verify the results. Students are given exercises to write functions to find the maximum of two, three or more integers, as well as to merge two sorted arrays, and provide corresponding test functions to validate the results.

Uploaded by

Ali Mzayhem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
68 views7 pages

I2204 Imperative Programming: DR Siba Haidar

The document discusses testing functions in C programming. It provides examples of writing test functions with the name of the original function followed by "Test" to verify the results. Students are given exercises to write functions to find the maximum of two, three or more integers, as well as to merge two sorted arrays, and provide corresponding test functions to validate the results.

Uploaded by

Ali Mzayhem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

I2204

Imperative Programming

Dr Siba Haidar
Testing a function

• In our programs
– every time we will write a function
– in order to test whether it gives us the result we want,
– we must write a test function called the same but followed with Test word
• Example I
– If the written function is callled myFunction
– The test function must be called myFunctionTest

2020-2021 I2204 - Dr Siba Haidar 2


Full example

#include <stdio.h>
void myFunction(){
printf("hello world!\n");
}

void myFunctionTest(){
myFunction();
}

void main(){
myFunctionTest();
getchar();
}

2020-2021 I2204 - Dr Siba Haidar 3


Exercise 1
• Write a C function "max2" that takes two integers as arguments and returns the value of
the largest one.

• Write the following max2Test

void max2Test(){
int a=1, b=3, max;
max=max2(a,b);
printf("the max(%d,%d)=%d\n",a,b,max
max=max2(b,a);
printf("the max(%d,%d)=%d\n",b,a,max);
}

• Write the main to call max2Test and verify your result


2020-2021 I2204 - Dr Siba Haidar 4
Exercise 2

• Write a C function "max3" that takes three integers as arguments and


returns the value of the largest one.

• Attention
– max3 MUST call max2 instead of repeating the work
• write max3Test and call it from your main
– Don't forget to delete the call for max2Test
• verify your result

2020-2021 I2204 - Dr Siba Haidar 5


Exercise 3

• write a C function "maxAll" that takes an array of integers and returns the
value of the largest one.

• attention
– maxAll MUST call max2 instead of repeating the work
• write maxALLTest in which you initialize an array
– int anArray[]={13,2,6,34,5,6,90,122,4,2,6,8,4,23,234};

• modify your main and test your function

2020-2021 I2204 - Dr Siba Haidar 6


Exercise 4

• write a C function ”merge" that takes two sorted arrays of integers and
merges them into a third one.

• write mergeTest in which you declare 3 arrays and initialize the first two

• write main and test your function

2020-2021 I2204 - Dr Siba Haidar 7

You might also like