papawashington
papawashington

Reputation: 23

Can't read in first iteration of a for loop in C

I'm trying to create a program that is able to calculate Lagrange polynomials, but I'm running into what is probably a trivial problem. I am given a number of x and y values that I am supposed to use to approximate a function for some other x. The variable nodes refers to the number of x and y value pairs I am given.

I'm unable to read in the first iteration of the x value, and it skips right to reading in the y value. i.e. it just prints x(0) and then y(0) without letting me input anything for x(0). This isn't an issue for any loops after the first one. Any help would be appreciated.

#include "stdio.h"
#include "math.h"
#define SIZE 40

int main(){

    // Defining variables and arrays
    int i, j, nodes;
    float nodex[SIZE], nodey[SIZE], appx;

    // Find how many nodes there are
    printf("How many nodes are being referenced?\n");
    scanf("%d", &nodes);

    // Find what number we are approximating for x
    printf("For what value of x are we approximating?\n");
    scanf("%d", &appx);

    for(i=0 ; i < nodes ; i++) 
    {
        printf("\nEnter x(%d)", i);
        scanf("%f", &nodex[i]);
        printf("\nEnter y(%d)", i);
        scanf("%f", &nodey[i]);
    }
}

Upvotes: 1

Views: 144

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753775

As noted in comments:

You're using %d with appx, instead of %f. The %d stops reading at the decimal point, and the &nodex[0] input resumes where the %d left off — at the decimal point. The value in appx is garbage too, of course.

You should test the return value from scanf(); it should be 1 for each call you show. You should print the values read so you know that what was read matches what you expected.

Some fixed code:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 40 

static void err_exit(const char *msg)
{
    fprintf(stderr, "%s\n", msg);
    exit(EXIT_FAILURE);
}

int main(void)
{
    // Defining variables and arrays
    int nodes;
    float nodex[SIZE], nodey[SIZE], appx;

    // Find how many nodes there are
    printf("How many nodes are being referenced?: ");
    fflush(stdout);
    if (scanf("%d", &nodes) != 1)
        err_exit("failed to read number of nodes");
    if (nodes < 3 || nodes > SIZE)
        err_exit("number of nodes not in range 0..40");

    // Find what number we are approximating for x
    printf("For what value of x are we approximating?: ");
    fflush(stdout);
    if (scanf("%f", &appx) != 1)
        err_exit("failed to read value");

    for (int i = 0; i < nodes; i++)
    {
        printf("Enter x(%d): ", i);
        fflush(stdout);
        if (scanf("%f", &nodex[i]) != 1)
            err_exit("failed to read x-value");
        printf("Enter y(%d): ", i);
        fflush(stdout);
        if (scanf("%f", &nodey[i]) != 1)
            err_exit("failed to read y-value");
    }

    printf("Approximating: %g\n", appx);
    printf("%d nodes:\n", nodes);
    for (int i = 0; i < nodes; i++)
        printf("%2d (%g,%g)\n", i, nodex[i], nodey[i]);

    return 0;
}

I assume you have support for C99. If not, you'll need to declare int i; outside the loops and use for (i = 0; i < nodes; i++) as the loops.

Compilation:

gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
    read-float-83.c -o read-float-83 

Sample run:

$ ./read-float-83
How many nodes are being referenced?: 5
For what value of x are we approximating?: 3.67
Enter x(0): 1.23
Enter y(0): 2.95
Enter x(1): 1.98
Enter y(1): 3.46
Enter x(2): 2.47
Enter y(2): 4.51
Enter x(3): 3.02
Enter y(3): 2.87
Enter x(4): 4.18
Enter y(4): -1.96
Approximating: 3.67
5 nodes:
 0 (1.23,2.95)
 1 (1.98,3.46)
 2 (2.47,4.51)
 3 (3.02,2.87)
 4 (4.18,-1.96)
$

Upvotes: 7

Related Questions