Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8756063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:58:56+00:00 2026-06-13T13:58:56+00:00

I have an assignment to Write a C program that allows a user to

  • 0

I have an assignment to “Write a C program that allows a user to enter up to 20 integers (it will stop accepting numbers based on a sentinel value or based on the 20-integer limit being reached). The program should then display the numbers in the reverse order of entry.”

I decided to make my sentinel value “EOF” (or CTRL+D / CRTL+Z). My code has some really erratic behavior:

  1. You have to press the EOF keys twice (which also creates a blank entry that is counted int the array.
  2. The first digit from the first entry gets truncated.

Everything else seems to work OK but, this is clearly not the desired results. Below is my code. Can you explain what’s wrong?

main() {
int i,iMax;
double dblMean;
int x[MAX];

printf("Please Enter Up to 20 Integers\n");

for (i=0; i<MAX; i++)
{
    printf("%d.> ",i+1);
    if (getchar() == EOF)           /* Check for CTRL+D OR CTRL+Z */
    break;                          /* Exit loop if EOF entered :: Must be pressed twice and eats first character of first entry */
    scanf("%d",&x[i]);
    iMax=i;
}

printf("\nYou entered  %d numbers\n",iMax+1);               /* Should be i+1 but EOF had to be entered twice */
printf("\nIndex       Item        Reverse Item\n");
for (i=0; i<=iMax; i++)
    printf("%3d%4c%8d%9c%11d\n",i,'  ',x[i],'  ',x[iMax-i]);
return 0;

}

EDITED:
Here is my final code, Thank you all for your help:

#include <stdio.h>
#include <stdlib.h>
#define MAX 20
int main()
{
    int i,iMax;
    int x[MAX];

    printf("Please Enter Up to 20 Integers\n");

    for (i=0; i<MAX; i++)
    {
        printf("%d.> ",i+1);
        if (scanf("%d",&x[i]) != 1)                         /* Checks for CTRL+D OR CTRL+Z */
        break;                                              /* EOF returns -1 and loop will be exited */
        iMax=i;                                             /* iMax must be set for second loop to exit properly */
                                                            /* Can sizeof x be used instead for second loop? */
    }

    printf("\nYou entered  %d numbers\n",iMax+1);           /* Displays number of items entered... Will be <= MAX*/
    printf("\nIndex       Item        Reverse Item\n");
    for (i=0; i<=iMax; i++)                                 /* Why wont 'for (i=0; i<=sizeof x; i++)' work? */
        printf("%3d%4c%8d%9c%11d\n",i,'  ',x[i],'  ',x[iMax-i]);
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:58:57+00:00Added an answer on June 13, 2026 at 1:58 pm

    The getchar() call reads (and effectively discards) the first digit because it is not EOF.

    You don’t test that scanf() worked; you should.

    for (i = 0; i < MAX; i++)
    {
        if (scanf("%d", &x[i]) != 1)
            break;
    }
    

    At this point, there are i integers in the array; you don’t really need to set iMax in the loop. You could simply set it when the loop exits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class assignment where I have to write a program that will
for an assignment I have to write a program that will take in an
For class I have an assignment: Write a C++ program that will output the
I have to write a program that will essentially sort nodes in a linked
The assignment is to write a computer program that will add 1/3 to itself
This is a homework assignment, I have to write a program that reads a
I have the following assignment: Write a complete 8086 program to perform the calculator
I have been asked to write a program using python for an assignment. I
I have an assignment that wants me to write an ternary search algorithm and
I was given an assignment to write a program to print only even numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.