Unable to print numbers entered into an array in reverse order

print array in reverse order c++
print array in reverse order in c
write statements to declare the array and assign the values to the corresponding positions.
array in java
reverse a number in java
print array in reverse order java
how to print array in reverse order in python
reverse a number in java using recursion

I'm attempting to write a program with an array in which:

  • The user enters 10 integers, and the integers are then displayed in reverse order, or
  • The user enters up to 10 integers and exits the while-loop using a 0, and the integers entered are then displayed in reverse order.

The code below is my best effort (my 5th attempt at solving the problem), but it's collecting the integer zero and displaying it in the array index (I've attempted to attach the other five, but it keeps producing an error):

int main()
{
   #define ARRAY_LENGTH 10

   int numbers[ARRAY_LENGTH];
   int numbersEntered = 0;

   for (int i = 0; i < ARRAY_LENGTH; i++)
   {
       numbers[i] = 0;
   }

   int number = 0;
   printf("Enter an integer; 0 to quit: ", ARRAY_LENGTH - 1);
   scanf_s("%d", &number);

   while (number != 0 && numbersEntered < ARRAY_LENGTH - 1)
   {
       numbersEntered++; 
       numbers[numbersEntered] = number;
       printf("Enter another integer; 0 to quit: ", ARRAY_LENGTH - 1);
       scanf_s("%d", &number);
   }

   for (int i = numbersEntered; i >= 0; i--)
   {
       printf("index %d -> %d\n", i, numbers[i]);
   }
}

See the problem is first you're initializing the entire array with zero here

for (int i = 0; i < ARRAY_LENGTH; i++)
   {
   numbers[i] = 0;
   }  

Then you're doing this

while (number != 0 && numbersEntered < ARRAY_LENGTH - 1)
   {
   numbersEntered++; 
   numbers[numbersEntered] = number;
   printf("Enter another integer; 0 to quit: ", ARRAY_LENGTH - 1);
   scanf_s("%d", &number);
   } 

So what is happening is, since numbersEntered was already initialized with zero you're incrementing it by 1, so the first digit is entered in the array numbers is 0 as you initialized the entire array itself in the start.

So a quick fix would be just to move it a line down and change the while loop code to this

while (number != 0 && numbersEntered < ARRAY_LENGTH - 1)
   {
   numbers[numbersEntered] = number;
   numbersEntered++; 
   printf("Enter another integer; 0 to quit: ", ARRAY_LENGTH - 1);
   scanf_s("%d", &number);
   }

Also as pointed out by Kiran in the comments, the printing for loop needs to be changed to this

for (int i = numbersEntered-1; i > 0; i--)
   {
   printf("index %d -> %d\n", i, numbers[i]);
   }

Computer Systems and Programming In 'C', It has to have an order entry, inventory control and tracking and tape The program needs to be able to forecast the tape sales for each tape and decide if you Print array values in reverse order the rainfall reading for each endinfquarter. In this program, an array num of type int is used to store the given integer numbers and variable nelem to store their count. First, the value of nelem is read. Then a for loop is used to read the given numbers and store them in array num. Finally, another for loop is used to print the given numbers in reverse order.

  1. The line printf("Enter an integer; 0 to quit: ", ARRAY_LENGTH - 1); should be printf("Enter an integer; 0 to quit: ");
  2. You should use scanf not scanf_s

The following code could work:

#include <stdio.h>

#define ARRAY_LENGTH 10

int main()
{
    int numbers[ARRAY_LENGTH];
    int i;
    int k;

    for (k = 0; k != ARRAY_LENGTH; ++k)
    {
        printf("Enter an integer; 0 to quit: ");
        if (scanf("%d", numbers + k) != 1 || numbers[k] == 0)
            break;
    }

    for (i = k - 1; i >= 0; --i)
        printf("index %d -> %d\n", i, numbers[i]);
    return 0; 
}

Learning C#, look back at the values of the constants (rowzero through rowThree), you'll be able Array. The Array class has a number of useful methods. Table 15-1 shows a dimensions of the array The Array class's static methods, Reverse() and Sort​(), The complete listing is shown in Example 15-9, followed by the output and  Algorithm to reverse an array. First of all take number of elements as input from user. Let it be N. Then ask user to enter N numbers and store it in an array(lets call it inputArray). Declare another array of size equal to input array. Using a for loop, copy elements from inputArray to reverseArray in reverse order.

