Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are required to write a C program - whose input is an existing text file wit

ID: 3598009 • Letter: Y

Question

You are required to write a C program

- whose input is an existing text file with a name specified in a command line as the first argument,

- whose output is a new text file with a name specified in a command line as the second argument; requirements to the new text file are given below.

Input and output files should have extension ‘.txt’ but only names of the files are specified in a command line.

Example: suppose the executable version of your program is named p2.out. The program will be executed by a command line of the following form:

p2.out inputfile outputfile

In this case file inputfile.txt must exist, outputfile.txt will be created.

Requirements for the output text file:

The output file should have copy of each line of the input file in the same order of lines. Each line of the output file should consist of the same words as the corresponding line in the input file but ordered backward.

In the input file words are separated by one or more spaces (' ') or by horizontal tab (' '). In the output file words should be separated by single space.

In the input and output files, each line ends with new line character (‘ ’). Lines do not exceed 255 characters including new line character.

The outline of your C program is as follows.

1.Read command line arguments and check the correctness.

2.Create a new file named second argument + ".txt"

3.Proceed with converting input file lines into output file lines.

4.Stop and print out "Finished." once finish reading and writing the files.

Thus, each time your program is executed, it should handle just one input file.

Explanation / Answer

program test_gauss_sparse
    implicit none

!   explicit interface to the gauss_sparse function
    interface
        function gauss_sparse(num_iter, tol, b, A, x, actual_iter) result(tol_max)
           real :: tol_max
           integer, intent(in) :: num_iter
           real, intent(in) :: tol
           real, intent(in), dimension(:) :: b, A(:,:)
           real, intent(inout) :: x(:)
           integer, optional, intent(out) :: actual_iter
        end function
    end interface

!   declare variables
    integer :: i, N = 3, actual_iter
    real :: residue
    real, allocatable :: A(:,:), x(:), b(:)

!   allocate arrays
    allocate (A(N, N), b(N), x(N))

!   Initialize matrix
    A = reshape([(real(i), i = 1, size(A))], shape(A))

!   Make matrix diagonally dominant
    do i = 1, size(A, 1)
        A(i,i) = sum(A(i,:)) + 1
    enddo

!   Initialize b
    b = [(i, i = 1, size(b))]

!   Initial (guess) solution
    x = b

!   invoke the gauss_sparse function
    residue = gauss_sparse(num_iter = 100, &
                           tol = 1E-5, &
                           b = b, &
                           A = a, &
                           x = x, &
                           actual_iter = actual_iter)

!   Output
    print '(/ "A = ")'
    do i = 1, size(A, 1)
        print '(100f6.1)', A(i,:)
    enddo

    print '(/ "b = " / (f6.1))', b

    print '(/ "residue = ", g10.3 / "iterations = ", i0 / "solution = "/ (11x, g10.3))', &
        residue, actual_iter, x


end program test_gauss_sparse

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote