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

1. What is C programming?

C programming is a high-level programming language that is commonly used to develop operating systems, software applications, and embedded systems. It was developed in the early 1970s by Dennis Ritchie at Bell Labs.

2. What is an array in C programming?

An array in C programming is a collection of elements of the same data type that are stored in a contiguous memory location. It allows for efficient storage and manipulation of data, making it a useful data structure for many programming tasks.

3. How do I create an array in C programming?

To create an array in C programming, you need to declare the array's data type, size, and name. For example, to create an array of integers with 5 elements, you would use the syntax "int array[5];".

4. How do I print all inputs in an array in C programming?

To print all inputs in an array in C programming, you can use a for loop to iterate through each element in the array and print its value. Alternatively, you can use the built-in function "printf" to print the array's contents.

5. How can I get help with my C programming homework?

There are several ways to get help with your C programming homework. You can ask your professor or classmates for assistance, seek help from online forums or communities, or hire a tutor or coding expert for personalized assistance.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
756
  • Engineering and Comp Sci Homework Help
Replies
4
Views
929
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
893
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
673
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Back
Top