Program that orders data from highest to lowest
2 次查看(过去 30 天)
显示 更早的评论
Mauro Mijangos
2021-6-1
Hello to all the community, I need your help to make a program in matlab that:
1 ask the user for the amount of data
2 that the user enter the data
3 that the program returns the data ordered from highest to lowest
4 that the program indicates if the greater number is even or odd
5 that the program indicates if the smaller number is odd or even
Greetings to all, and many for your help.
12 个评论
James Tursa
2021-6-1
What have you done so far? What specific problems are you having with your code?
Mauro Mijangos
2021-6-1
I don't know how to start the code, I don't know how to apply the bubble algorithm for this case.
James Tursa
2021-6-1
编辑:James Tursa
2021-6-1
For 1 and 2, see this function:
For 3, see the bubble sort algorithm here:
Just pick one of the pseudo-code examples and rewrite it using MATLAB syntax. E.g., use ( ) instead of [ ] and use 1-based indexing instead of 0-based indexing.
For 4 and 5, I don't understand the instructions. If it is simply to determine if the largest and smallest numbers are even or odd, see this function:
or this function:
James Tursa
2021-6-1
Work on getting just code for 1 and 2 first. Use an example from the link and see if you can get code that will ask the user for a number and store that in a variable. This is very simple code and you should be able to get it working very quickly by using the example.
Rik
2021-6-2
Which part doesn't work? I see you only used input to ask the user how many numbers they want to enter, but not to actually enter them. (your code also has trailing end, which I will assume is a typo)
I would also suggest adding comments about what your code is doing, preferably in English, as that would make it easy to share here. I took the liberty or re-translating the instructions.
clear,clc
%step 1: ask the user for the amount of data
tamano=input('INGRESE NUMERO DE DATOS\n');
%step 2: let the user enter the data
fprintf('INTRODUZCA LOS %d DATOS',tamano);
%step 3a: sort the data highest to lowest
%this is the bubble sort algorithm
cont=0
cont=cont+1
for j = i+1:i<tamano-1:cont
for j=i+1:j<tamano:cont
if(num(j)<num(i))
temp=num(j);
num(j)=num(i);
num(i)=temp
end
end
end
%step 3b: return the data ordered from highest to lowest
fprintf('LOS NUMEROS ORDENADOS SON:\n')
for i=0:i<tamano:cont
fprintf('%d,'mun(i))
end
%step 4: indicate if the largest number is even or odd
%step 5: indicate if the smallest number is odd or even
fprintf('\n\n\n')
Mauro Mijangos
2021-6-2
when I run the progrmam it marks me this:
INGRESE NUMERO DE DATOS
20
INTRODUZCA LOS 20 DATOSWarning: Colon operands must be real scalars.
> In MAYOR_A_MENOR (line 14)
LOS NUMEROS ORDENADOS SON:
Warning: Colon operands must be real scalars.
> In MAYOR_A_MENOR (line 26)
clear
clc
%step 1: ask the user for the amount of data
tamano=input('INGRESE NUMERO DE DATOS\n');
%step 2: let the user enter the data
fprintf('INTRODUZCA LOS %d DATOS',tamano);
%step 3a: sort the data highest to lowest
%this is the bubble sort algorithm
cont=0;
cont=cont+1;
for j = i+1:i<tamano-1:cont
for j=i+1:j<tamano:cont
if(num(j)<num(i))
temp=num(j);
num(j)=num(i);
num(i)=temp;
end
end
end
%step 3b: return the data ordered from highest to lowest
fprintf('LOS NUMEROS ORDENADOS SON:\n')
for i=0:i<tamano:cont
fprintf('%d',mun(i))
end
%step 4: indicate if the largest number is even or odd
%step 5: indicate if the smallest number is odd or even
fprintf('\n\n\n')
Jan
2021-6-2
编辑:Jan
2021-6-2
What is the purpose of this code:
cont=0;
cont=cont+1;
Easier:
cont = 1;
Your code fails in this line:
for j = i+1:i<tamano-1:cont
because i is not defined as a variable. Then it is the the imaginary unit.
There are more problem:
- "fprintf('INTRODUZCA LOS %d DATOS',tamano);" This does no let the user input data. Use the input() command again in a loop.
- "for j = i+1:i<tamano-1:cont" - Please read the documentation:
doc for
Better:
for i = 1:tamano
for j = i+1:tamano
3. The same problem here: "for i=0:i<tamano:cont". This is not the way for loops work in Matlab.
Rik
2021-6-2
You are being helped with the code. Did you read the documentation for the functions Jan suggested? We are not going to do your homework for you.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)