We would like to write a script that prints a vector Vi one element per line (i.e. a new line is following every element), with 2 digits following the decimal point, and 8 digits in all. Every number is followed by a comma (,) except the last. In other words every number printed (other than the last) will be followed by a comma and then a new line. Which of the below codes can do that? a.l = length(Vi); fprintf('%.82f,\n',Vi(1:l-1)); fprintf('%6.2f\n',Vi(l));   b.l = length(Vi); fprintf('%8f,\n',Vi(1:l-1)); fprintf('%.2f\n',Vi(l));   c.l = length(Vi); fprintf('%8.2f,\n',Vi(1:l));   d.l = length(Vi); fprintf('%8.2f,\n',Vi(1:l-1)); fprintf('%8.2f\n',Vi(l));

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

We would like to write a script that prints a vector Vi one element per line (i.e. a new line is following every element), with 2 digits following the decimal point, and 8 digits in all. Every number is followed by a comma (,) except the last. In other words every number printed (other than the last) will be followed by a comma and then a new line. Which of the below codes can do that?

a.l = length(Vi);

fprintf('%.82f,\n',Vi(1:l-1));

fprintf('%6.2f\n',Vi(l));

 

b.l = length(Vi);

fprintf('%8f,\n',Vi(1:l-1));

fprintf('%.2f\n',Vi(l));



 

c.l = length(Vi);

fprintf('%8.2f,\n',Vi(1:l));



 

d.l = length(Vi);

fprintf('%8.2f,\n',Vi(1:l-1));

fprintf('%8.2f\n',Vi(l));

Expert Solution
Step 1

The "I = " just gets prints as-is. It's nothing special and any characters like that, that aren't preceded with a \ or a % just go down exactly as you put them. The % means it's going to replace that part of the string with a variable. The f means a floating point variable. An s means a string, and a d means an integer. So %f means it's going to replace the %f with the value of "I", out to 6 or 7 decimal places, because x is the variable that shows up after the 'I = %f\n' formatting string.

If you wanted a specific number of decimal places you could use %.3f to have 3 decimal places. If you wanted to pad spaces on the right to make the whole field, say, 20 characters wide, then you could use %20.3f which would put 3 decimal places to the right of the decimal point, however many digits it needs to the left of the decimal point, and then pad as many spaces on the left as it takes to make the whole thing 20 characters wide.

 

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education