//This is for kth largest element. (So this is for n-k smallest element) //Sudipta Kundu [Wipro Technologies] #include <stdio.h> //Input: array with index range [first, last)
//Output: new index of the pivot. An element in the middle is chosen to be a pivot. Then the array's elements are
//placed in such way that all elements <= pivot are to the left and all elements >= pivot are to the right.
int positionPivot(int* array, int first, int last); //Input: array with index range [first, last) and integer K (first <= K < last)
//Output: array whose Kth element (i.e. array[K]) has the "correct" position. More precisely,
//array[first ... K - 1] <= array[K] <= array[K + 1 ... last - 1]
void positionKthElement(int* array, int first, int last, int k); int main() {
int array[] = {7,1,8,3,1,9,4,8};
int i;
for (i = 0; i < 8; i++) {
positionKthElement(array, 0, sizeof(array) / sizeof(array[0]),i);
printf("%d is at position %d\n", array[i], i);
}
return 0;
}
int positionPivot(int* array, int first, int last) {
if (first last)
return first; int tmp = (first + last) / 2;
int pivot = array[tmp];
int movingUp = first + 1;
int movingDown = last - 1;
array[tmp] = array[first];
array[first] = pivot;
while (movingUp <= movingDown) {
while (movingUp <= movingDown && array[movingUp] < pivot)
++movingUp;
while (pivot < array[movingDown])
--movingDown;
if (movingUp <= movingDown) {
tmp = array[movingUp];
array[movingUp] = array[movingDown];
array[movingDown] = tmp;
++movingUp;
--movingDown;
}
}
array[first] = array[movingDown];
array[movingDown] = pivot;
return movingDown;
} void positionKthElement(int* array, int first, int last, int k) {
int index;
while ((index = positionPivot(array, first, last)) != k) {
if (k < index)
last = index;
else
first = index + 1;
}
}
1) Use any method you like, for example a bubble sort or a quick sort, to sort the array in ascending order.
2) Pick the kth element.
If you are using an array : sort using qsort() then take middle element.
int findMax(int *array) { int max = array[0]; for(int i = 1; i < array.length(); i++) { if(array[i] > max) max = array[i] } return max; }
1010
Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.
The simplest way is probably to read the numbers into an array and then prints each element of the array starting at the last one and moving backwards.
If you are using an array : sort using qsort() then take middle element.
int findMax(int *array) { int max = array[0]; for(int i = 1; i < array.length(); i++) { if(array[i] > max) max = array[i] } return max; }
1010
Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.
The simplest way is probably to read the numbers into an array and then prints each element of the array starting at the last one and moving backwards.
/* using ellipses (...) to indicate tabs for clarity */ double largest (double *array, int M, int N) { ... int i, j; ... double *element; ... double answer = array[0][0]; ... for (i=0; i<M; i++) { ... ... for (j=0; j<N; j++) { ... ... ... element = array + i*M + j; ... ... ... if (*element > answer) answer = *element; ... ... } ... } ... return answer; }
#include<stdio.h> void main() { int a[10],n,i,large,small; printf("enter the value of n\n"); scanf("%d",&n); printf("enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); large=a[0]; small=a[0]; for(i=1;i<n;i++) { if(a[i]>large) large=a[i]; if(a[i]<small); small=a[i];} printf("largest element in the array id %d\n",large); printf("smallest element in the array is %d\n",small); }
== Java does not allow reference to memory locations. == In C: for (i=0; i<n; ++i) printf ("a[%d] is at %p\n", i, &a[i]);
The results of that programming error is undefined. You must NEVER EVER write, or EVEN READ an array element beyond the allocated size of the array. Period.I would flunk a student that consistently did this, and I would fire a programmer that did the same.
If you have the series stored in an array, you loop through the array and print each array element in turn. Another possibility is to print out the numbers in the series as you generate them. In that case, you may not need to store anything (depending on the series, of course).
// Assuming you dynamically allocated this array using "new"... delete array[arraysize - 1]; arraysize--;
Reference:cprogramming-bd.com/c_page1.aspx# array programming