answersLogoWhite

0

It is not possible to show a flowchart in this website -- it is text only. The algorithm can be summarised as follows:

int sum(std::array<int>& a)

{

int sum = 0; // initialise the return value

for (auto i : a) // for each value in the array

sum += i; // increment the sum by the value

return sum; // return the sum

}

User Avatar

Wiki User

10y ago

What else can I help you with?

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; }


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;


Would you Write c plus plus program using array for Fibonacci number?

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.....


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 draw pascals triangle in gwbasic?

I suggest using an array with as many elements as the longest row you need. To keep it simple, keep two copies of the array, and calculate each element of the "new" array as the sum of the corresponding element, plus the previous element, of the "old" array. Then copy the information back for the next step.

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 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 to write a code for finding sum of left diagonals elements in an array using c plus plus?

truzi i Ghal


Example of flowchart of while loop in c plus plus?

kk


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 ignore certain components of an array in C plus plus?

The simplest way is to simply not place those elements in the array in the first place. If that is not possible, then one solution would be to make a single traversal of the array, deciding which elements must be ignored as you go, and building another array of pointers to those elements that must be processed. However, if the decision is a fairly simple one, then you don't need a separate array, you can simply make the decision as you process the array. You're not physically ignoring the elements, you're simply deciding which elements to process. Which method you use ultimately depends on how complex the decision is and whether that decision needs to be remembered for any subsequent traversals or not. But in order to physically ignore an element, the element must be removed from the array, which would require at least one complete traversal to build a new array. It's up to you to determine if it's worthwhile creating a new array or not.


Would you Write c plus plus program using array for Fibonacci number?

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.....


Program in c plus plus to delete an element in sorted array?

If the array is dynamic then use a vector instead of an array. You can then use the vector::erase() function to delete an element or a range of elements. Remember that if the vector contains pointers to unshared memory, then you must release the pointer before erasing the element containing that pointer. If the array is static then you cannot delete elements. The assumption with static arrays is that you will neither add nor delete, you will only modify existing elements. However, you can emulate a deletion by shunting elements to the left, and keeping track of how many used elements there are (which must always be less than or equal to the upper bound plus one). Again, if the array contains pointers to unshared memory, you must release the pointer before shunting elements. You can also do the same thing with dynamic C-style arrays, but once you've shunted elements to the left you can reallocate the array with the new size to physically delete the final element.


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

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


Dynamic memory allocation in c plus plus?

The following function demonstrates a dynamic array of integers. The array is dynamically allocated a random number of elements, from 1 to 100, with pointer p pointing at the start address (&amp;p[0]). Note that the number of elements needn't be random, but unlike a static array which has a constant size and can therefore be allocated in the data segment by the compiler, the number of elements in a dynamic array is not known in advance and must therefore be allocated on the free store at runtime. The number of elements is variable. #include &lt;iostream&gt; #include &lt;time.h&gt; int main() { srand(( unsigned ) time( NULL )); int elements = rand() % 100 + 1; int * p = new int[elements]; // do something with array... // p[0] will return the first element // p[elements-1] returns the last element. delete [] p; // release memory. p = NULL; return( 0 ); }


How do you draw pascals triangle in gwbasic?

I suggest using an array with as many elements as the longest row you need. To keep it simple, keep two copies of the array, and calculate each element of the "new" array as the sum of the corresponding element, plus the previous element, of the "old" array. Then copy the information back for the next step.