This here is a solution of mine which includes some good practices, that is a tweak of your code:

1) you would want your defines outside of a function (or main) so that you can use the same constant in the entire program;

2) any function that has a return type (e.g int main) must return something at the end, thus you must have a "return 0" at the end of your main function;

3) the do-while part will ensure that your piece of code will be executed once before the while condition is checked;

4) numbers[numbersEntered++] = number; will assign to "numbers[numbersEntered]" the value of "number" and then will increment "numbersEntered" by one; This is to reduce the amount of code that you have to write :);

Hope this helped, and if you have more questions, I hope I can answer them :).

#define ARRAY_LENGTH 10

int main()
{

    int numbers[ARRAY_LENGTH];
    int numbersEntered = 0;

    for (int i = 0; i < ARRAY_LENGTH; i++)
        numbers[i] = 0;

    int number = 0;

    do
    {

        printf("Enter an integer; 0 to quit: ");
        scanf_s("%d", &number);

        if (number == 0)
            break;

        numbers[numbersEntered++] = number;


    } while (number != 0 && numbersEntered < ARRAY_LENGTH);

    if (numbersEntered > 0)
    {

        for (int i = numbersEntered - 1; i >= 0; i--)
            printf("index %d -> %d\n", i, numbers[i]);

    }

    return 0;

}

Custom CGI Scripting with Perl, Accessing a Hash Array In order to access a value from the array, we use the keyword: $first_name $USER_BIO{'first_name'}; If I used a variable as the key  To print an array in reverse order, we shall know the length of the array in advance. Then we can start an iteration from length value of array to zero and in each iteration we can print value of array index. This array index should be derived directly from iteration itself. Algorithm

I think it's because you are not inserting any number to the first place in the array (place[0]), because you are prompting the value of numbersEntered too early. Try to write your loop this way:

while (number != 0 && numbersEntered < ARRAY_LENGTH - 1){
   numbers[numbersEntered] = number;
   printf("Enter another integer; 0 to quit: ", ARRAY_LENGTH - 1);
   scanf_s("%d", &number);
   numbersEntered++; 
}

How to print elements of an array in reverse order in Perl, Given an array of strings we can print the elements in reverse order in a number of ways: my @words = qw(Foo Bar Moo);  The array is passed to the function, and a new array is created, and contents of the passed array (in reverse order) are copied into it, and finally, contents of the new array are copied into the array passed to the function.

Chapter 7: Arrays, We need to be able to read each input value twice: once to compute the average (a cumulative sum) and again to A second loop should print the values of the array with spaces between the values. Here is pseudocode that follows the sequence of interactions, using an array to manage the values that the user enters. Write an Efficient C Program to Reverse Bits of a Number; Write a program to print all permutations of a given string; Program to reverse words in a given string in C++; Program to reverse a string (Iterative and Recursive) First string from the given array whose reverse is also present in the same array; Program to reverse an array using pointers

Java program to reverse a number using for, while and recursion, The program will prompt user to input the number and then it will reverse the Output: Input your number and press enter: 145689 Reverse of input number is:  Logic to print array in reverse order. This algorithm in real does not produces a reversed array. Instead it just prints array in reverse order. If you are looking to reverse the elements then skip to next logic. So here goes step by step descriptive logic to print array in reverse order. Input size and elements in array from user.

Java Program to print the elements of an array in reverse order , public static void main(String[] args) {; //Initialize array; int [] arr = new int  To reverse an array in Java Programming, you have to ask to the user to enter the array size and then the array elements. Now start swapping the array elements. Make a variable say temp of the same type. And place the first element in the temp, then the last element in the first, and temp in the last and so on.

Comments
  • You skip the first element of the array in your while loop because you increment numbersEntered before you write. You need to switch the order
  • Unrelated, both of those prompt printf's are passing an argument that isn't called for (there is no specifier in the format string, so passing ARRAY_LENGTH-1 afterward make no sense).
  • You can initialize all elements in the array to zero by int numbers[ARRAY_LENGTH] = {0}; note this only works when initializing to zero.
  • Thank your comments. They're all really helpful. @WhozCraig - my instructor recommended the code ARRAY_LENGTH-1, but I can see from removing it and trying it out that it's superfluous.
  • And what about the for loop one which is used for printing array elements, Is it fine?
  • Thank you so much for this answer and your explanation. I get it now.