Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: To gain insight into the performance differences when a series of if-

ID: 3576697 • Letter: O

Question

Objective: To gain insight into the performance differences when a series of if-then-else tests are converted into a switch - case configuration. The specific assignment is to compare the execution times of two routines that perform the same task, but implement the decision processes differently. Assignment: Write a main program which generates a source array which is populated with random numbers in the range of 0 to 999. Write your program so that the size of the array can be changed easily or specified on the command line. Specific requirements are: Use rand() to generate a sequence of random integers. Use the modulus function to limit the original numbers to a range of 0 to 999. Count the number of values which fall in the range of 0 - 199, 200 - 399, 400 - 599, 600 - 799, 800 - 999 using both a switch statement and a sequence of if-then-else statements. Measure how much time it takes to generate a count for all of the numbers with both approaches, and compare the two approaches' execution times. For the case statement, I suggest that you convert the number to an integer form that lends itself to the switch statement. You may use something like: int index = number/200. This will work because the number is an integer and the fractional part of the result is discarded - this is good practice to see the effects of different ways that numbers are represented.

Explanation / Answer

c program:

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[50],size,i,a=0,b=0,c=0,d=0,e=0,p=0,q=0,r=0,s=0,t=0,index;
clrscr();
printf(" enter the size of the array:");
scanf("%d",&size);
for(i=0;i<size;i++)
arr[i]=rand()%1000;
//using if else ladder
printf(" count using if else");
for(i=0;i<size;i++)
printf(" %d",arr[i]);
for(i=0;i<size;i++)
{
if(arr[i]>=0&&arr[i]<=199)
a++;
else if(arr[i]>=200&&arr[i]<=399)
b++;
else if(arr[i]>=400&&arr[i]<=599)
c++;
else if(arr[i]>=600&&arr[i]<=799)
d++;
else if(arr[i]>=800&&arr[i]<=999)
e++;
}
printf(" number of values between 0 to 199 are %d",a);
printf(" number of values between 200 to 399 are %d",b);
printf(" number of values between 400 to 599 are %d",c);
printf(" number of values between 600 to 799 are %d",d);
printf(" number of values between 800 to 999 are %d",e);
printf(" count using switch case");
for(i=0;i<size;i++)
{
index=arr[i]/200;
switch(index)
{
case 0:p=p+1;
       break;
case 1:q=q+1;
       break;
case 2:r=r+1;
       break;
case 3:s=s+1;
       break;
case 4:t=t+1;
       break;
default:printf("nothing");
   break;
}
}
printf(" number of values between 0 to 199 are %d",p);
printf(" number of values between 200 to 399 are %d",q);
printf(" number of values between 400 to 599 are %d",r);
printf(" number of values between 600 to 799 are %d",s);
printf(" number of values between 800 to 999 are %d",t);
getch();
}

measuring times:

let n be the size of the array,then the for loop runs for n+1 times.for if else,every element in array is comapred to the condition statement writtten in if block.so the if statment is executed only on time in for loop,so entirely it will take (n+1)*1 total execution time.

similarly,in switch case index varaible is computed every time.that is index variable is computed for n times and for loop runs for n+1 times and switch case executes for 1 time so overall execution time for switch case is (n+1)*n*1