Question

In: Computer Science

Please, write a loop to print odd numbers an array a with pointers backwards. You can...

Please, write a loop to print odd numbers an array a with pointers backwards. You can use a variable “size” for array size.

Solutions

Expert Solution

NOTE: If any changes are to be done in the code, or if there is any doubt feel free to put it in the comments , i will be there to solve the doubts,

Here is the complete c/c++ code:

//Inlcuding the required header files
#include <stdio.h>
#include <stdlib.h>
int main()
{
    //Declare an array of size 10
    int arr[10] = {3, 4, 2, 5, 1, 6, 7, 8, 9, 78};
    //Define a size variable and it will store the number of elements in arr by doing 100/10 = 10
    int size = sizeof(arr) / sizeof(arr[0]);
    //Make a pointer point to the last element
    int *ptr = arr + size;
    //While the pointer is not at the start print the corresponding value if it is odd
    while (ptr != arr)
    {
        if ((*ptr) % 2 == 1)
            printf("%d ", *ptr);
        //Decrement the pointer
        ptr--;
    }
    //Now we are at the first position, print it if it is odd
    if ((*ptr) % 2 == 1)
        printf("%d", *ptr);
    return 0;
}

Here is the image of the code to help understand the indentation

Here is the output of the above code, you can try experimenting with the array to see what results you get


Related Solutions

Question 1 Please, write a loop to print even members of an array a with pointers....
Question 1 Please, write a loop to print even members of an array a with pointers. You can use a variable “size” for array size Example Input: 1,2,5,6,8,9 Output : 2,6,8 Question 6 Please, write a loop to print odd numbers an array a with pointers backwards. You can use a variable “size” for array size Example Input: 1,2,5,6,8,9 Output : 9,5, Question2 1 LINKED LIST QUESTIONS For the following two questions you need to provide a code in C...
Using an array and a function, print the values of an array backwards. Please follow these...
Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.(java language)
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax. in java
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT