Program that orders data from highest to lowest
显示 更早的评论
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
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:
Mauro Mijangos
2021-6-1
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.
Mauro Mijangos
2021-6-2
Mauro Mijangos
2021-6-2
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
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.
Mauro Mijangos
2021-6-2
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 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!