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 Commenti

What have you done so far? What specific problems are you having with your code?

I don't know how to start the code, I don't know how to apply the bubble algorithm for this case.

James Tursa
James Tursa il 1 Giu 2021
Modificato: James Tursa il 1 Giu 2021
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:
Can you help me with the code??
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.
I tried to make the code, but it did not work, I attach the file.
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')
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
Jan il 2 Giu 2021
Modificato: Jan il 2 Giu 2021
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:
  1. "fprintf('INTRODUZCA LOS %d DATOS',tamano);" This does no let the user input data. Use the input() command again in a loop.
  2. "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.
can you help me with the code ??
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.

Accedi per commentare.

Risposte (0)

Categorie

Prodotti

Release

R2020a

Richiesto:

il 1 Giu 2021

Commentato:

Rik
il 2 Giu 2021

Community Treasure Hunt

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

Start Hunting!

Translated by