1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Multiple inputs c-programming

  1. Feb 16, 2014 #1
    1. The problem statement, all variables and given/known data
    I'm trying to create a c program that will allow for multiple inputs for a simple linear equation, and will print those values. the program I've created only returns one value


    2. Relevant equations

    y = mx + b

    3. The attempt at a solution
    Code (Text):

    #include <stdio.h>
    #include <math.h>

    int main ()
    {

        int numbers[2];
        int x,y;
            printf("Enter x data set:");
       
            for(x=0;x<2;x++);

            x = numbers[2];
            scanf("%d",&numbers[2]);

             
        y =   5 * numbers[2] + 4;
        printf("the y values are; %d", y);
       
    }
     
    Last edited by a moderator: Feb 16, 2014
  2. jcsd
  3. Feb 16, 2014 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    ramses07, notice how I edited your opening post. Your code looks like code because I added [ code ] and [ /code ] tags (delete the spaces after/before the opening/closing square braces, or simpler, use the "code" icon).

    Your problem is your loop for(x=0;x<2;x++);. You need to
    • Get rid of that semicolon!
      That semicolon makes this loop a no-op, code that does absolutely nothing.
    • Replace that semicolon with an open brace.
      This means you need a close brace somewhere later, after the body of your loop.
    • Make the lines that follow form the body of your loop.
     
  4. Feb 16, 2014 #3

    phinds

    User Avatar
    Gold Member
    2016 Award

    You have declared y as an integer and you have a single print statement. Since you have specifically told it to print one value, I don't understand why you are surprised that it only prints one value.

    EDIT: I see DH beat me to it. Your problem is, as he pointed out, your loop control
     
    Last edited: Feb 16, 2014
  5. Feb 16, 2014 #4
    so this is what I've come up with, but I'm only getting zeros

    Code (Text):

    #include <stdio.h>
    #include <math.h>

    int main ()
    {

        int numbers[20], y[20];
        int x;
            printf("Enter x data set:");
       
            for(x=0;x<20;x++)
            {scanf("%d",&numbers[x]);
           
            x = numbers[x];}

             
        y[x] =   5 * numbers[x] + 4;

        printf("the y values are; \n%d\n%d\n", y[1], y[0]);
       
    }
     
     
  6. Feb 16, 2014 #5

    phinds

    User Avatar
    Gold Member
    2016 Award

    Do you understand the extent of your loop? Why did you not follow DH's recommendation on your loop extent?
     
  7. Feb 16, 2014 #6

    Mark44

    Staff: Mentor

    1. Why do you have this statement? x = numbers[x];
    x is your loop control variable, so you should not be resetting it in the body of your loop.
    2. The statement y[x] = 5 * numbers[x] + 4; is outside the loop, so it executes once. Furthermore, since the last iteration of the loop reset x to whatever value you entered, the statement where you set y[x] will attempt to set the element of the array whose index is x. This element might or not be within the array.
    3. Your printf statement prints only two values of the y array.

    Your indentation could be improved. Two important principles are that 1) statements that execute the same number of times should be at the same indentation level, and 2) the bodies of control statements (loops, if blocks, and so on) should be indented.
    ed the same. Using good indentation helps readers of your code understand its structure more easily. This is how I would indent your code.

    Code (Text):
    #include <stdio.h>
    #include <math.h>

    int main ()
    {

       int numbers[20], y[20];
       int x;

       printf("Enter x data set:");  // printf statement not indented

       for(x=0;x<20;x++)
       {                                           // Braces for loop body are clearly evident
          scanf("%d",&numbers[x]);  // Body of for loop indented
          x = numbers[x];
       }
             
       y[x] =   5 * numbers[x] + 4;
       printf("the y values are; \n%d\n%d\n", y[1], y[0]);
       
    }

     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Multiple inputs c-programming
  1. C++ Program (Replies: 17)

Loading...