array and loop problem

Hi.It's not working.I am learning english.My english language is very bad.ok
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdlib>
#include <iostream>

using namespace std;

int maxdizi(int dizi[],int i) {
    int buyuksayi=0;
    int count=0;
    for(int j=0; j<i; j++) {
            for(int z=0; z<i; z++) {
                    if(dizi[j]==dizi[z]){
               count++;
               }
              if(dizi[j]>dizi[z]) {
                                  count++;
                                  }
                                  }
              
              if(count==i) {
                           buyuksayi=dizi[j];
                           
                           }
                           }
              return buyuksayi;
              }
                                                              

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;
}

Do you understand it?I am sorry but I want to show most big number of array.thank you.
Last edited on
Reformatted to something remotely readable:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cstdlib>
#include <iostream>

using namespace std;

int
maxdizi (int dizi[], int i)
{
  int buyuksayi = 0;
  int count = 0;
  for (int j = 0; j < i; j++)
    {
      for (int z = 0; z < i; z++)
        {
          if (dizi[j] == dizi[z])
            {
              count++;
            }
          if (dizi[j] > dizi[z])
            {
              count++;
            }
        }
      if (count == i)
        {
          buyuksayi = dizi[j];
        }
    }
  return buyuksayi;
}


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;
}


What exactly is your question?
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?.
The algorithm to find the greatest element in an array is
max=0
for a=1 to array.size-1{
if array[a]>array[max] then{
max=a
}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>

using namespace 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.
ok.thank you very much.I am working on this program..I will untie it.
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];

// call bubble sort function
bubble(numbers, size);
cout<<"the sorted numbers are"<<endl<<endl;

//repeating back the bubble sort (final sort)
for(int idx =0; idx <= MAX_ARRAY; idx++)
{
cout<<numbers[idx];
cout<<endl;
}

cout<<"hit any key+return to exit";
cin>>excape;
return 0;
}
// bubble sort function
// globals none


void bubble(int numbers[], int size)
{
cout<<size;
int temp,a,b;
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;}
}//end of bubble sort

Last edited on
Bubble sort is an O(n^2) algorithm to implement a function that should run in linear time!

To add to jsmith's comment, sorting is an acceptable solution if you were going to sort the data set afterwards anyway.
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
Last edited on
understand thanks IDE10T ;)
Topic archived. No new replies allowed.