0

I have a homework assignment that requires user to input a set of real numbers. I must store these into an array of size 20, and must print the array in floats.

My problem here is that my array is printing more than the five numbers that are required. the five numbers are 10, 37, 15, 21, 18.

I need help printing only the five numbers, in float with one decimal place.

I'm Using Centos6.7 in Oracle VM VirtualBox, with gedit text editor. Any help is appreciated.

#include <stdio.h>
#define SIZE 20


int main(void)
{
    int i, inputs[SIZE];

    printf("Enter real numbers, up to %d, q to quit\n", SIZE);
    for(i=0; i < SIZE; i++)
        scanf("%d", &inputs[i]);

    printf("You entered the following values:\n");
    for(i=0; i < SIZE; i++)
        printf("%4d", inputs[i]);
    printf("\n");

return 0;
}

This is the output of the program:

[ee2372@localhost cprog]$ gcc jperez_aver.c
[ee2372@localhost cprog]$ ./a.out 
Enter real numbers, up to 20, q to quit
10 37 15 21 18 q
You entered the following values:
  10  37  15  21  18   04195443   0-503606696327674196037   0-891225184  494195968   0   0   04195552   0
3
  • 1
    First problem I see here: You prompt the user to enter real numbers, but expect integers instead. Commented Oct 11, 2015 at 7:47
  • 1
    You should always compile with warnings and compile in the latest version of C, i.e, use gcc jperez_aver.c -Wall -Wextra -pedantic -std=c11. Commented Oct 11, 2015 at 7:47
  • 1
    Second problem: You print your whole array, no matter how many values were actually entered. Commented Oct 11, 2015 at 7:48

1 Answer 1

5

You must keep track of how many numbers the user has entered. For this, you need a new variable. Increment it if the user enters an integer. Something like this will suffice:

#include <stdio.h>

#define SIZE 20

int main(void)
{
    int i, count = 0, inputs[SIZE];      /* Note the new variable */

    printf("Enter real numbers, up to %d, q to quit\n", SIZE);
    for(i = 0; i < SIZE; i++)
    {
        if(scanf("%d", &inputs[i]) == 1) /* If `scanf` was successful in scanning an `int` */
            count++;                     /* Increment `count` */
        else                             /* If `scanf` failed */
            break;                       /* Get out of the loop */
    }

    printf("You entered the following values:\n");
    for(i = 0; i < count; i++)           /* Note the change here */
        printf("%4d", inputs[i]);

    printf("\n");

    return 0;
}

If you want the user to enter numbers having decimals, you should use:

#include <stdio.h>

#define SIZE 20

int main(void)
{
    int i, count = 0;
    float inputs[SIZE];                  /* For storing numbers having decimal part */

    printf("Enter real numbers, up to %d, q to quit\n", SIZE);
    for(i = 0; i < SIZE; i++)
    {
        if(scanf("%f", &inputs[i]) == 1) /* If `scanf` was successful in scanning an `float` */
            count++;                      /* Increment `count` */
        else                              /* If `scanf` failed */
            break;                        /* Get out of the loop */
    }

    printf("You entered the following values:\n");
    for(i = 0; i < count; i++)
        printf("%.1f \n", inputs[i]); /* Print the number with one digit after the decimal, followed by a newline */

    printf("\n");

    return 0;
}

Note that both the above approaches leaves q (or whatever non-integer the user typed) in the stdin. You can clean this from the stdin by using

int c;
while((c = getchar()) != '\n' && c != EOF);

after the first for loop.

Sign up to request clarification or add additional context in comments.

6 Comments

Just a little nitpick left: For whatever reason, the assignment requires float ... I see this braindead requirement around a lot in assignments, like professors never heard about float being promoted to double anyways when calling a variadic function ....
Hmm, double of course is the way to go ... but if the professor wants "non-sensical" code, oh well ...
Thank you for the input and the editing @Cool. So my mistake was printing the whole array, instead of creating a separate variable that can be increased along with the variables input by the user. I see that and for clarification, the assignment requires the output to be "0.0" format. First thing that came to mind was float. Thank you so much again for the assistance.
Thanks @Rudy for the help.
For arrays, no, probably not (a double is generally twice the size of a float). But you get better precision, and because floats are often converted to double before certain function calls, and the results back to float, it makes more sense (it is probably faster) to do everything in double to avoid all those conversions. So both types have their pros and cons. Which you choose depends on your application. Some applications even require other types, like decimals, or BigIntegers, or ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.