Durring my last semester, we were given a bonus quiz to write a function the received and numberical input and produced the revise as an output.
example a=123
b=321
we were not allowed to use any string variable or function. Any ideas now that the semester is over Id like to know what you think.
My original thought was to divide the input but the got harder with larger number as the function should receive any number

1 件のコメント

Joseph Pauwels
Joseph Pauwels 2014 年 5 月 7 日
boy it is a good thing this was not an English class. that spelling and miss used work are horrible. I am glad yall knew what I meant

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2014 年 5 月 7 日

0 投票

This works, but obviously only for integers:
a = 123;
% a = fix(rand*1E6) % Test Integer
La = fix(log10(a));
x = a;
for k1 = La:-1:0
d(k1+1) = fix(x/(10^k1));
x = rem(x,10^k1);
end
v10 = 10.^(La:-1:0)';
Flipped_a = d*v10
The Flipped_a variable is the result. I tested it on other random integers as well.

3 件のコメント

Joseph Pauwels
Joseph Pauwels 2014 年 5 月 7 日
that was the point, only on integers. fix and rem arent string variables right?
Star Strider
Star Strider 2014 年 5 月 7 日
The fix and rem functions are not string functions.
  • The fix function rounds toward zero.
  • The rem function (similar to mod) returns the remainder after division.
Wesley Ooms
Wesley Ooms 2014 年 5 月 8 日
fix, log10, rand, and rem are functions. The way i read the question is that no function is allowed.

サインインしてコメントする。

その他の回答 (4 件)

Carlos
Carlos 2014 年 5 月 7 日

0 投票

a=[1 2 3 4 5];
>> b=zeros(1,length(a));
count=1;
l=length(a);
while(count<=l)
b(count)=a(l+1-count);
count=count+1;
end

1 件のコメント

Joseph Pauwels
Joseph Pauwels 2014 年 5 月 7 日
Thanks Carlos, that i could do. but not what i meant 12345 not 1 2 3 4 5.

サインインしてコメントする。

Wesley Ooms
Wesley Ooms 2014 年 5 月 7 日

0 投票

totally not optimized, but this will do the trick:
clear a b
a=32385
i=1
while floor(a/10)
b(i)=a-floor(a/10)*10
i=i+1
a =floor(a/10)
end
b(i)=a
d=1
e=0
for c=0:numel(b)-1
e=e+d*b(end-c)
d=d*10
end

5 件のコメント

Wesley Ooms
Wesley Ooms 2014 年 5 月 7 日
ok, i see i used 2 functions (floor and numel) you can get around the numel by placing the part of the for loop in the while loop, but i don't kno how to get around floor. I think Carlos answer is not what your teacher meant.
Wesley Ooms
Wesley Ooms 2014 年 5 月 7 日
you can get around floor by using this:
clear a b
a=132385
i=1
c=0
d=1
e=0
while a
c=-1:(a/10)
a1=c(end)
b(i)=a-a1*10
a=a1
i=i+1
end
for c=0:i-2
e=e+d*b(end-c)
c=c+1
d=d*10;
end
Joseph Pauwels
Joseph Pauwels 2014 年 5 月 7 日
it doesn't spit out an answer, it just keeps running
here my code
function [b] = reverse( a ) %UNTITLED2 Summary of this function goes here % Detailed explanation goes here
i=1; c=0; d=1; e=0;
while a c=-1:(a/1); a1=c(end); b(i)=a-a1*10; a=a1; i=i+1; end for c=0:i-2; e=e+d*b(end-c); c=c+1; d=d*10; end end
Wesley Ooms
Wesley Ooms 2014 年 5 月 8 日
編集済み: Wesley Ooms 2014 年 5 月 8 日
that is because you
do c=-1:(a/1); instead of c=-1:(a/10);
the following must work: (but i admit it is not optimized for speed)
function b=swapnumber(a)
b = 0;
while a
c = -1:a/10;
c = c(end);
b = (b+a-c*10)*10;
a = c;
end
b = b/10;
Wesley Ooms
Wesley Ooms 2014 年 5 月 8 日
the following code also works but is faster for large numbers since it predetermines the size of the number
function b=swapnumber(a)
b=0;d=0;while a>1;a=.1*a;d=d+1;end
for i=0:d;c=0:a*10;c=c(end);b=b+c*10^i;a=a*10-c;end

サインインしてコメントする。

Sagar Damle
Sagar Damle 2014 年 5 月 7 日
編集済み: Sagar Damle 2014 年 5 月 8 日

0 投票

I think the code which I am going to put here is the standard code to reverse a number.(This code is used in C language,of course,according to its own syntax!)Also,it is easy to understand.Remember this code,I think it is very helpful!
a = 126986;
b = a; % Save value of "a" in new variable "b".
reverse = 0;
while b > 0 % OR while b ~= 0 (Both 'while' statements are same.)
r = rem(b,10);
reverse = reverse * 10 + r;
b = floor(b/10);
end
a
reverse
Joshua Amuga
Joshua Amuga 2016 年 11 月 2 日

0 投票

IVP :=ode({y''(x)+4*y'(x)+3*y,y(0)=3,y'(0)=4},y(x)); Undefined function or variable 'IVP'. this is what i got

カテゴリ

ヘルプ センター および File ExchangeSimulink Environment Customization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by