이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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 (한국어)