現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)