sprintf inputting fieldwidth problem

This is my homework question:
Write a script that will generate a random integer from 1000 to 2000, ask the user for a field width, and print the random integer with the specified field width. The script will use sprintf to create a string such as ‘The # is %4d\n’ (if, for example, the user entered 4 for the field width) which is then passed to the fprintf function. To print (or create a string using sprintf) either the % or \ character, there must be two of them in a row.
The problem I have is the part where I am supposed to input the fieldwidth. Every time I try, it outputs a rounded number or something in scientific notation which i do NOT want. Below is my code:
high=2000;
low=1000;
number=rand*(high-low)+low
fieldwidth=input('Enter a field width: \n');
phrase=sprintf('Your number is %*.d \n', fieldwidth ,number)
%the fprintf is to compare with the sprintf to see if I'm doing it right
fprintf('Your number is %4d \n',number)
fprintf(phrase)

 Respuesta aceptada

Star Strider
Star Strider el 16 de Nov. de 2014
This is a bit tricky, because you have to know that to print out a ‘%’ as literal output in fprintf or sprintf output, you need to enter ‘%%’. So your format string (including the double ‘\\’ to output a newline character) is in the ‘fmt’ assignment here:
fieldwidth = 10;
fmt = sprintf('Your number is %%%dd \\n',fieldwidth);
x = randi(1E10);
fprintf(fmt,x)
Adapt it as necessary to your own code. Also, look at ‘fmt’ to see what the sprintf call produces.

4 comentarios

Hmm, its still not working for my code. From what I understand, we have %%%dd because we are going through the fprintf and sprintf function right? sprintf will use up %%d and the leftover %d is for fprintf?
high=2000;
low=1000;
number=rand*(high-low)+low;
fieldwidth=input('Enter a field width: \n');
phrase=sprintf('Your number is %%%dd \\n', fieldwidth);
fprintf(phrase,number)
Star Strider
Star Strider el 17 de Nov. de 2014
Editada: Star Strider el 17 de Nov. de 2014
That’s correct. It worked when I ran it.
The problem is that in your Question you said that you need to: ‘generate a random integer from 1000 to 2000’. You aren’t generating an integer with rand, since it will generate floating point numbers on the interval [0 1]. In your code, you’re simply multiplying the floating point numbers by 1000 and adding 1000.
Try this instead:
number = randi([1000 2000]);
See the documentation for randi for details.
David Phung
David Phung el 17 de Nov. de 2014
HEADDESK I TOTALLY FORGOT I WAS GENERATING INTEGERS. Thank you so much! I feel so stupid right now. agh. I really cant thank you enough.
Star Strider
Star Strider el 17 de Nov. de 2014
My pleasure!
You’re not the first to re-read the assignment after spending some time on it and realise you overlooked something significant. (Personal experience here!)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 16 de Nov. de 2014

Comentada:

el 17 de Nov. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by