Question

Need help adjusting this code in C. I'm not sure this one part of the code...

Need help adjusting this code in C. I'm not sure this one part of the code is correct, it needs to generate n random addresses between 0 and (2^32)-1. So I set it to "address = rand() % 232;" but I'm not sure if that is correct. Can you please fix thanks.

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

int main()
{

unsigned int address;
unsigned int pg_num;
unsigned int offset;
unsigned int i;
unsigned int n = 1000000;
double time_taken;

srand((unsigned)time(NULL));
clock_t begin = clock();

for (i = 0; i < n; i++)
{
    address = rand() % 232;
    pg_num = address / 4096;
    offset = address % 4096;
}

clock_t end = clock();

time_taken = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Total CPU time: %f\n", time_taken);
return 0;

}

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

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

int main()
{

unsigned int address;
unsigned int pg_num;
unsigned int offset;
unsigned int i;
unsigned int n = 1000000;
double time_taken;

srand((unsigned)time(NULL));
clock_t begin = clock();

for (i = 0; i < n; i++)
{
// it is 2^32-1 not 232
address = rand() % (int)(pow(2,32)-1);
pg_num = address / 4096;
offset = address % 4096;
}

clock_t end = clock();

time_taken = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Total CPU time: %f\n", time_taken);
return 0;

}

1 2 3 4 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> 6 7 - int main() { unsigned int address; 1

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Need help adjusting this code in C. I'm not sure this one part of the code...
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
  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify...

    ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify Program 2 such that it generates n random virtual addresses between 0 and 232-1 and computes the page number and offset for each address. Here, do not write to the console. Instead, run your program with n = 1000000 random virtual addresses and compute the total CPU time of the task. Report your findings, as well as how you ran your program. Provide commentary...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • Could someone please help me with this C language code I'm confused as to why i'm...

    Could someone please help me with this C language code I'm confused as to why i'm getting an error every time I run it on command line if some could please help me as soon as possible. #include <stdio.h> #include <stdlib.h> int main() { char *ptr_two = (char *)malloc(sizeof(char)*50); printf("%p\n", ptr_two); ptr_two = "A constant string in C"; printf("%p\n", ptr_two); printf("%s\n", ptr_two); free(ptr_two); return 0; }

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • Why i having this error using opengl C++ here part of code #include "glaux.h" #include "glut.h"...

    Why i having this error using opengl C++ here part of code #include "glaux.h" #include "glut.h" #include <stdio.h> #include <stdlib.h> #include <conio.h> #define _USE_MATH_DEFINES #include <math.h> #include <time.h> some coder here and etc bool isGood(Tpos a) { DataToCp(); int k=0,i,j,lt; Tpos temp; cp[a.y][a.x]='x'; if ((step(a.y,a.x)>3)||(step(a.y+1,a.x)>3)||(step(a.y-1,a.x)>3)||(step(a.y,a.x+1)>3)||(step(a.y,a.x-1)>3)) return false; for(j=1;j<=m;j++) for(i=1;i<=m;i++) if (cp[j][i]=='.') { if (k>0) return false; fill(j,i,&temp,&lt); k++; } return true; } void genMap() { int i,j; map.x=m+2; map.z=m+2; printf("Loading...\n"); srand( (unsigned)time( NULL ) ); for (j=0;j<map.z;j++) for (i=0;i<map.x;i++) {...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • Create a functional ATM using the code base given in the example. You must use the...

    Create a functional ATM using the code base given in the example. You must use the routines provided in the code base example. Failure to follow the directions will result in an 'F' the example is down below! /* * @desc: Simple ATM machine * @duedate: 23-April-2019 * * * */ #include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> char menu(); char transaction_menu(); /* Verifies the Pin Entered match the PIN on record */ int validatePin(int); /* Verifies the funds...

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