answersLogoWhite

0


Best Answer

int array[10] = {...};

for (int i = 0; i < 10; ++i) {

if (i % 2 == 0) array[i] += 5;

else array[i] -= 10;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: 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?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a program in c plus plus to remove fifth element in the array?

To remove any element from an array, you must overwrite every element from that point on with the element that follows it. That is, every element after the removed element must be copied to the preceding element. So to remove the 5th element, you copy the 6th element to the 5th element, the 7th to the 6th, and so on. Once all elements have been copied, you can reduce the array size by one element. Alternatively, you can leave the array size as is and simply maintain a count of the used elements (unused elements will always be at the end of the array).Note that in C++, a vector encapsulates an array of any type and provides member methods for manipulating that array, including the removal of individual elements. While manipulating arrays is a worthwhile exercise in determining "how things work", it's always better to make use of efficient, tried-and-tested code, rather than constantly re-inventing wheels to provide such common functionality.


Why multidimensional array element access using indirection operator?

The number of dimensions is immaterial. All arrays are implemented as a one dimensional array. A multidimensional array is simply an array where every element is itself an array. The only thing actually known about any array is that its name is a reference to the start address. Unlike an ordinary (non-array) variable, the elements in the array do not have names, we can only refer to them by their memory offsets from the start of the array. As such, in order to obtain the values stored at those offsets, we must dereference them. While the subscript operator gives us notational convenience, it's easy to forget that there's actually pointer arithmetic and dereferencing going on behind the scenes.


What is the difference between null array and an empty array?

There is no null array as such. However, a pointer to an array may be nullified (pointing to zero) before memory is allocated to it, or after that memory is released. But the same is true of any pointer. That is, the pointer is NULL, not what it previously pointed to. An empty array usually refers to a dynamic array for which no memory has yet been allocated (in other words, a null pointer). However, the term can also apply to static arrays or dynamic arrays that have been allocated memory, where every element is initialised with a value that has no significance. For instance, an array of natural numbers (positive integers greater than 0) might be initialised with the value 0 (a non-natural number) in every element to indicate that the array is empty. By contrast, individual elements could be set to zero to indicate which elements are available (an empty element as opposed to an empty array).


Columns and rows in c programming?

Do you perhaps mean -- a two-dimensional array? A two dimensional array is nothing more than a one-dimensional array where every element is a one-dimensional array. int matrix[4][5]; C is a row-major language thus the first dimension refers to the number of rows. Here we have declared an array of 4 rows, where each row is an array of 5 elements of type int.


How arrays are used in synon?

Synon array processing is totally intuitive for any synon user. You do not need to open a manual. If you can use a file, you can use an array. The main difference is that there are only 4 function types: CRTOBJ, CHGOBJ, DLTOBJ, RTVOBJ, and only 1 "ACP" (set of Key fields)The array processing really behaves more like an indexed file than an array. The file portion (Records) is a multiple occurrence data structure and the index portion is an array. The data structure contains the fields as defined in the *ARRAY file. The array(Index) is the fields defined as the Key concatenated with the occurrence# of the data structure. When you create an element in the array (a record in the file), the program takes the next available occurrence of the data structure and loads the fields. The program also creates the Index element by concatenating the key fields with the occurrence# of the newly added data structure. The program then does a Sort Array to sequence the Index so that the records may be accessed in key sequence. Any time a record is deleted, the Index element is blanked out and the Index is Sorted (Sort Array). This is a very clean solution and the performance is surprisingly good considering that the array is sorted after each add or delete.When accessing the data, a LOKUP is performed on the array using the key value with blanks instead of the occurrence# and looking for GT in the array. This allows processing of partial keys as positioners or restrictors. The program then reads the data structure for the occurrence# taken from the array element to get the data(fields). For the next occurrence, it is not necessary to do another LOKUP (which is slow) because the array is sorted. The program only has to read the next element in the array and continue the processing as long as the partial key matches the key in the array. From the point of view of the Synon user, this processing is identical to a file RTVOBJ.Synon loads the array from the bottom (an RPG technique to improve performance in large arrays) because the LOKUP command may be given a starting position, but cannot be stopped until every element is checked when no match is found. By loading the array backwards from the bottom and counting the elements, the search may be started at the last element loaded and only elements which contain data are read.IF No Match is FoundAn array is defined for 1000 elements.Only 10 elements are loaded.These are loaded to occurrences 991 thru 1000.When the LOKUP is performed, it starts at 991 and only 10 compares are made.** If the array is loaded from the top, 1000 compares are performed.

Related questions

How do you write a program in c plus plus to remove fifth element in the array?

To remove any element from an array, you must overwrite every element from that point on with the element that follows it. That is, every element after the removed element must be copied to the preceding element. So to remove the 5th element, you copy the 6th element to the 5th element, the 7th to the 6th, and so on. Once all elements have been copied, you can reduce the array size by one element. Alternatively, you can leave the array size as is and simply maintain a count of the used elements (unused elements will always be at the end of the array).Note that in C++, a vector encapsulates an array of any type and provides member methods for manipulating that array, including the removal of individual elements. While manipulating arrays is a worthwhile exercise in determining "how things work", it's always better to make use of efficient, tried-and-tested code, rather than constantly re-inventing wheels to provide such common functionality.


Why multidimensional array element access using indirection operator?

The number of dimensions is immaterial. All arrays are implemented as a one dimensional array. A multidimensional array is simply an array where every element is itself an array. The only thing actually known about any array is that its name is a reference to the start address. Unlike an ordinary (non-array) variable, the elements in the array do not have names, we can only refer to them by their memory offsets from the start of the array. As such, in order to obtain the values stored at those offsets, we must dereference them. While the subscript operator gives us notational convenience, it's easy to forget that there's actually pointer arithmetic and dereferencing going on behind the scenes.


What is the difference between null array and an empty array?

There is no null array as such. However, a pointer to an array may be nullified (pointing to zero) before memory is allocated to it, or after that memory is released. But the same is true of any pointer. That is, the pointer is NULL, not what it previously pointed to. An empty array usually refers to a dynamic array for which no memory has yet been allocated (in other words, a null pointer). However, the term can also apply to static arrays or dynamic arrays that have been allocated memory, where every element is initialised with a value that has no significance. For instance, an array of natural numbers (positive integers greater than 0) might be initialised with the value 0 (a non-natural number) in every element to indicate that the array is empty. By contrast, individual elements could be set to zero to indicate which elements are available (an empty element as opposed to an empty array).


How do you write the program to search for a word in an array of 5 words?

Write a loop that compares the word with every word in the array. For example, in Java, I think it would be something like this: // Declare some variables here ... for (int i = 0; i


How 3d arrays are represented in memory?

All multi-dimensional arrays are represented as arrays of arrays. That is, each element of the array is itself an array. Thus a two-dimensional array can be thought of as being a one-dimensional array where every element is a one-dimensional array. A three-dimensional array is therefore a one-dimensional array of two-dimensional arrays. And so on. The actual memory layout of a multi-dimensional array is no different to that of a one-dimensional array of the same size: int a[12]; int b[3][4]; Assuming a 4-byte int, the amount of memory allocated to a is 12 x 4 = 48 bytes. The array b is essentially an array where each element holds an array of 4 integers, thus each element is 16 bytes in length. Given there are 3 such elements in total, the total size of b is 3 x 16 = 48 bytes, the same as was allocated to a. Although the allocations are exactly the same in terms of size, the layouts differ. The array a is an array of twelve 4-byte elements (of type int) whereas array b is an array of three 16-byte elements, each of which is itself an array of four 4-byte elements (of type int). This changes the way we refer to the individual elements of the array. Every element in an array is referred to by its offset address from the start of the array. This is determined by multiplying its zero-based index by the element size. In the case of a, every element is 4-bytes in length, thus element a[2] refers to the element that is offset 2 x 4 = 8 bytes from the start of the array. But in the case of b, however, b[2] would refer to the 16-byte element that is offset 2 x 16 = 32 bytes from the start of the array. Given that we're actually referring to an element which is itself a 4-element array, we must use a second subscript to refer to the elements of that array. Thus b[2][3] would refer to the integer that is offset 3 x 4 bytes from the start of the array referred to by b[2]. Extending this idea into three-dimensions is simply a matter of taking a third subscript into account.


Columns and rows in c programming?

Do you perhaps mean -- a two-dimensional array? A two dimensional array is nothing more than a one-dimensional array where every element is a one-dimensional array. int matrix[4][5]; C is a row-major language thus the first dimension refers to the number of rows. Here we have declared an array of 4 rows, where each row is an array of 5 elements of type int.


How arrays are used in synon?

Synon array processing is totally intuitive for any synon user. You do not need to open a manual. If you can use a file, you can use an array. The main difference is that there are only 4 function types: CRTOBJ, CHGOBJ, DLTOBJ, RTVOBJ, and only 1 "ACP" (set of Key fields)The array processing really behaves more like an indexed file than an array. The file portion (Records) is a multiple occurrence data structure and the index portion is an array. The data structure contains the fields as defined in the *ARRAY file. The array(Index) is the fields defined as the Key concatenated with the occurrence# of the data structure. When you create an element in the array (a record in the file), the program takes the next available occurrence of the data structure and loads the fields. The program also creates the Index element by concatenating the key fields with the occurrence# of the newly added data structure. The program then does a Sort Array to sequence the Index so that the records may be accessed in key sequence. Any time a record is deleted, the Index element is blanked out and the Index is Sorted (Sort Array). This is a very clean solution and the performance is surprisingly good considering that the array is sorted after each add or delete.When accessing the data, a LOKUP is performed on the array using the key value with blanks instead of the occurrence# and looking for GT in the array. This allows processing of partial keys as positioners or restrictors. The program then reads the data structure for the occurrence# taken from the array element to get the data(fields). For the next occurrence, it is not necessary to do another LOKUP (which is slow) because the array is sorted. The program only has to read the next element in the array and continue the processing as long as the partial key matches the key in the array. From the point of view of the Synon user, this processing is identical to a file RTVOBJ.Synon loads the array from the bottom (an RPG technique to improve performance in large arrays) because the LOKUP command may be given a starting position, but cannot be stopped until every element is checked when no match is found. By loading the array backwards from the bottom and counting the elements, the search may be started at the last element loaded and only elements which contain data are read.IF No Match is FoundAn array is defined for 1000 elements.Only 10 elements are loaded.These are loaded to occurrences 991 thru 1000.When the LOKUP is performed, it starts at 991 and only 10 compares are made.** If the array is loaded from the top, 1000 compares are performed.


What are the benefits of multidimensional arrays?

Multi-dimensional arrays are accessed using more than one index: one for each dimension. Multidimensional indexing can be reduced internally to linear indexing; for example, a two-dimensional array with 6 rows and 5 columns is typically represented by a one-dimensional array of 30 elements.


What is traversing arrays?

Traversing an array simply means visiting every element of the array exactly once. Pointers facilitates this job. A pointer is first created to contain the base address of the array thereby pointing the first element. As the pointer is incremented, it points to the very next element and so on till the last element is visited. Thus the elements are visited sequentially in the same order as they are stored in the array.


What is time requirement for insert operation one dimensional array state average and worst case time management?

The time-complexity of an insert operation upon an array is O(n-k), where n is the number of elements in the array (such that n&gt;=0), and k is the position where the insertion will occur (such that k&lt;=n). The time-complexity arises due to the need to move the last n-k elements in the array to make room for the new element. Thus inserting at or near the start of the array will take more time than inserting at or near the end of the array. Inserting at the end of an array is a constant-time operation, O(1). However, all of this presumes that the array has one or more unused elements at the end of the array to allow for expansion. If the array is full (no unused elements), the entire array must be re-allocated. For optimal performance, implementations will typically grow an array by increasing the allocation by 50% - 100%, thus time-complexity is said to be amortized rather than finite due to the occasional need to re-allocate. Note that the number of dimensions is immaterial since all arrays are intrinsically one-dimensional. That is; a two-dimensional array is nothing more than a one-dimensional array where every element is itself a one-dimensional array. The length of each element will affect the actual time taken to complete an insertion, however it does not affect the time-complexity.


How many types of arrays in c?

An array is simply a contiguous block of memory containing two or more elements. There are two types of array: a static array which is allocated on the stack at compile time; and a dynamic array which is allocated on the heap at runtime. Both can be one-dimensional or multi-dimensional. A one-dimensional array can be likened to a row (or column) of chessboard squares, with as many squares as required to store all the elements. A multi-dimensional array is any array with two or more dimensions. A two-dimensional array can be likened to the whole chessboard, where any square can be identified by its row and column index. However the dimensions needn't be equal. A two-dimensional array can also be imagined as a one-dimensional array where every element is simply another one-dimensional array. Three-dimensional arrays can be likened to a cube, or as a one-dimensional array of two-dimensional arrays. A four-dimensional array can be linked to a one-dimensional array of three-dimensional arrays, and so on. Although every one-dimensional array must be allocated in contiguous memory, multi-dimensional arrays can be dynamically allocated so that each dimension is itself a separately allocated one-dimensional array of pointers to the next dimension, making it possible to allocate extremely large arrays over a series of smaller allocations rather than as a single contiguous block.


How do you create a two dimentional array?

A one-dimensional array is an array where each element in the array points to a specific value of the type specified by the array (all values must be of the same type). For example, we can store integer values in an integer array, character values in a character array and strings in a string array. Multi-dimensional arrays are implemented as one-dimensional arrays where every element is itself a one-dimensional array, for as many dimensions as required. The overall size of any array (in elements) is the product of all its dimensions, thus a two-dimensional array of 4x5 elements has 20 elements in total, divided into 4 arrays of 5 elements each. However, because all the elements are allocate contiguously, any multi-dimensional array can be treated as if it were one-dimensional. Note that every element of an array must be exactly the same length, even when that element is another array. The most common type of array we use is a pointer array (an array of pointer elements). Given that a non-null pointer does not store any size information (the number of elements being referred to), we typically use null-terminated pointer arrays, where a null pointer denotes the end of the array being referred to. This makes it possible to implement "jagged" or "irregular" multi-dimensional arrays, where each dimension can be a different length. An array of variable-length strings is an example of a jagged array, such that each element points to a null-terminated character array.