//Array Passer
//Demonstrates relationship between pointers and arrays
#include
<iostream>
using
namespace std;
void
increase(int* const array, const int NUM_ELEMENTS);
void
display(const int* const array, const int NUM_ELEMENTS);
int
main()
{
cout <<
"Creating an array of high scores.\n\n";
const
int NUM_SCORES = 3;
int
highScores[NUM_SCORES] = {5000, 3500, 2700};
cout <<
"Displaying scores using array name as a constant pointer.\n";
cout << *highScores << endl;
cout << *(highScores + 1) << endl;
cout << *(highScores + 2) <<
"\n\n";
cout <<
"Increasing scores by passing array as a constant pointer.\n\n";
increase(highScores, NUM_SCORES);
cout <<
"Displaying scores by passing array as a constant pointer to a constant.\n";
display(highScores, NUM_SCORES);
return
0;
}
void
increase(int* const array, const int NUM_ELEMENTS)
{
for
(int i = 0; i < NUM_ELEMENTS; ++i)
array
[i] += 500;
}
void
display(const int* const array, const int NUM_ELEMENTS)
{
for
(int i = 0; i < NUM_ELEMENTS; ++i)
cout <<
array[i] << endl;
}
The lowest subscript of an array in C, or C++ is 0.
yes
A multidimensional array in C or C++ is simply an array of arrays, or an array of an array of arrays, etc. for however many dimensions you want. int a; // not an array int a[10]; // ten int a's int a[10][20]; // twenty int a[10]'s, or 200 int a's int a[10][20][30]; // and so on and so forth...
int array[10] = {...}; for (int i = 0; i < 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }
A std::string is an object that encapsulates an array of type char whereas a C-style string is a primitive array with no members. A std::string is guaranteed to be null-terminated but a C-style string is not.
The lowest subscript of an array in C, or C++ is 0.
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.
yes
A multidimensional array in C or C++ is simply an array of arrays, or an array of an array of arrays, etc. for however many dimensions you want. int a; // not an array int a[10]; // ten int a's int a[10][20]; // twenty int a[10]'s, or 200 int a's int a[10][20][30]; // and so on and so forth...
No.
int array[10] = {...}; for (int i = 0; i < 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }
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; }
A std::string is an object that encapsulates an array of type char whereas a C-style string is a primitive array with no members. A std::string is guaranteed to be null-terminated but a C-style string is not.
int GetMaxElement( void * array) { if (array != 0) { return(max(array[], typeof(array))); } return(0); }
Harold C. Passer has written: 'The electrical manufacturers, 1875-1900'
type variable {[optional array size]} {= optional initializer};
An ordered list of data in any programming language is simply a sorted array or list. In C++ this can either mean a sorted array, vector, list or forward list.