In: Computer Science
Write a C program to Declare an integer array of size 10 with values initialized as follows.
int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Compute each item of a new array of same size derived from the above array by:
adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9]) add it with 2nd item (treat it as a circular array).
Print all items of the new array in a single line.
Explanation:
here is the code which has the intArray initialised and it then declares the resArray of same size.
Then it uses a for loop, to assign the values of resArray like, 1st element in intArray + 3rd element, 2nd with 4th and so on as mentioned in the statement.
Then with another loop, all the elements of resArray are printed on a single line.
Code:
#include <stdio.h>
int main()
{
int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int resArray[10];
for (int i = 0; i < 10; i++) {
resArray[i] = intArray[i] + intArray[(i+2)%10];
}
for (int i = 0; i < 10; i++) {
printf("%d ", resArray[i]);
}
output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!