Max element in array

How to find a max element in array using only pointers?
Using the pointer, look at each element in the array, using the pointer to read the value and then changing the pointer to point to the next value, and keep track of the max value as you go through the array.
The Reference documentation on this site has a page about C++ Standard Library algorithm std::max_element. Worth a read.
I did max element now I have to modify my array in way : first element go to last second to the previous last and etc
1
2
3
4
5
6
7
8
9
10
 while(f<=g)
  {
     int temp=*f;
     *f=*g;
     *g=temp;
     f++;
     g--;
     cout<<*f<<endl;
  }

Now my code look like this but doesnt work. I have to create the same array but in reverse order without using array's index.
This is also known as "reversing" the array.

std::reverse(array, array+size_of_array);
The Reference documentation on this site for std::reverse shows possible implementation:
1
2
3
4
5
6
7
8
9
template <class BidirectionalIterator>
  void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
  while ( (first != last)&&(first != --last) )
  {
    std::iter_swap( first, last );
    ++first;
  }
}

How does that differ from your version?

It does not print anything. Reversing an array and showing the array are and should be separate operations.

If you don't use the return value, then ++foo and foo++ achieve the same.
On array the f<=g is safe. On linked list it is not.
Your lines 3-5 do the same as the iter_swap().

Your code:
1
2
3
4
5
6
while ( first <= last )
{
  std::iter_swap( first, last );
  ++first;
  --last;
}

Looks almost the same. Not quite though. For example, why swap if first==last? That is what you do.

We can't see how you initialize the f and g, nor what "doesn't work" mean.
Last edited on
1
2
3
4
5
6
7
8
9
10
*w=*g;
  while(f<=w)
  {
     int temp=*f;
     *f=*g;
     *g=temp;
     f++;
     g--;

  }

My code reverse first three elements and further is bad. I have no idea why.
I initalize f as pointer of first element in array and g is pointer of last element in aray.
Last edited on
I initalize f as pointer of first element in array and g is pointer of last element in aray.


Could you show us how did you do it? It will be easier to pinpoint what is going.

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
#include <iostream>

void reverseArray(int *, int *, int);
void printArray(int *, int);

int main()
{
	const int arraySize = 10;
	int original[arraySize]{23,55,65,11,77,56,43,79,22,34};
	int reverse[arraySize];

	std::cout << "Original order...\n";
	printArray(original, arraySize);
	std::cout << "Reverse order...\n";
	reverseArray(original, reverse, arraySize);
	printArray(reverse, arraySize);

	return 0;
}

/*
	The reverse function will use two parameters that are integer
	pointers to traverse the array from the original order to reverse
	order. It has two counters, which one counter will start at the 
	first element and the other counter will start at the last element
	of the array. The third parameter is the size of the array to be 
	used in the for loop to iterate the elements.
*/
void reverseArray(int *numbers, int* reverse, int arraySize)
{
	int countDec{ arraySize - 1};
	for (int countInc = 0; countInc < arraySize; countInc++)
	{
		*(reverse + countInc) = *(numbers + countDec);
		countDec--;
	}
}

void printArray(int *arr, int arraySize)
{
	for (int count = 0; count < arraySize; count++)
		std::cout << *(arr + count) << std::endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
array input(random numbers)
for example T[10]
int *f=&t[0];         // first element of array
int *l =&t[9];        //last element of array


 *w=*l; // create *w as the no changing element
  while(f<=w)
  {
     int temp=*f;
     *f=*l;
     *l=temp;
     f++;
     l--;

  }
for(int i=0; i<10; i++)         //output array
{
cout<<T[i];

}

  }
Last edited on
[EDIT] I would use the suggestion of using the STL algorithm; however, it seems that it is an assignment requirement.
To give you an idea...
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
#include <iostream>


int main()
{
	const int arraySize {10};
	int T[arraySize]{ 23,55,65,11,77,56,43,79,22,34 };
	int *f = &T[0];
	int *g = &T[arraySize - 1];


	std::cout << "Original Order:\n";
	for (int count = 0; count < 10; count++)
		std::cout << *(T + count) << std::endl;

	std::cout << "Reversing the Order...\n";
	while (f <= g)
	{
		int temp = *f;
		*f = *g;
		*g = temp;
		f++;
		g--;
	}

	std::cout << "Reverse Order:\n";
	for (int count = 0; count < 10; count++)
		std::cout << *(T + count) << std::endl;

	return 0;
}


Output...

Original Order:
23
55
65
11
77
56
43
79
22
34
Reversing the Order...
Reverse Order:
34
22
79
43
56
77
11
65
55
23
Last edited on
Topic archived. No new replies allowed.