It is possible, if it is a dynamically allocated array:
int n= 0; Elem *p= NULL;
...
AddElem (Elem newval)
{
n += 1; p = (Elem *)realloc (p, n*sizeof (Elem));
p[n-1] = newval;
}
#include<iostream> void append(std::vector<int>& v, int i){ v.push_back(i); } int main() { std::vector<int> v; append( v, 100 ); // same as calling v.push_back(100); return(0); }
** pseudo code ** if array length == 1, return first element else if array length > 1 max = first element for second item to last item if item > max max = item return max else // array length is 0 error
using multidimensional array
the example of array over charcter variables is char ["string"]
If you are using an array : sort using qsort() then take middle element.
#include<iostream> void append(std::vector<int>& v, int i){ v.push_back(i); } int main() { std::vector<int> v; append( v, 100 ); // same as calling v.push_back(100); return(0); }
To determine the size of an array in C using the keyword sizeof, you would use the syntax: sizeof(array) / sizeof(array0).
** pseudo code ** if array length == 1, return first element else if array length > 1 max = first element for second item to last item if item > max max = item return max else // array length is 0 error
using multidimensional array
the example of array over charcter variables is char ["string"]
Heres something i whipped up in a hurry... This uses the Bubble Sort method found (related links) #include <iostream> using namespace std; int main(int argc, const char* argv) { int arraysize = 5; //Unsorted array size int array [] = { 5, 3, 4, 2, 1 }; //The array of numbers itself //Display the unsorted array cout << "Before: {"; for (int c=0; c <= arraysize; c++) { cout << array[c]; if (c != arraysize) { cout << ","; } } cout << "}" << endl; //Acctually sort the array int tmp=0; //Used for swaping values for (int loop=0; loop <= (arraysize - 1); loop++) { for (int c=0; c <= (arraysize - 1); c++) //The sort loop { if (array[c] > array[c + 1]) { //Swaps the two values in the array tmp = array[c]; array[c] = array[c + 1]; array[c + 1] = tmp; //Cleanup tmp = 0; } } } //Display the sorted array cout << "After: {"; for (int c=0; c <= arraysize; c++) { cout << array[c]; if (c != arraysize) { cout << ","; } } cout << "}" << endl; return 0; }
If you are using an array : sort using qsort() then take middle element.
public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i < array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }
A string in C is stored in a 1 dimension array so an array of strings is simply a two dimension array.
plz as soon as possible give me the program for shorting an array in asscending order without using any sort function in c++
No.
You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....