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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education