Question

Copy the source code for task 1 and add DEBUG statements around the timing functionality to allow optional use of timing. Par

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 + loopend.tv_usec-loopstart.tv_usec;

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

}

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

-----------------------------------------------From Given Source Code Without Modification-------------------------------------------

#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 + loopend.tv_usec-loopstart.tv_usec;

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

}

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;

}

CAUsers,svs97Desktop Untitledlexe 534 355 6921 7014 6325 6330 073 6329 5678 2892 3032 029 4634 3662 621 523 2329 2634 5029 72

-----------------------------------------------------When #define DEBUG 1 is used------------------------------------------------------

#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;

}

CAUsers,svs97Desktop Untitledlexe e08 1996 2024 997 424 3037 98 004 026 2834 031 1995 2824 し033 1029 1031 2826 1831 1829 1655

-------------------------------------------------------------When #define DEBUG 0 and if condition is used---------------------------

#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;

}

CAUsers,svs97Desktop Untitledlexe -436623679 486623679 4866236/9 186623679 86623679 -436623679 486623679 486623679 186623679

***** Time is there in the given screenshots*****

Add a comment
Know the answer?
Add Answer to:
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...
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
  • #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...

    #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...

  • #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...

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] +...

    #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] + strText[1] + strText[2]; int nTextLen = strlen(strText); int count = 0; printf("Welcome to token generator!\n"); printf("Enter a word to use in the token generator.\n You may enter as many words as you like. \n Press q and key when finished.\n"); scanf("%c", &strText[i]); //compute when to stop loop //check nTextLen == 1 and strText[0] == 'q' for(i=0;i< nTextLen;i++) if ((strlen(strText) != 1) || (strText[0] !=...

  • Cache performance The starting code would have: struct position { int x; int y; } int...

    Cache performance The starting code would have: struct position { int x; int y; } int N; struct position grid[N][N]; int totalX=0; int totalY=0; int i, j; //For X loop for( i = 0; i < N; i ++){ for( j = 0; j < N; j++){ totalX += grid[i][j].x; } } //For Y loop for( j = 0; j < N; j++){ for( i = 0; i < N; i++){ totalY += grid[i][j].y; } } Part I: This part...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary;...

    How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary; int reminder, weight = 1; binary = 0.0; //Input decimal number from user printf("Enter any decimal number: "); scanf("%lld", &decimal); // Since we do not want change the value of decimal in the code // let us use tempDecimal to store the value of decimal tempDecimal = decimal; printf("\n\n"); // Let us use while loop first to implement printf("while loop part:\n"); /**************START FROM HERE...

  • 6. Consider the following program: #include <stdio.h> main() { int a,b,c,d ; a=0; while (1) {...

    6. Consider the following program: #include <stdio.h> main() { int a,b,c,d ; a=0; while (1) { printf("%d\n", a); printf("Input? "); scanf("%d",&c); if (c == 0) break; d=0; for (b=1; b<=c; b++) if (c%b == 0) d++; if (d == 2 11 C == 1) a=a+c; } } What does this program do? Rewrite the code, organizing it using sound principles. Include comments and redo variable names and indentation. Use multiple functions, blocks, and/or preprocessing if you deem it necessary.

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