answersLogoWhite

0


Best Answer

int GetMaxElement( void * array)

{

if (array != 0)

{

return(max(array[], typeof(array)));

}

return(0);

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

int a[N] = { ... some data ...};

int i, m;

m=a[0];

for (i=1; i<N; i++) if (a[i] > m) m = a[i];

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: To find the largest element of an array in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a C plus plus function lastLargestIndex that returns the index of the last occurrence of the largest element in the array?

int lastLargestIndex(int a[],int n) //a=array, n= number of elements in array { int max=a[0],maxp=0; //max=largest no., maxp= position of largest no. for(int i=0;i&lt;n;i++) if(a[i]&gt;=max) { max=a[i]; maxp=i; } return maxp; }


How do you draw pascals triangle in gwbasic?

I suggest using an array with as many elements as the longest row you need. To keep it simple, keep two copies of the array, and calculate each element of the "new" array as the sum of the corresponding element, plus the previous element, of the "old" array. Then copy the information back for the next step.


C plus plus array-based lists?

If you mean an array where each element is a list, then the STL is your friend. To create an array of lists of any type T, use the following declaration: std::vector&lt;std::list&lt;T&gt;&gt; my_array_of_lists;


How do you write a program in c plus plus to remove fifth element in the array?

To remove any element from an array, you must overwrite every element from that point on with the element that follows it. That is, every element after the removed element must be copied to the preceding element. So to remove the 5th element, you copy the 6th element to the 5th element, the 7th to the 6th, and so on. Once all elements have been copied, you can reduce the array size by one element. Alternatively, you can leave the array size as is and simply maintain a count of the used elements (unused elements will always be at the end of the array).Note that in C++, a vector encapsulates an array of any type and provides member methods for manipulating that array, including the removal of individual elements. While manipulating arrays is a worthwhile exercise in determining "how things work", it's always better to make use of efficient, tried-and-tested code, rather than constantly re-inventing wheels to provide such common functionality.


Why upperbound of array in c plus plus overflow during runtime?

An array is simply a contiguous block of memory that is divided into one or more elements of equal size. The array name is itself a reference to the start address of the array, which has the same address as the first element in the array (the element with index 0). The index is essentially an offset from the start of the array, multiplied by the size of an element. However, there is no built-in mechanism in C to prevent you from accessing elements beyond the upper bound of the array at runtime -- essentially overflowing the array. Since C++ inherits from C, the same problem exists in C++. For instance, in a 10 element array, the upper bound is 9. If you attempt to write to element 10, you are overflowing the array, the buffer, because that memory does not belong to the array. You then introduce undefined behaviour. At best, nothing bad will happen. At worst, people could die. Once you introduce undefined behaviour there's simply no telling what could happen -- it's a time-bomb waiting to go off. The only way to avoid such problems is to ensure all your array offsets remain within the bounds of the array. That is, the onus is upon the C++ programmer -- just as it still is with the C programmer.

Related questions

How do you write a C plus plus function lastLargestIndex that returns the index of the last occurrence of the largest element in the array?

int lastLargestIndex(int a[],int n) //a=array, n= number of elements in array { int max=a[0],maxp=0; //max=largest no., maxp= position of largest no. for(int i=0;i&lt;n;i++) if(a[i]&gt;=max) { max=a[i]; maxp=i; } return maxp; }


How do you draw pascals triangle in gwbasic?

I suggest using an array with as many elements as the longest row you need. To keep it simple, keep two copies of the array, and calculate each element of the "new" array as the sum of the corresponding element, plus the previous element, of the "old" array. Then copy the information back for the next step.


C plus plus array-based lists?

If you mean an array where each element is a list, then the STL is your friend. To create an array of lists of any type T, use the following declaration: std::vector&lt;std::list&lt;T&gt;&gt; my_array_of_lists;


Why in c plus plus index always start with 0?

C++ array indices are zero-based because the first element in any array is offset 0 elements from the start address. The second element is offset by 1 element and the third by 2 elements, and so on. To put it another way, the index refers to the number of elements that come before the desired element. The first element has zero elements before it, so it is index 0. For an array of n elements, the last element is at index n-1.


How do you write a program in c plus plus to remove fifth element in the array?

To remove any element from an array, you must overwrite every element from that point on with the element that follows it. That is, every element after the removed element must be copied to the preceding element. So to remove the 5th element, you copy the 6th element to the 5th element, the 7th to the 6th, and so on. Once all elements have been copied, you can reduce the array size by one element. Alternatively, you can leave the array size as is and simply maintain a count of the used elements (unused elements will always be at the end of the array).Note that in C++, a vector encapsulates an array of any type and provides member methods for manipulating that array, including the removal of individual elements. While manipulating arrays is a worthwhile exercise in determining "how things work", it's always better to make use of efficient, tried-and-tested code, rather than constantly re-inventing wheels to provide such common functionality.


Why upperbound of array in c plus plus overflow during runtime?

An array is simply a contiguous block of memory that is divided into one or more elements of equal size. The array name is itself a reference to the start address of the array, which has the same address as the first element in the array (the element with index 0). The index is essentially an offset from the start of the array, multiplied by the size of an element. However, there is no built-in mechanism in C to prevent you from accessing elements beyond the upper bound of the array at runtime -- essentially overflowing the array. Since C++ inherits from C, the same problem exists in C++. For instance, in a 10 element array, the upper bound is 9. If you attempt to write to element 10, you are overflowing the array, the buffer, because that memory does not belong to the array. You then introduce undefined behaviour. At best, nothing bad will happen. At worst, people could die. Once you introduce undefined behaviour there's simply no telling what could happen -- it's a time-bomb waiting to go off. The only way to avoid such problems is to ensure all your array offsets remain within the bounds of the array. That is, the onus is upon the C++ programmer -- just as it still is with the C programmer.


Searching array element by using pointer in c plus plus?

The name of an array can be looked as a pointer of this array,and it points to the local memory address of the first element.So we can gave the address to a pointer.The flow is an easy example to show hou to use a pointer to print an array.#include "iostream.h"void main(){char a[]="abcdefgh";char *b=a;//afor(int i=0;i


What is the lowest subscript of an array in c plus plus?

The lowest subscript of an array in C, or C++ is 0.


Why you use loop in arrays in c plus plus?

We use loops with arrays because it's one the simplest methods of traversing an array. since each element in an array is the same size, every element can be access via an offset from the start of the array, using the array suffix operator []. Arrays employ zero-based offsets because the first element is always at the same address as the array itself. Thus the first element is at offset [0], while the third is at offset [2]. What this means is that if we multiply the size of an element by its offset index, we can determine the address of that element and therefore access that element's value. The subscript operator does this for us automatically, thus giving us constant-time random access to any element in the array. We can also use pointers to manually calculate the address of an element (which is what actually goes on behind the scenes). However, when we wish to traverse the array, one element at a time, a loop is the simplest method of doing so. The loop simply iterates through all the offset indices, beginning with offset 0, then 1, and 2, and so on. The final index is always 1 less than the number of elements in the array, because arrays are zero-based.


What is array in c plus plus in simple words?

An array is a contiguous block of memory that is used to store two or more variables (elements) of the same type, one immediately after the other. Each element in the array can be accessed in constant time via its index, thus permitting constant time random access to any element. Indices are zero-based, thus the first element is at index 0, the second at index 1, and so on. Arrays can be simple one-dimensional structures or more complex multi-dimensional structures. For instance, a two-dimensional array allows data to be treated as a table, with rows and columns, such that any element can be accessed in constant time via the intersection of its row and column index. Thus element [2][3] is the element in the 3rd row, 4th column.


Program in c plus plus to delete an element in sorted array?

If the array is dynamic then use a vector instead of an array. You can then use the vector::erase() function to delete an element or a range of elements. Remember that if the vector contains pointers to unshared memory, then you must release the pointer before erasing the element containing that pointer. If the array is static then you cannot delete elements. The assumption with static arrays is that you will neither add nor delete, you will only modify existing elements. However, you can emulate a deletion by shunting elements to the left, and keeping track of how many used elements there are (which must always be less than or equal to the upper bound plus one). Again, if the array contains pointers to unshared memory, you must release the pointer before shunting elements. You can also do the same thing with dynamic C-style arrays, but once you've shunted elements to the left you can reallocate the array with the new size to physically delete the final element.


How do you declare a string array and add elements to it in C plus plus?

You cannot add elements to a fixed array in C or C++. If, however, the array is declared as a pointer to an array, you can add elements by allocating a new array, copying/adding elements as needed, reassigning the new array to the pointer, and deallocating the original array.