Question

In: Computer Science

Using the C Programming language, write a program that sums an array of 50 elements. Next,...

Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling.

Marking:-

Optimize the code for an array of 50 elements using loop unrolling. Prepare a graph that show the performance improvements. (30)

Solutions

Expert Solution

Loop unrolling is a loop transformation technique that helps to optimize the execution time of a program. We basically remove or reduce iterations. Loop unrolling increases the program’s speed by eliminating loop control instruction and loop test instructions.

c program without loop unrollong:

#include<stdio.h>
#include<stdlib.h>

int main()
{
int n,sum=0;
n=50;
int a[50];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("sum is %d",sum);
return 0;

}

now with loop unrolling:

#include<stdio.h>
#include<stdlib.h>

int main()
{
int n,sum=0;
n=50;
int a[50];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum=sum+a[0];
sum=sum+a[1];
sum=sum+a[2];
sum=sum+a[3];
sum=sum+a[4];
sum=sum+a[5];
sum=sum+a[6];
sum=sum+a[7];
sum=sum+a[8];
sum=sum+a[9];
sum=sum+a[10];
sum=sum+a[11];
sum=sum+a[12];
sum=sum+a[13];
sum=sum+a[14];
sum=sum+a[15];
sum=sum+a[16];
sum=sum+a[17];
sum=sum+a[18];
sum=sum+a[19];
sum=sum+a[20];
sum=sum+a[21];
sum=sum+a[22];
sum=sum+a[23];
sum=sum+a[24];
sum=sum+a[25];
sum=sum+a[26];
sum=sum+a[27];
sum=sum+a[28];
sum=sum+a[29];
sum=sum+a[30];
sum=sum+a[31];
sum=sum+a[32];
sum=sum+a[33];
sum=sum+a[34];
sum=sum+a[35];
sum=sum+a[36];
sum=sum+a[37];
sum=sum+a[38];
sum=sum+a[39];
sum=sum+a[40];
sum=sum+a[41];
sum=sum+a[42];
sum=sum+a[43];
sum=sum+a[44];
sum=sum+a[45];
sum=sum+a[46];
sum=sum+a[47];
sum=sum+a[48];
sum=sum+a[49];

printf("sum is %d",sum);
return 0;

}

Advantage of loop unrolling is that it makes the program fast. However, it makes our program bit lengthy.


Related Solutions

Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT