thank you for answer.It's not working.I don't see 10000. I'am see 0.so I want to see 10000.This program have a problem.But i dont find the problem.please Where is the problem?.
#include <cstdlib>
#include <iostream>
usingnamespace std;
int
maxdizi (int array[], int i)
{
int buyuksayi = 0;
int count = 0;
int max;
max=0;
for (int a=1; a<i-1; a++) {
if (array[a]>array[max]){
max=a;
}
}
}
int main (int argc, char *argv[])
{
int dizi2[] = { 900, 10000, 7000, 300, 30 };
int buyuk;
buyuk = maxdizi (dizi2, 5);
cout << buyuk << endl;
system ("PAUSE");
return EXIT_SUCCESS;
}
helios thanks for answers but didn't working.I want to see 10000.but I'am seeing 4 now.I'am really tired.because this program very very very bad:((
The algorithm I gave you places the index of the maximum value in max, not the maximum value itself.
Also, your function isn't returning anything, and your for isn't checking the last element.
Alternatively, you can use the existing tool of the language instead of reinventing the wheel. Then you won't have to write additional functions to do this with a floating point value or some other user defined type. maxidizi only works for integers but std::max_element is a template function and works for any type that has an adequate operator< defined.
1 2 3 4 5 6 7 8
#include <algorithm>
int main (int argc, char *argv[])
{
int dizi2[] = { 900, 10000, 7000, 300, 30 };
cout << *std::max_element(dizi2, dizi2 + 5) << endl;
system ("PAUSE");
return EXIT_SUCCESS;
}
You can use this bubble sort to find the biggest number. the biggest number will be moved to the beginning of the array. You can modify to sort characters or strings.
// Bubble sort for numbers
#include <iostream>
using namespace std;
// 10 unit array for numbers
const int MAX_ARRAY = 9;
int numbers[MAX_ARRAY];
// Bubble sort function (prototype)
void bubble(int numbers[], int size);
int main ()
{
char excape;
int size;
cout<<"enter 10 numbers to be sorted"<<endl<<endl;
for(size =0; size <= MAX_ARRAY; size++)
cin>>numbers[size];
very good.Thank you ID10T ERROR.
but
for(a=0; a<=size-2; a++)
for(b=a+1; b<=size-1; b++)
if (numbers[b]>numbers[a])
{
temp=numbers[a];
numbers[a]=numbers[b];
numbers[b]=temp;}
}
why size-2 and size-1
the range of A is 0-8 and the range of B is 1-9 if size-2 and size-1 are placed. For each A you compare to B. As you notice the whole range of the array is covered by both A and B, but it is offset by 1. size-2 and size-1 is to cut down on unnecessary loops. the program runs fine setting them both to just size.
if you input 12,13,14,15,16,17,18,19,20,21
it will take 55 loops to sort the numbers, if size-2, and size-1 are placed the amount of loops is cut down to 45.
the resulting output is the same 21,20,19,18,17,16,17,16,15,14,13,12