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)
Using C language: Write a program that asks the user for the size of an array,...
Using C language: Write a program that asks the user for the size of an array, then reads a number of integer values (from user input) into the array. Write a function to print out the array and call it to print the array out after all values are read in. Write a function to implement Insertion Sort and run it on the data array to sort the array. Write another function to implement Selection Sort and run it on...
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.
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55} b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at...
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.
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...
Write a Y86 program in C language that sorts an array of data using Bubble Sort....
Write a Y86 program in C language that sorts an array of data using Bubble Sort. Allow the user to input up to 10 numbers from the keyboard. Sort the array in place (i.e., no need to allocate additional memory for the sorted array). Your program should be a complete one
C PROGRAMMING LANGUAGE Problem title : Bibi's Array Bibi also has an array containing N elements....
C PROGRAMMING LANGUAGE Problem title : Bibi's Array Bibi also has an array containing N elements. Like Lili, Bibi wants to know the highest frequency (most occurrences) and all elements which have that frequency. Format Input The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤ i ≤...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT