Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Please help me to write 2 functions int calcArea(int side1, int side2) and int sumAreaImprove(int a, int b, int *arr) below. Please don't modify other

Please help me to write 2 functions int calcArea(int side1, int side2) and int sumAreaImprove(int a, int b, int *arr) below. Please don't modify other information anywhere, only the code in those 2 functions will be graded.

#include

#include

#include // for times()

#include // for clock()

char * name = "I. FORGOT";

// These can be changed for debugging purposes

// but DO CHANGE THEM TO THE ORIGINAL VALUES

// RUNS = 10000

// SIZE = 10000

#define RUNS 10000

#define SIZE 10000

/* write a function using gcc inline assembly

* that calculates the area of a triangle.

* the formula for doing so is:

* area = 1/2 x side1 x side2

*/

int calcArea(int side1, int side2)

{

int area = 1.0;

// PUT YOUR ASSEMBLY CODE HERE

// DON'T WRITE ANY OTHER

// C CODE

return area;

}

/* this is the original function

* DON"T TOUCH THIS FUNCTION

* it's for reference only

*/

int sumAreaOrig(int a, int b, int *arr)

{

int sum, i, j;

for(i=0; i

sum = 0;

for(j=0; j

sum += arr[j]*calcArea(a,b) + j;

}

}

return sum;

}

/* PLEASE MODIFY THIS FUNCTION

* make sure it returns the same

* sum as the original function

*/

int sumAreaImprove(int a, int b, int *arr)

{

/* you can add any variables here */

int sum, i, j;

/* leave this outer loop alone*/

for(i=0; i < RUNS; i++)

{

/* you can change this inner loop */

/* but remember it must sum over all values */

sum = 0;

for(j=0; j

sum += arr[j]*calcArea(a,b) + j;

}

}

return sum;

}

/* You can modify main for debugging

* but PLEASE RETURN IT TO THE ORIGINAL

* CODE. I will be using only your two functions

* above when grading. So don't write code

* you want to keep outside of the above two

* functions (they won't be copied over)

*/

int main(int argc, char **argv)

{

struct tms origStart, origStop, impStart, impStop;

double origTime, impTime;

double difference;

int area, sumOrig, sumImprove;

int side1, side2;

int i;

int array[SIZE];

for(i=0; i

array[i] = i;

if(argc < 3) {

printf("Need to provide two side measurements ");

exit(-1);

} else {

side1 = atoi(argv[1]);

side2 = atoi(argv[2]);

}

printf("side1: %d side2: %d ", side1, side2);

printf("Name: %s ",name);

area = calcArea(side1, side2);

printf("the area is: %d ", area);

//start time here

times(&origStart);

sumOrig = sumAreaOrig(side1, side2, array);

times(&origStop);

//end time here

printf("results from orignal: %d ", sumOrig);

// start time here

times(&impStart);

sumImprove = sumAreaImprove(side1, side2, array);

times(&impStop);

// end time here

printf("results from improved: %d ", sumImprove);

//do time calculations here

origTime = (origStop.tms_utime - origStart.tms_utime);

impTime = (impStop.tms_utime - impStart.tms_utime);

printf("original user time: %f ",origTime);

printf("improved user time: %f ", impTime);

difference = ((origTime - impTime)/origTime)*100.0;

printf("percent difference: %f ", difference);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

Students explore these related Databases questions