For the assignment, be sure to run unmodified code, but you are free to   play around with the code to try out different parameters or different implementations. The functions don't necessarily do anything in particular, but does a  lot of basic operations on 8 variables. Note that function1 and function2  do NOT produce the same result, but they do similar amounts of operations.  Look at the differences between function1 and function2  What is the difference (apart from what the function does)?  Do the two functions do the same amount of computation?  If so, do you think that the two functions will run in the same amount of time? #include #include #include int function1(int a, int b, int c, int d, int e, int f, int g, int h, int n) {   for (int i=0; i

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

 For the assignment, be sure to run unmodified code, but you are free to 
 play around with the code to try out different parameters or different implementations.

The functions don't necessarily do anything in particular, but does a
 lot of basic operations on 8 variables. Note that function1 and function2
 do NOT produce the same result, but they do similar amounts of operations.

 Look at the differences between function1 and function2
 What is the difference (apart from what the function does)?
 Do the two functions do the same amount of computation?
 If so, do you think that the two functions will run in the same amount of
time?

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

int function1(int a, int b, int c, int d, int e, int f, int g, int h, int n) {
  for (int i=0; i<n; i++) {
    a = h + g;
    b = a - h;
    c = b + a;
    d = c - b;
    e = d + c;
    f = e - d;
    g = f + e;
    h = g - f;
  }
  return h;
}

int function2(int a, int b, int c, int d, int e, int f, int g, int h, int n) {
  for (int i=0; i<n; i++) {
    h = g - f;
    g = f + e;
    f = e - d;
    e = d + c;
    d = c - b;
    c = b + a;
    b = a - h;
    a = h + g;
  }
  return h;
}

// This is just the main which does all of the running and printing out
// You should be able to understand this code, but it is not required for the lab
int main(void) {
  // Variables

  int a,b,c,d,e,f,g,h;
  int res;
  clock_t start, end;
  int numiters = 100000000;
  
  // Benchmark function1
  
  a = 1;
  b = 1;
  c = 1;
  d = 1;
  e = 1;
  f = 1;
  g = 1;
  h = 1;
  
  start = clock();
  res = function1(a,b,c,d,e,f,g,h,numiters);
  end = clock();
  double seconds = (end - start)/((double)CLOCKS_PER_SEC);

  printf("function1 (%d iterations):\n", numiters);
  printf("\tThe result of the function is: %d\n", res);
  printf("\ttime: %f seconds\n",seconds);
  printf("\n");
  
  // Benchmark function2

  a = 1;
  b = 1;
  c = 1;
  d = 1;
  e = 1;
  f = 1;
  g = 1;
  h = 1;
  start = clock();
  res = function2(a,b,c,d,e,f,g,h,numiters);
  end = clock();
  seconds = (end - start)/((double)CLOCKS_PER_SEC);

  printf("function2 (%d iterations):\n", numiters);
  printf("\tThe result of the function is: %d\n", res);
  printf("\ttime: %f seconds\n",seconds);
  printf("\n");

  return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY