My reversed vector is outputting on five different lines in the command window. How can I output to just one line?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Part of the assignment that I'm having trouble on calls for me to write a program that wants when a user inputs a 5 digit positive integer, the output would be the digits reversed.
Earlier in my assignment, it called for me to write a function when the user inputs a five digit positive integer, the output would be the five digits of that number. For example, five_digits(12345) = [1,2,3,4,5]. I completed this part with the code:
function N=five_digits(n)
N = (num2str(n))-'0';
end
In the workspace window, the N comes out to be [1,2,3,4,5].
So the part I'm having trouble on is the reversing of the digits and what the output is from the program I wrote. Here is the program I wrote for reversing of the digits using the five_digits function:
N = input('Input a five digit positive integer.\n');
C = fliplr(five_digits(N));
fprintf('%d\n', C);
When I run the program, if I enter 12345, my output would be 5 4 3 2 1, but each digit on five separate lines. However, in the workspace window, it does show that the digits were indeed reversed with [5,4,3,2,1].
What can I do to have the reversed vector output all on the same line just like how when I run the five_digits function when the digits are all spread out on one line?
0 Kommentare
Akzeptierte Antwort
Tom
am 12 Sep. 2012
The '\n' is creating a new line each time there is a digit:
fprintf('%d%d%d%d%d\n', C)
should work.
0 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!