2

Im trying to make a program that will get sequence from the user that end with 0, and then i want to print the last 5 numbers (not including the 0).

I can assume that the user will input all the numbers in one line and will end it with 0.

I wrote that code but something is wrong with it, I think its something about the scanf line.

Input:

1 6 9 5 2 1 4 3 0

Output: no output

#include <stdio.h>
#define N 5

int main()
{
    int arr[N] = {0};
    int last_input, j;
    
    printf("please enter more than %d number and than enter 0: \n", N);
    
    last_input = 0;
    while (last_input<N) {
       scanf(" %d", &j);
       if (j == '0') {
          last_input = N;
          break;
       }
       else {
          arr[last_input] = j;
       }
       if (last_input==(N-1)) {
          last_input=-1;
       }
       ++last_input;
   }
    
    
    printf("The last %d numbers u entered are:\n", N); 
    
    for (j=(last_input+1); j<N; ++j) {
       printf(" %d", arr[j]);    
    }

    for (j=0; j<last_input; ++j) {
       printf(" %d", arr[j]);  
    }

    return 0;
}
10
  • 1
    For one thing, the test if (j == '0') is wrong. You're comparing to the character '0'. You want to compare to the integer 0: if (j == 0). Commented Jun 2, 2022 at 11:07
  • 1
    Can you give us an I/O example?
    – vmemmap
    Commented Jun 2, 2022 at 11:08
  • @Roi yes sure. input "1 6 9 5 2 1 4 3 0", output - there is no output. I will edit my question too. ty
    – Alfa Hores
    Commented Jun 2, 2022 at 11:10
  • 1
    @AlfaHores: Put backticks (`codehere`) around the code snippet (you can use backslashes to escape the backticks themselves, which is how I made that demonstration). Commented Jun 2, 2022 at 11:21
  • 1
    @AlfaHores Note: space character not needed in format for scanf(" %d", &j);.
    – chux
    Commented Jun 2, 2022 at 11:22

2 Answers 2

2

This comparison

if (j == '0') {

does not make a sense because the user will try to enter the integer value 0 instead of the value (for example ASCII 30h or EBCDIC F0h) for the character '0'.

You need to write at least

if (j == 0) {

Due to these sub-statements of the if statement

  last_input = N;
  break;

this for loop

for (j=(last_input+1); j<N; ++j) {
   printf(" %d", arr[j]);    
}

is never executed and does not make a sense.

This statement

last_input=-1;

results in breaking the order of the N last elements in its output. And moreover the result value of the variable last_input will be incorrect.

You need to move elements of the array one position left. For this purpose you can use a loop of standard C function memmove.

The program can look the following way.

#include <stdio.h>
#include <string.h>


int main( void ) 
{
    enum { N = 5 };
    int arr[N];

    printf( "Please enter at least not less than %d numbers (0 - stop): ", N );

    size_t count = 0;

    for (int num; scanf( "%d", &num ) == 1 && num != 0; )
    {
        if (count != N)
        {
            arr[count++] = num;
        }
        else
        {
            memmove( arr, arr + 1, ( N - 1 ) * sizeof( int ) );
            arr[N - 1] = num;
        }
    }

    if (count != 0)
    {
        printf( "The last %zu numbers u entered are: ", count );
        for (size_t i = 0; i < count; i++)
        {
            printf( "%d ", arr[i] );
        }
        putchar( '\n' );
    }
    else
    {
        puts( "There are no entered numbers." );
    }
}

The program output might look like

Please enter at least not less than 5 numbers (0 - stop): 1 2 3 4 5 6 7 8 9 0
The last 5 numbers u entered are: 5 6 7 8 9
1

I made some changes based on ur comments and now its work fine!

#include <stdio.h>
#define N 5

int main()
{
    int arr[N] = {0};
    int last_input, j;
    
    printf("please enter more than %d number and than enter 0: \n", N);
    
    last_input = 0;
    while (last_input<N) {
       scanf("%d", &j);
       if (j == 0) {
          break;
       }
       else {
          arr[last_input] = j;
       }
       if (last_input==(N-1)) {
          last_input=-1;
       }
       ++last_input;
   }
    
    
    printf("The last %d numbers u entered are:\n", N); 
    
    for (j=(last_input); j<N; ++j) {
       printf("%d ", arr[j]);    
    }

    for (j=0; j<last_input; ++j) {
       printf("%d ", arr[j]);  
    }

    return 0;
}

thank u guys <3.

2
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – lemon
    Commented Jun 2, 2022 at 14:21
  • @lemon I saw the article but didnt understand what wrong. maybe I didnt write what did I changed? if this is the problem I will edit. i can delete the answer too if needed...
    – Alfa Hores
    Commented Jun 2, 2022 at 17:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.