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

Click For Summary

Discussion Overview

The discussion revolves around a C programming homework problem where the participant seeks help in creating an array, accepting user input, and printing the contents of that array. The focus is on syntax issues related to for loops and the correct structure of the code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • The initial code provided by the participant contains syntax errors in the for loops, specifically the use of semicolons that terminate the loops prematurely.
  • Some participants point out that the incorrect placement of semicolons leads to the for loops not executing the intended statements, resulting in only the first number being printed.
  • A participant explains that the for loop should be structured to include the statements within curly braces to ensure they are executed as part of the loop.
  • There is a discussion about the optional use of braces in for loops, with some participants emphasizing that while braces are not always required for single statements, they are recommended to avoid confusion.
  • Further clarification is provided regarding how misleading indentation can lead to misunderstandings about which statements are included in the loop body.
  • A participant attempts to modify the loop to print "Hello" and "World" multiple times, leading to a discussion about the necessity of braces when multiple statements are involved.

Areas of Agreement / Disagreement

Participants generally agree on the syntax issues related to the for loops and the importance of using braces for clarity. However, there are varying opinions on the necessity of braces in all cases, and the discussion remains focused on clarifying these points without reaching a consensus on every aspect.

Contextual Notes

Some participants note that the original code's structure may lead to confusion for new programmers, highlighting common pitfalls in C programming syntax.

doktorwho
Messages
181
Reaction score
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
Check your syntax for your for loops. As soon as I corrected those, the code appeared to work fine.
 
  • Like
Likes   Reactions: doktorwho
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   Reactions: doktorwho
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.
 
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.
 
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.
 
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.
 
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?
 
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   Reactions: 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   Reactions: doktorwho

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K