CHALLENGE 3.8.1: If-else statement: Fix errors. ACTIVITY Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum > 0) printf("Positive.\n"); else printf("Non-positive, converting to 1.\n"); userNum = 1; printf("Final: %d\n", userNum); Learn how our autograder works 1 #include 3 int main(void) { int userfum; 5 6 scanf("%d", &userNum); if(userNum>0) printf("postive.\n"); 10 11 printf("Non-positive, converting to 1.\n"); 12 13 14 7 8 15} 16 17 18) else( userNum-1; } printf("final:%d\n", userfum); return 0; Run Failed to compile main.c:17:4: error: expected identifier or before 'return' 17 1 return 0; I main.c:18:1: error: expected identifier or (before ''token 18 ) Mete Although

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter2: Using Data
Section: Chapter Questions
Problem 17RQ: When you perform arithmetic operations with operands of different types, such as adding an int and a...
icon
Concept explainers
Question
100%

Need help with this, It's C Programming.

### 3.8.1: If-else statement: Fix errors.

#### Instruction
Re-type the code and fix any errors. The code should convert non-positive numbers to 1.

```c
if (userNum > 0)
    printf("Positive.\n");
else
    printf("Non-positive, converting to 1.\n");
    userNum = 1;

printf("Final: %d\n", userNum);
```

### Example Code with Syntax Errors

```c
#include <stdio.h>

int main(void) {
    int userNum;

    scanf("%d", &userNum);

    if(userNum > 0)
        printf("Positive.\n");
    else
        printf("Non-positive, converting to 1.\n");
        userNum = 1;

    printf("Final:%d\n", userNum);

    return 0;
}
```

### Explanation
The above code has a logical and syntactic error. The `else` statement will always execute because the `userNum = 1;` line is not within the scope of the `else` block. To correct it, place braces `{}` around the `else` block.

### Correct Code

```c
#include <stdio.h>

int main(void) {
    int userNum;

    scanf("%d", &userNum);

    if(userNum > 0) {
        printf("Positive.\n");
    } else {
        printf("Non-positive, converting to 1.\n");
        userNum = 1;
    }

    printf("Final:%d\n", userNum);

    return 0;
}
```

### Error Messages
- **Failed to Compile:**
    ``` 
    main.c:17:4: error: expected identifier or '(' before 'return'
    17 | return 0;
    |     ^~~~~~
    main.c:18:1: error: expected identifier or '(' before '}' token
    18 | }
    | ^
    ```
  - **Explanation:**
    The errors reported here suggest a missing syntax that should be addressed by using braces properly around the if-else statements.
  
### Additional Note:
Although the reported line number is in the uneditable part of the code, the error actually exists in your code. Tools often highlight errors at the end of the code block when the true issue lies within misused statements or blocks.
Transcribed Image Text:### 3.8.1: If-else statement: Fix errors. #### Instruction Re-type the code and fix any errors. The code should convert non-positive numbers to 1. ```c if (userNum > 0) printf("Positive.\n"); else printf("Non-positive, converting to 1.\n"); userNum = 1; printf("Final: %d\n", userNum); ``` ### Example Code with Syntax Errors ```c #include <stdio.h> int main(void) { int userNum; scanf("%d", &userNum); if(userNum > 0) printf("Positive.\n"); else printf("Non-positive, converting to 1.\n"); userNum = 1; printf("Final:%d\n", userNum); return 0; } ``` ### Explanation The above code has a logical and syntactic error. The `else` statement will always execute because the `userNum = 1;` line is not within the scope of the `else` block. To correct it, place braces `{}` around the `else` block. ### Correct Code ```c #include <stdio.h> int main(void) { int userNum; scanf("%d", &userNum); if(userNum > 0) { printf("Positive.\n"); } else { printf("Non-positive, converting to 1.\n"); userNum = 1; } printf("Final:%d\n", userNum); return 0; } ``` ### Error Messages - **Failed to Compile:** ``` main.c:17:4: error: expected identifier or '(' before 'return' 17 | return 0; | ^~~~~~ main.c:18:1: error: expected identifier or '(' before '}' token 18 | } | ^ ``` - **Explanation:** The errors reported here suggest a missing syntax that should be addressed by using braces properly around the if-else statements. ### Additional Note: Although the reported line number is in the uneditable part of the code, the error actually exists in your code. Tools often highlight errors at the end of the code block when the true issue lies within misused statements or blocks.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning