I2204 Imperative Programming: DR Siba Haidar
I2204 Imperative Programming: DR Siba Haidar
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
#include <stdio.h>
void myFunction(){
printf("hello world!\n");
}
void myFunctionTest(){
myFunction();
}
void main(){
myFunctionTest();
getchar();
}
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);
}
• 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
• 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};
• 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