Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get sequence of numbers and then print the last 5?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
}

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading