Question

Part 2 4. 5. add a #define statement at the top of your code for DEBUG Use an #ifdef and #endif statement to allow for condit

#include <stdio.h>

#include <sys/time.h>

#define DEBUG 1 //Constant is defined for true

int main()

{

struct timeval start,end; //to store time of starting and ending of program

//if statement is added

if(DEBUG){

gettimeofday(&start,0);

}


printf("Time in microsecond for every second: \n"); //output

for(int i=0;i<100;i++) //outer loop to run 100 times

{

struct timeval loopstart,loopend; //to store time of starting and ending of loop

//if statement added

if(DEBUG){

gettimeofday(&loopstart,0); //measuring time at start of loop


}

for(int j=0;j<1000000;j++); //outer loop to run 1000000 times

//if statement added


if(DEBUG){

gettimeofday(&loopend,0); //measuring time at end of loop


}


//calculating time for one iteration

long ms=(loopend.tv_sec-loopstart.tv_sec)*1000000 + loopend.tv_usec-loopstart.tv_usec;

printf("%ld\n",ms); //output

}

//if statement added


if(DEBUG){

gettimeofday(&end,0);


}


long ms=(end.tv_sec-start.tv_sec)*1000000 + end.tv_usec-start.tv_usec; //calculating time for whole program

printf("Time for entire program: %ld\n",ms); //output

return 0;

}

################################################

#include <stdio.h>

#include <sys/time.h>

#define DEBUG 0 //for false

int main()

{

struct timeval start,end; //to store time of starting and ending of program

//if false is applied

if(DEBUG){


gettimeofday(&start,0);

}


printf("Time in microsecond for every second: \n"); //output

for(int i=0;i<100;i++) //outer loop to run 100 times

{

struct timeval loopstart,loopend; //to store time of starting and ending of loop

//if false is applied

if(DEBUG){

gettimeofday(&loopstart,0); //measuring time at start of loop


}

for(int j=0;j<1000000;j++); //outer loop to run 1000000 times

//if false is applied


if(DEBUG){

gettimeofday(&loopend,0); //measuring time at end of loop


}


//calculating time for one iteration

long ms=(loopend.tv_sec-loopstart.tv_sec)*1000000 + loopend.tv_usec-loopstart.tv_usec;

printf("%ld\n",ms); //output

}

//if false is applied


if(DEBUG){

gettimeofday(&end,0);


}


long ms=(end.tv_sec-start.tv_sec)*1000000 + end.tv_usec-start.tv_usec; //calculating time for whole program

printf("Time for entire program: %ld\n",ms); //output

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The solution to the above question is given below with screenshot of output.

=====================================================

I kept the logic simple and i have tested all outputs.

If there is anything else do let me know in comments.

=====================================================

============ CODE TO COPY ==============================

main.c

#include <stdio.h>
#include <sys/time.h>

#define DEBUG 1 //Constant is defined for true

int main()
{

    struct timeval start, end; //to store time of starting and ending of program

    //if statement is added

#ifdef DEBUG
        gettimeofday(&start, 0);
#endif

    printf("Time in microsecond for every second: \n"); //output

    for (int i = 0; i < 100; i++) //outer loop to run 100 times
    {

        struct timeval loopstart, loopend; //to store time of starting and ending of loop

        //if statement added

#ifdef DEBUG
            gettimeofday(&loopstart, 0); //measuring time at start of loop
#endif

        for (int j = 0; j < 1000000; j++); //outer loop to run 1000000 times

        //if statement added

#ifdef DEBUG
            gettimeofday(&loopend, 0); //measuring time at end of loop
#endif

        //calculating time for one iteration

        long ms = (loopend.tv_sec - loopstart.tv_sec) * 1000000 + loopend.tv_usec - loopstart.tv_usec;

        printf("%ld\n", ms); //output
    }

    //if statement added

#ifdef DEBUG
        gettimeofday(&end, 0);
#endif

    long ms = (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec; //calculating time for whole program

    printf("Time for entire program: %ld\n", ms); //output

    return 0;
}


=====================================================
Output :

When DEBUG 1

Time in microsecond for every second:
5937
5731
2877
2727
2724
2710
2695
2722
4103
2705
2716
2725
4089
2704
2714
4099
2726
2692
2713
2719
2860
2952
2699
4628
2703
2692
2704
2719
4101
2725
2715
2721
4103
2693
2700
2713
2724
2700
4596
2696
2724
2724
2704
2718
2724
2699
2720
2724
2700
2720
2724
2698
2713
2700
2700
2807
2791
2750
2773
2696
2694
2708
2729
2714
2700
2729
2724
2706
2717
2720
2701
2718
2724
2699
2713
2724
2700
2713
2724
2695
2714
2727
2724
2709
2726
2724
2707
2722
2725
2708
2720
2725
2703
2731
2693
2703
2717
2705
2704
2695
Time for entire program: 289250

When DEBUG 0

Time in microsecond for every second:
2692
2707
3096
3123
2770
3101
2708
2873
2709
3071
2857
2692
2716
2696
2725
2712
4943
3524
3513
4971
3513
3517
4925
3513
3517
5433
3505
3565
3524
3504
3529
3538
3540
3544
3618
3522
4901
3542
3513
3514
3526
5448
3504
3518
4920
3513
3514
3535
3528
5446
3508
3505
3529
3541
3593
3541
3549
3512
3508
3518
3529
3527
3536
3542
3522
3542
3507
3517
3528
3529
4736
2701
2724
2697
4837
3505
3522
3529
3530
3533
5441
3504
3526
3568
3537
3543
3515
3510
3513
3519
3529
3531
3535
3544
3542
3505
3510
3519
3522
3527
Time for entire program: 356438

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> #include <sys/time.h> #define DEBUG 1 //Constant is defined for true int main() { struct timeval start,end; //to store time of starting and ending of program //if state...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • task1 code: #include <stdio.h> #include <sys/time.h> int main() { struct timeval start,end; //to store time of starting and ending of program gettimeofday(&start,0); printf("Time...

    task1 code: #include <stdio.h> #include <sys/time.h> int main() { struct timeval start,end; //to store time of starting and ending of program gettimeofday(&start,0); printf("Time in microsecond for every second: \n"); //output for(int i=0;i<100;i++) //outer loop to run 100 times { struct timeval loopstart,loopend; //to store time of starting and ending of loop gettimeofday(&loopstart,0); //measuring time at start of loop for(int j=0;j<1000000;j++); //outer loop to run 1000000 times gettimeofday(&loopend,0); //measuring time at end of loop //calculating time for one iteration long ms=(loopend.tv_sec-loopstart.tv_sec)*1000000...

  • #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate...

    #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate a random number. Then it has to declare how many tries the user has for the game loop. we then need the player to enter their guess. After every guess we have to give an output of how many numbers they have in the right location and how many they have the right number. The player will keep guessing until their 10 tries are...

  • Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...

    Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(char *s) {    /* this is here so code compiles */ return 0; } /* array version */ /* concantenate t to the end of s; s must be big enough */ void str_cat(char s[], char t[]) {    int i, j;    i = j = 0;    while (s[i] != '\0')    /* find end of s */        i++;    while ((s[i++] = t[j++]) != '\0') /* copy t */        ;...

  • computers. 1. How many times will this loop repeat? include <stdlib.h> #include <stdio.h> void main(void) int...

    computers. 1. How many times will this loop repeat? include <stdlib.h> #include <stdio.h> void main(void) int 10 for(= 0; i < 101) printf("d", 1); A. 10 times B. 1 time C. It will run forever D. 0 times 2. What will be the output of this program? #include <stdlib.h> #include <stdio.h> int main() int a = 100, b = 200, C = 300; if(!a >= 500) b = 300; C = 400; printf("%d, 8d, &d", a, b, c); return 0;...

  • C++ Can someone please help with this code... even when the players number isnt a winning number the program always says the player wins: #include <stdio.h> #include <time.h> //main functi...

    C++ Can someone please help with this code... even when the players number isnt a winning number the program always says the player wins: #include <stdio.h> #include <time.h> //main function int main() {    //seeding a random number    srand(time(0));    //define the arrays for the wagered, winning amount, percent amount    double amounts[7] = { 1, 5, 10, 20, 50, 100, 1000 };    double payoff[7] = { 100, 500, 1000, 2000, 5000, 10000, 100000 };    double percent[7] = { 0.01, 0.05, 1, 2,...

  • lab4C.c file contains: #include <stdio.h> ..... #define SIZE 10 #define SIZE2 40 int main(int argc, char *argv...

    lab4C.c file contains: #include <stdio.h> ..... #define SIZE 10 #define SIZE2 40 int main(int argc, char *argv[]) { char input[SIZE2]; char name[SIZE]; .... char resu[SIZE2], resu2[SIZE2], resu3[SIZE2]; printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); while (...) { /* use fgets to read again */ printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); } return 0; }3.1 Specification Develop an ANSI-C program that reads user information from the standard inputs, and outputs...

  • Identify and list all the User defined Functions to be used in the system #include <stdio.h>...

    Identify and list all the User defined Functions to be used in the system #include <stdio.h> ///for input output functions like printf, scanf #include <stdlib.h> #include <conio.h> #include <windows.h> ///for windows related functions (not important) #include <string.h> ///string operations /** List of Global Variable */ COORD coord = {0,0}; /// top-left corner of window /** function : gotoxy @param input: x and y coordinates @param output: moves the cursor in specified position of console */ void gotoxy(int x,int y) {...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

  • How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses...

    How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex).      Replace the codes (for mutex) by the codes (for reader-writer lock).            To initialize reader-writer lock, pthread_rwlock_initializer.            At the end,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT