+ 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; } } }
26 ответов
+ 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;
}
+ 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..?)
+ 2
* Accept single input number.
* From input number to 0, run loop by reducing 1 in each iteration
* print number.
edit: question changed?
+ 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..
+ 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]);
+ 1
Jayakrishna 🇮🇳 thank you for the info🙏🏽
+ 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?
+ 1
Do u want to change elements inside the massive?
Or just output?
+ 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();
+ 1
U are using 2 "for" cycle, but for what? This method is using for 2-dimensional massives. Don't forget about WHILE cycle
+ 1
Btw, don't forget about google, StackOverflow, habr and Cyberforum)
+ 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
+ 1
Jayakrishna 🇮🇳 thanks so much
+ 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
+ 1
Smith Welder great info. Thanks
+ 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":
+ 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);
}
}
}
}