+ 2

Help please

I want to get some number as input and output them in order from bigger to smaller int i,j,t; double a=new double[5]; for(i=0;i<a.Length;++i){ a[i]=double.Parse(Console.ReadLine()); } /*bubble alghoritm */ for(j=0;j<a.Length;++j){ for(i=0;i<a.Length-1;++i){ if(a[i]<a[i+1]){ t=a[i]; a[i]=a[i+1]; a[i+1]=t; } } }

9th Feb 2023, 6:55 PM
Learning Python
26 Answers
+ 2
You are using a[i] , and i not changing, it's value is already exceeded array length. instead you need a[mid]. And use < instead of > in if condition. while(low<=high){ if(a[mid]==10){ //(1) find=true; break; } if(a[mid]<10){ //(2) high=mid-1; } else{ low=mid+1; } j++; mid=(low+high)/2; }
10th Feb 2023, 11:02 AM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 3
You need to learn sorting algorithms. You are trying bubble sort algorithm, that required swapping of values when not in order. (Start loop from 1) https://www.sololearn.com/learn/774/?ref=app https://www.sololearn.com/learn/649/?ref=app (Is this links are working fine..?)
9th Feb 2023, 8:00 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 2
* Accept single input number. * From input number to 0, run loop by reducing 1 in each iteration * print number. edit: question changed?
9th Feb 2023, 7:04 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 2
--snip-- int i,n; int[] a=new int[5]; for(i=0;i<5;i++){ a[i]=Convert.ToInt32(Console.ReadLine()); } Array.Sort(a); Array.Reverse(a); foreach (var u in a) Console.Write(u); This is simple way, but has a lots ways for that..
9th Feb 2023, 7:30 PM
Smith Welder
Smith Welder - avatar
+ 1
--snip-- /* for(i=0;i<5;i++){ if(a[i]>a[i-1]){ Console.WriteLine(a[i]); } else{ Console.WriteLine(a[i-1]); } }*/ for(int u=4;u>=0;--u) Console.WriteLine(a[u]);
9th Feb 2023, 7:04 PM
Smith Welder
Smith Welder - avatar
9th Feb 2023, 7:44 PM
Smith Welder
Smith Welder - avatar
+ 1
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ thank you for the info๐Ÿ™๐Ÿฝ
10th Feb 2023, 4:38 AM
Learning Python
+ 1
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ Smith Welder i tried those links. I could get the ordering part by buble algorithm. Then i used binary search to check if 10 is in the array that i get it from the user... but it's not working. Why?
10th Feb 2023, 9:34 AM
Learning Python
+ 1
Do u want to change elements inside the massive? Or just output?
10th Feb 2023, 9:56 AM
Rhein Sakatoku
Rhein Sakatoku - avatar
+ 1
Anyway, u may use this code. int[] a = new int[5]; for (int i = 0; i < a.Length; ++i) { a[i] = Convert.ToInt32(Console.ReadLine()); } Array.Sort(a); Array.Reverse(a); for (int i = 0; i < a.Length; ++i) { Console.WriteLine(a[i]); } Console.ReadKey();
10th Feb 2023, 10:04 AM
Rhein Sakatoku
Rhein Sakatoku - avatar
+ 1
U are using 2 "for" cycle, but for what? This method is using for 2-dimensional massives. Don't forget about WHILE cycle
10th Feb 2023, 10:08 AM
Rhein Sakatoku
Rhein Sakatoku - avatar
+ 1
Btw, don't forget about google, StackOverflow, habr and Cyberforum)
10th Feb 2023, 10:13 AM
Rhein Sakatoku
Rhein Sakatoku - avatar
+ 1
I don't know, what are you talking about. I just now checked my code, everything is ok. Maybe problem is in compilation, try to use Microsoft Visual Studio, or onlinegbp
10th Feb 2023, 10:21 AM
Rhein Sakatoku
Rhein Sakatoku - avatar
10th Feb 2023, 10:25 AM
Rhein Sakatoku
Rhein Sakatoku - avatar
10th Feb 2023, 11:41 AM
Learning Python
+ 1
Don't write all in Main method. Make some methods and call its in main method. Get used to do so.. This is example for you with extensions methods, ref operators, etc.. https://code.sololearn.com/cI17b50S15Nx/?ref=app
10th Feb 2023, 1:38 PM
Smith Welder
Smith Welder - avatar
+ 1
Smith Welder great info. Thanks
10th Feb 2023, 2:07 PM
Learning Python
+ 1
def trade(crypto, budget, strategy): current_price = get_price(crypto) if current_price is None: print(f"Crypto {crypto} not found") return if strategy == "buy": if current_price < budget: print(f"Buying {crypto} at ${current_price}") # Place a buy order elif strategy == "sell": if current_price > budget: print(f"Selling {crypto} at ${current_price}") # Place a sell order else: print("Invalid strategy. Choose either 'buy' or 'sell'.") import time import requests import pandas as pd def get_price(crypto): url = f"https://api.coinmarketcap.com/v1/ticker/{crypto}/" data = requests.get(url).json() return float(data[0]["price_usd"]) def trade(crypto, budget, strategy): current_price = get_price(crypto) if strategy == "buy": if current_price < budget: print(f"Buying {crypto} at ${current_price}") # Place a buy order elif strategy == "sell":
11th Feb 2023, 7:38 AM
Stephen Dykes
11th Feb 2023, 12:50 PM
cheikh med
cheikh med - avatar
+ 1
using System; namespace OrderNumbers { class Program { static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); double[] numbers = new double[n]; Console.WriteLine("Enter the numbers:"); for (int i = 0; i < n; i++) { numbers[i] = Convert.ToDouble(Console.ReadLine()); } // sorting the numbers in descending order Array.Sort(numbers); Array.Reverse(numbers); Console.WriteLine("The numbers in descending order:"); foreach (double num in numbers) { Console.WriteLine(num); } } } }
11th Feb 2023, 5:58 PM
Jeovรก Emanuel