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!

Help with my C code

  1. Mar 30, 2017 #1
    1. The problem statement, all variables and given/known data
    I am fairly new to C programming. I need to write a code that creates an array of max 300 numbers, ask the user to input the numbers and then print that array.

    2. Relevant equations
    3. The attempt at a solution

    Here is my code
    Code (C):

    #include <stdio.h>

    void main() {
        int numbers[300];
        int i, n;

        printf("Enter the size of your array: \n");
            scanf("%d", &n);

        printf("Enter the numbers: \n");
        for (i = 0; i < n; ++i);
            scanf("%d", &numbers[i]);

        printf("Now your array will be printed: \n");
        for (i = 0; i < n; ++i);
            printf("%d ", numbers[i]);
         
            return 0;
    }
     
    The problem is that it only prints the first number inputed. How do i fix this?
     
  2. jcsd
  3. Mar 30, 2017 #2

    Drakkith

    User Avatar

    Staff: Mentor

    Check your syntax for your for loops. As soon as I corrected those, the code appeared to work fine.
     
  4. Mar 30, 2017 #3

    Mark44

    Staff: Mentor

    Code (C):
    for (i = 0; i < n; ++i);
        scanf("%d", &numbers[i]);
    As @Drakkith said, the syntax of your for loops is wrong.
    Your indentation is misleading. All the for loop does is count from 0 through n - 1, and then it's done. Next, it attempts to take input from the user and store it at index n in your array.

    Yours is a common mistake among new C (and related languages) programmers.
     
  5. Mar 30, 2017 #4
    Code (C):

    #include <stdio.h>
    void main() {
        int numbers[300];
        int i, n;

        printf("Enter the size of your array: \n");
        scanf("%d", &n);

        printf("Enter the numbers: \n");
        for (i = 0; i < n; i++)
        {
          scanf("%d", &numbers[i]);
        }

        printf("Now your array will be printed: \n");
          for (i = 0; i < n; i++)
        {
          printf("%d ", numbers[i]);
        }
           
        return 0;
    }
     
    So when i put ; at the end i basically just said that my for loop only counts and does nothing else. Could you explain how is it now corrected? I get the right outputs now.
     
  6. Mar 30, 2017 #5

    Mark44

    Staff: Mentor

    Yes, exactly.
    The syntax of a for loop is
    Code (Text):
    for( <init expression>; <test expression>; <update expression>) <statement>;
    <statement> can be empty, which is what you had before.
    Basically, you had an empty for loop that just counted, and following it a block statement--the call to scanf() surrounded by braces. As far as the compiler was concerned, there was no connection between the for loop and the following block.
     
  7. Mar 30, 2017 #6

    Drakkith

    User Avatar

    Staff: Mentor

    In C, semicolons terminate statements, so putting a semicolon right after the first for loop line prevents it from executing anything underneath it. That's why you leave the semicolon out and add the curly braces. That way the compiler knows that the for loop is to check the conditional statements first, then execute anything inside the brackets before looping back through.
     
  8. Mar 30, 2017 #7

    Mark44

    Staff: Mentor

    The braces aren't required in all cases.
    Code (C):

    for (i = 0; i < 10; i++)
        printf ("Hello\n");
    However, if the body of the for loop consists of two or more statements, the braces are required.
    Code (C):

    for (i = 0; i < 10; i++)
        printf ("Hello\n");
        printf("World");
    This loop, which has misleading indentation, would print Hello 10 times, and would then print World once, as it is not part of the loop body. Humans might be fooled by the indentation, but the compiler isn't.

    So, even though the braces aren't required if the loop body consists of only one statement, it's a good idea to include them anyway, in the possibility that you come back and add another statement to what you think is the loop body, but actually isn't.
     
  9. Mar 30, 2017 #8
    And to print the Hello World 10 times with the same code i do:
    Code (C):

    for (i = 0; i < 10; i++)
        printf ("Hello\n")
        printf("World");
    which makes the one loop end after the world?
     
  10. Mar 30, 2017 #9

    Drakkith

    User Avatar

    Staff: Mentor

    Since your loop consists of more than 1 statement, you need curly braces.
     
  11. Mar 30, 2017 #10

    Mark44

    Staff: Mentor

    No. The second printf() statement is not part of the for loop.
    The code above is exactly the same as this:
    Code (C):

    for (i = 0; i < 10; i++)
    {
        printf ("Hello\n")
    }
    printf("World");
     
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: Help with my C code
Loading...