MATLAB: How to use fprintf properly

fprintf

Here is the question:
Write a script that does the following. Use fprintf and do not show scientific notaiton. NOTE: You will be able to reuse this code in Project 2. Publish your results from MATLAB in a Word document and upload here.
  • Generate an array y with 100 elements that is normally distributed with a mean of 2.5 and a standard deviation of 1 using the expression 2.5+1*randn(1). Notice you will get fractional results.
  • Determine the average y-value (use fprintf and avoid scientific notation). Use a for loop (do not simply use the command mean().)
  • Determine the number of y-values less than the number 2.0
PLEASE DO NOT GIVE ME THE COMPLETE ANSWER TO THIS WHOLE PROBLEM. I just want to know if I should split this question into three different sections using "%%" or if I should just lump the whole thing together. Also, I am trying to use fprintf and am having very little success. Do I put fprintf in three separate sections or just at the end of the whole problem? Thanks!

Best Answer

I just want to know if I should split this question into three different sections using "%%" or if I should just lump the whole thing together.
It does not matter to MATLAB. Some people find it easier to do debugging if they use %% seconds, but I find it to be more of a nuisance myself.
The one thing I would point out is that using %% is more likely to get you style points.
Do I put fprintf in three separate sections or just at the end of the whole problem?
It does not matter to MATLAB. However, it most of the time, people find it easier to read code that does outputing quite soon after computing the values to be output. There are, though, definitely cases where you are looking to create a formatted "report", and in such cases it often makes the most sense to compute the values and then call a report-creating function to create the report.