asked 61.6k views
5 votes
Write onic sinement that uses pointer pvar to assign the value 3.141592 to field dx of variable var. Do not use "var" in the statement. This requires the pointer syntax for strnetares struct mystruct float x; double dx; \} struct mystruct var, "pvar; pvar = &var; The file my.dat contains an unknown number of unformatted 4-byte float values. Write code to read in the second-to-the-last 4 -byte float value and store it in variable x. That is: not the last value; not the next-to-last value; but the second-to-the-last value. #include> main( ) FILE *pf; float x; pf=fopen("my.dat", "rb"); Complete the program below to use the rand( ) function to place 400 pseudo-random integer values uniformly distributed between the values 6 and 64 (inclusive of 6 and 64 ) into the vector y[]. #includeh> #includeh> main( ) int y[400], i;

asked
User Rashedcs
by
8.1k points

1 Answer

1 vote

1. Assigning value using a pointer:

```C

#include <stdio.h>

struct mystruct {

float x;

double dx;

};

int main() {

struct mystruct var, *pvar;

pvar = &var;

pvar->dx = 3.141592; // Assigning the value 3.141592 to field dx using the pointer

return 0;

}

```

In this code, we define a structure `mystruct` with `float x` and `double dx` fields. We create a variable `var` of type `mystruct` and a pointer `pvar` of type `mystruct*`. Then, we assign the address of `var` to `pvar` using the address-of operator `&`. Finally, we use the pointer syntax `pvar->dx` to assign the value 3.141592 to the `dx` field of `var` without directly using `var`.

2. Reading the second-to-the-last value from a file:

```C

#include <stdio.h>

int main() {

FILE *pf;

float x;

int count = 0;

pf = fopen("my.dat", "rb");

if (pf == NULL) {

printf("Error opening the file.\\");

return 1;

}

// Seek to the end of the file and check its size

fseek(pf, 0, SEEK_END);

long size = ftell(pf);

// Read the second-to-the-last float value

fseek(pf, size - sizeof(float) * 2, SEEK_SET);

fread(&x, sizeof(float), 1, pf);

fclose(pf);

printf("Second-to-the-last value: %.2f\\", x);

return 0;

}

```

In this code, we open the file "my.dat" in binary mode for reading. We first check if the file was successfully opened. Then, we seek to the end of the file using `fseek` and determine its size using `ftell`. With the file size, we can calculate the position of the second-to-the-last float value by seeking back from the end by `sizeof(float) * 2`. Finally, we read the float value using `fread` and print it.

3. Generating pseudo-random values using rand():

```C

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {

int y[400], i;

// Set seed for random number generation

srand(time(NULL));

// Generate pseudo-random values between 6 and 64

for (i = 0; i < 400; i++) {

y[i] = rand() % 59 + 6;

}

// Print the generated values

for (i = 0; i < 400; i++) {

printf("%d ", y[i]);

}

return 0;

}

```

In this code, we first set the seed for the random number generation using `srand` and `time`. Then, we use a loop to generate 400 pseudo-random values between 6 and 64 (inclusive) using `rand() % 59 + 6`. Finally, we print the generated values using another loop.

Please note that these code snippets are provided as guidance and may require further modifications based on your specific requirements.

answered
User Framontb
by
7.9k points