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

Consider the following code segment within a loop body: if ( n % 2 == 0 ) // bra

ID: 2247371 • Letter: C

Question

Consider the following code segment within a loop body:

if ( n % 2 == 0 ) // branch 1

                  a++;

if ( n % 10 == 0 ) // branch 2  

                    b++;

Assume that the following list of 10 values of n is to be processed by 10 iterations of this loop:

18, 29, 30, 41, 52, 60, 79, 80, 91, 100

List the predictions for the following branch prediction schemes and calculate the prediction accuracies for each scheme.

(a) Always taken.

(b) Always not taken.

(a) 1-bit predictor, initialized to predict taken.

(b) 2-bit predictor, initialized to weakly predict taken.

Explanation / Answer

#include<stdio.h>

int main(){

int list[] = {18, 29, 30, 41, 52, 60, 79, 80, 91, 100};

int a=0, b=0, n;

for(n=0;n<10;n++){

if ( n % 2 == 0 ) // branch 1

a++;

if ( n % 10 == 0 ) // branch 2

b++;

}

printf("a) Always taken : %d ",a);

printf("b) Always not taken : %d", b);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote