For instance, you have array of type int with a name myArray, and you do not know size of the array. You can use following statement to get it:
int arraySize = myArray/myArray[0];
arraySize gives you number of elements in myArray.
Traverse the array from index 0 until you find the number. Return the index of that number.
(array.length - 1) will find the index of the last element in an array (or -1 if the array is empty).
by using index position we can find the particular element in array.
Basically, &array[i]; That is, the memory location for an array object with index i. Or, you can do: (array + i);
#include<stdio.h> #include<conio.h> main() { int a[100]; int n,largest,index,position; printf("enter the number of elements in the array"); scanf("%d",&n); printf("enter %d elements",n); for(index=0;index<n;index++) scanf("%d",&a[index]); largest=a[0]; position=0; for(index=1;index<n;index++) if(a[index]>largest) { largest=a[index]; position=index; } printf("largest element in the array is %d\n",largest); printf("largets element's position in the array is %d\n",position+1); getch(); }
The simplest way is usually to iterate through an array using a loop and store either the index or the value of the highest number you find. For example: int findLargestIndex(int *array, int arraysize) { int largestIndex = 0; for(int i = 0; i < arraysize; i++) { if(array[i] > array[largestIndex]) largestIndex = i; } return largestIndex; }
The processor makes no difference in C programming -- the compiler will generate the appropriate instructions for you. To find the largest number in a sequence of numbers, store the numbers in an array. Then invoke the following function, passing the array and its length: unsigned largest (double* num_array, unsigned size) { if (!num_array !size) return size; unsigned max = 0; unsigned index; for (index=1; index<size; ++index) if (num_array[index]>num_array[max]) max = index; return max; } The return value holds the index of the largest value in the array.
It will be a loop if you want to try each number in turn. Make sure you make some way to get out of the loop, or it will run forever.First, you will have to find all factors of the number and store them in an array. divide the index number by every number between it and 1. There won't be any decimals if it is a factor.Note: the divisor and the answer will both be factors.Then, add up all of the items in that array and compare it to the index number. If there is a match, then you've found a perfect number!If you made it so the index number is part of the array, then you'll have to remove it by halving the added array's result.The first four perfect numbers are 6, 28, 496 and 8128.
This is very common exception and name of the exception class tells exactly what kind of problem you have. If you would look into stack trace, which should have been generated too you would be able to find exact place where it happened. The problem is that "Array Index Out Of Bounds". This means that you used index on array which is invalid. That could negative number, because all arrays starts from 0. If you array has N items and you will try to get item with index N or higher you will get this exception too. Only available indexes are from 0 to N - 1, where 0 points to the first item and N - 1 to the last one.
To find the median of an array of numbers, first, arrange the numbers in ascending order. If the array has an odd number of elements, the median is the middle number. If the array has an even number of elements, the median is the average of the two middle numbers.
#include<stdio.h> // returns the index of the largest value in the array int max (int a[], unsigned size) { if (!size) return -1; // invalid array int index = 0; // assume index 0 holds the largest value (so far) for (int i=1; i<size; ++i) { // traverse the remainder of the array if (a[i]>a[index]) { // compare with the current largest index = i; // the current value is larger } } return index; // a[index] holds the largest value } int main (void) { int x[10] = {7, 4, 3, 9, 5, 2, 1, 8, 6}; printf ("The largest value in the array is %d\n", max (x, 10)); return 0; }
array.length will return the number of elements in array.