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;
}
To append an element to an array you must reallocate the array with one more element than it currently has. Reallocation may expand the existing memory, or it may allocate fresh memory if there isn't room for expansion. If new memory is allocated, the existing elements will be copied automatically to the new location. Either way, you will have an empty element at the end of the array which will accommodate the new object.
Note that you cannot expand a static array -- the array must be allocated dynamically to begin with.
The example shown below demonstrates allocation and reallocation of a dynamic array. The code includes fail-safes should any allocation fail.
#include <iostream>
using namespace std;
int main()
{int max=25, i;
char *c, *o;
// Allocate memory for max char types.
if((c=(char*)malloc(max*sizeof(char)))==NULL) return(1); // Report error to caller.
// Initialise memory with consecutive letters (a to y).
for(i=0; i<max; ++i ) c[i]='a'+i;
// Print array.
for(i=0; i<max; ++i) cout<<c[i];
cout<<endl;
// Save current pointer in case of error.
o=c;
// Reallocate the memory to accommodate a new char.
if((c=(char*)realloc(c,++max*sizeof(char)))==NULL)
{ free(o); // Release original pointer.
return(1); // Return error to caller. }
// Insert new char at end of array.
c[max-1]='z';
// Print new array.
for(i=0; i<max; ++i) cout<<c[i]; cout<<endl;
// Release array.
free(c);
return(0); // Report success to caller.
}
Output:
abcdefghijklmnopqrstuvwxy
abcdefghijklmnopqrstuvwxyz
#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); }
using multidimensional array
** 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
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; }
plz as soon as possible give me the program for shorting an array in asscending order without using any sort function in c++
No.
A string in C is stored in a 1 dimension array so an array of strings is simply a two dimension array.
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.....
To write a C++ program to display the student details using class and array of object.