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

  • Below is my c++ code. For the first section how do I include multiple text files...

    Below is my c++ code. For the first section how do I include multiple text files such as file8, file 25, file 50, file 125? Also, for algorithm 1 my clock function does not work. I need it to start the clock before the algorithm then run through it and record the end time. Then find the difference between end time and start time and convert it to milliseconds. The print out the max sum and the run time!!!! Im...

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

  • C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include...

    C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #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; song++){       // Allocate memory for each individual character string       database[song] =...

  • 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 ()    {   ...

  • I'm writing code for a poker game and I'm not sure where I'm going wrong with...

    I'm writing code for a poker game and I'm not sure where I'm going wrong with it. My compiler is sending back errors for lines (typedef int bool)(the two char* declarations) and the two functions before my if else statement (approx lines 109). #include <stdio.h> #include <stdlib.h> #include <time.h> #define SUITS 4 #define FACES 13 #define AVAILABLE 0 #define TAKEN 1 #define SIZE 5 #define TRUE 1 #define FALSE 0 void dealACard(char *suits[], char *faces[], int deck[][FACES]); void dealAHand(char *suits[],...

  • 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++) {...

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