answersLogoWhite

0


Best Answer

In C++, it is better to use vectors than dynamic arrays because a vector always knows its own size. C-style arrays are better suited to static arrays where the size is always known in advance. The following demonstrates how a vector might be used within a class.

#include<iostream>

#include<vector>

class foo

{

public:

std::vector<int>& operator+= (int data){ m_data.push_back(data); return(m_data);}

int operator[] (size_t element)const{

ASSERT(element<m_array.size());

return( m_array[element]; }

const size_t size()const{return(m_data.size());}

private:

std::vector<int> m_data;

}

int main()

{

foo f;

f += 42;

f += 9;

for(int i=0; i<f.size(); ++i)

std::cout<<f[i]<<std::endl;

}

Output:

42

9

User Avatar

Wiki User

βˆ™ 11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 13y ago

#include <iostream>

using std::cout;

using std::endl;

...

int main()

{

const int arraySize = 5;

double myArray [arraySize] = {0.0};

//Enter elements

...

//Display elements

for (int i = 0; i < arraySize; i++)

{

cout << myArray[i] << endl;

}

...

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: HOW to display array elements C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you help me with the C plus plus code of the program which has 10 index of array it adds 5 into every even elements of the array and then it subtracts 10 into the odd elements of the array?

int array[10] = {...}; for (int i = 0; i &lt; 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }


What is the lowest subscript of an array in c plus plus?

The lowest subscript of an array in C, or C++ is 0.


How arrays must be handled in c plus plus?

If the array is static it can declared in the structure itself: struct myArrayTag { int num[12]; // array of 12 integers (e.g., 48 bytes). } myArray; If it is dynamic then you must use a pointer and allocate the array outside the structure. You should also maintain a variable in the structure to keep track of how many elements the array currently has: struct myBufferTag { int * array; // Pointer to array of integers. int size; // Size of array (number of elements); } myBuffer;


How do you write a program in c plus plus to check if an array is symmetric?

To determine if an array is symmetric, the array must be square. If so, check each element against its transpose. If all elements are equal, the array is symmetric.For a two-dimensional array (a matrix) of order n, the following code will determine if it is symmetric or not:templatebool symmetric(const std::array& matrix){for (size_t r=0 ; r


How do you create an element in an array using C plus plus program?

If this is a homework related question, you really should consider trying to solve it yourself before looking at this answer. Otherwise, the value of the lesson, and the reinforcement provided by the assignment, will be lost to you. You do not create individual elements of arrays in C or C++. You create the array, and then reference the elements. Say you want to create a 10 x 20 array of floats. You would declare float MyFloatArray[10][20]. You would then reference the elements, such as MyFloatArray[3][8]. Note that 10 and 20 are the number of elements in each direction, but the valid range of indices is 0 to 9 and 0 to 19, respectively.

Related questions

How do you declare a string array and add elements to it in C plus plus?

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.


Can you help me with the C plus plus code of the program which has 10 index of array it adds 5 into every even elements of the array and then it subtracts 10 into the odd elements of the array?

int array[10] = {...}; for (int i = 0; i &lt; 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }


How to write a code for finding sum of left diagonals elements in an array using c plus plus?

truzi i Ghal


What is the lowest subscript of an array in c plus plus?

The lowest subscript of an array in C, or C++ is 0.


How do you make a C plus plus program that arrange the the numbers in ascending order?

Heres something i whipped up in a hurry... This uses the Bubble Sort method found (related links) #include &lt;iostream&gt; 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 &lt;&lt; "Before: {"; for (int c=0; c &lt;= arraysize; c++) { cout &lt;&lt; array[c]; if (c != arraysize) { cout &lt;&lt; ","; } } cout &lt;&lt; "}" &lt;&lt; endl; //Acctually sort the array int tmp=0; //Used for swaping values for (int loop=0; loop &lt;= (arraysize - 1); loop++) { for (int c=0; c &lt;= (arraysize - 1); c++) //The sort loop { if (array[c] &gt; 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 &lt;&lt; "After: {"; for (int c=0; c &lt;= arraysize; c++) { cout &lt;&lt; array[c]; if (c != arraysize) { cout &lt;&lt; ","; } } cout &lt;&lt; "}" &lt;&lt; endl; return 0; }


How arrays must be handled in c plus plus?

If the array is static it can declared in the structure itself: struct myArrayTag { int num[12]; // array of 12 integers (e.g., 48 bytes). } myArray; If it is dynamic then you must use a pointer and allocate the array outside the structure. You should also maintain a variable in the structure to keep track of how many elements the array currently has: struct myBufferTag { int * array; // Pointer to array of integers. int size; // Size of array (number of elements); } myBuffer;


Why in c plus plus index always start with 0?

C++ array indices are zero-based because the first element in any array is offset 0 elements from the start address. The second element is offset by 1 element and the third by 2 elements, and so on. To put it another way, the index refers to the number of elements that come before the desired element. The first element has zero elements before it, so it is index 0. For an array of n elements, the last element is at index n-1.


How do you write a program in c plus plus to check if an array is symmetric?

To determine if an array is symmetric, the array must be square. If so, check each element against its transpose. If all elements are equal, the array is symmetric.For a two-dimensional array (a matrix) of order n, the following code will determine if it is symmetric or not:templatebool symmetric(const std::array& matrix){for (size_t r=0 ; r


Program in c to delete an element in sorted array?

You cannot delete elements from an array. But you can move the elements: if (del_index &lt; no_of_elements-1) { memmove (&amp;array [del_index], &amp;array [del_index+1], sizeof (array [0]) * (no_of_elements - del_index - 1)); } --no_of_elements;


How do you write a program in C to find and display the sum of each row and column of a 2 dimensional array of type float?

array type


How do you create an element in an array using C plus plus program?

If this is a homework related question, you really should consider trying to solve it yourself before looking at this answer. Otherwise, the value of the lesson, and the reinforcement provided by the assignment, will be lost to you. You do not create individual elements of arrays in C or C++. You create the array, and then reference the elements. Say you want to create a 10 x 20 array of floats. You would declare float MyFloatArray[10][20]. You would then reference the elements, such as MyFloatArray[3][8]. Note that 10 and 20 are the number of elements in each direction, but the valid range of indices is 0 to 9 and 0 to 19, respectively.


Array implementation of priority queue example program in c plus plus?

yes