How to resolve the algorithm Write float arrays to a text file step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Write float arrays to a text file step by step in the C programming language

Table of Contents

Problem Statement

Write two equal-sized numerical arrays 'x' and 'y' to a two-column text file named 'filename'. The first column of the file contains values from an 'x'-array with a given 'xprecision', the second -- values from 'y'-array with 'yprecision'. For example, considering: The file should look like: This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Write float arrays to a text file step by step in the C programming language

The provided C code performs the following tasks:

  1. Data Initialization:

    • It initializes an array x of size 4 with floating-point values. These values are: 1, 2, 3, and 1e11 (scientific notation for 10 raised to the power of 11).
    • It initializes another array y of size 4 to hold the square roots of the elements in x.
  2. File Opening:

    • It opens a file named "floatArray" in write mode using fopen. This file will store the square roots of the numbers in array x.
  3. Calculating and Writing to File:

    • It enters a loop that iterates through the elements of x.
    • For each element in x, it calculates the square root and stores the result in the corresponding element of y.
    • It writes the values from x and y to the open file with formatting to specify the desired number of decimal places.
    • Specifically, it uses the following format string: "%.3g\t%.5g\n"
      • "%.3g" is used to write the value of x and specifies that it should use scientific notation if necessary and display up to 3 significant digits.
      • "%.5g" is used to write the value of y and specifies that it should use scientific notation if necessary and display up to 5 significant digits.
      • "\t" is a tab character used to separate the two values.
      • "\n" represents a newline character.
  4. Loop Completion and File Closing:

    • After processing all elements of x, it closes the file using fclose.
  5. Output:

    • The resulting file "floatArray" will contain the following lines:
    1       1
    2       1.4142
    3       1.7321
    1e+11   3.1623e+05
    

    These lines show the original numbers in x and their corresponding square roots in y, formatted as specified by the format string.

Source code in the c programming language

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

int main(int argc, char **argv) {

   float x[4] = {1,2,3,1e11}, y[4];
   int i = 0;
   FILE *filePtr;

   filePtr = fopen("floatArray","w");

   for (i = 0; i < 4; i++) {
      y[i] = sqrt(x[i]);
      fprintf(filePtr, "%.3g\t%.5g\n", x[i], y[i]);
   }

   return 0;
}


1       1
2       1.4142
3       1.7321
1e+11   3.1623e+05


  

You may also check:How to resolve the algorithm String length step by step in the Trith programming language
You may also check:How to resolve the algorithm Inverted syntax step by step in the Prolog programming language
You may also check:How to resolve the algorithm System time step by step in the Pascal programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Nutt programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the ALGOL 68 programming language