Help w/ C Programming Homework - Create Array & Print All Inputs

In summary: However, if the body of the for loop consists of two or more statements, the braces are required.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 this code into thinking that the loop executes 10 times, and then prints "World" once. The braces are required to ensure that the for loop runs 10 times.
  • #1
doktorwho
181
6

Homework Statement


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.

Homework Equations


3. The Attempt at a Solution [/B]
Here is my 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?
 
Physics news on Phys.org
  • #2
Check your syntax for your for loops. As soon as I corrected those, the code appeared to work fine.
 
  • Like
Likes doktorwho
  • #3
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.
 
  • Like
Likes doktorwho
  • #4
Drakkith said:
Check your syntax for your for loops. As soon as I corrected those, the code appeared to work fine.

Mark44 said:
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.
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.
 
  • #5
doktorwho said:
So when i put ; at the end i basically just said that my for loop only counts and does nothing else.
Yes, exactly.
doktorwho said:
Could you explain how is it now corrected? I get the right outputs now.
The syntax of a for loop is
Code:
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.
 
  • #6
doktorwho said:
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.

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.
 
  • #7
Drakkith said:
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.
The braces aren't required in all cases.
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.
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.
 
  • #8
Mark44 said:
The braces aren't required in all cases.
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.
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.

And to print the Hello World 10 times with the same code i do:
C:
for (i = 0; i < 10; i++)
    printf ("Hello\n")
    printf("World");
which makes the one loop end after the world?
 
  • #9
doktorwho said:
And to print the Hello World 10 times with the same code i do:
C:
for (i = 0; i < 10; i++)
    printf ("Hello\n")
    printf("World");
which makes the one loop end after the world?

Since your loop consists of more than 1 statement, you need curly braces.
 
  • Like
Likes doktorwho
  • #10
doktorwho said:
And to print the Hello World 10 times with the same code i do:
C:
for (i = 0; i < 10; i++)
    printf ("Hello\n")
    printf("World");
which makes the one loop end after the world?
No. The second printf() statement is not part of the for loop.
The code above is exactly the same as this:
C:
for (i = 0; i < 10; i++)
{
    printf ("Hello\n")
}
printf("World");
 
  • Like
Likes doktorwho

Similar threads

Replies
4
Views
1K
Replies
3
Views
1K
Replies
9
Views
4K
Replies
5
Views
2K
Replies
1
Views
10K
Replies
3
Views
1K
Replies
12
Views
2K
Replies
12
Views
1K
Replies
3
Views
954
Back
Top