4806761279990q3zqy7 Jump to level 1 Write multiple if statements: If car Year is before 1967, print "Probably has few safety features." (without quotes). If after 1970, print "Probably has seat belts.. If after 1992, print "Probably has electronic stability control.. If after 2001, print 'Probably has airbags. End each phrase with period and newline. Ex: carYear = 1995 prints: Probably has seat belts. Probably has electronic stability control. 1 #include 2 3 int main(void) { 4 int carYear; 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 } 25 26 27 scanf("%d", &carYear); if (carYear 1967) { printf("Probably has few safety features. \n"); } if (carYear 1970) { printf("Probably has seat belts.\n"); } if (carYear 1992) { printf("Probably has electronic stability control.\n"); } if (carYear - 2001) { printf("Probably has airbags.\n"); } return 0; D-D-

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
Related questions
Question
100%

Need some help with this code, C Programming.

## If-else Statements in C Programming

### Challenge Activity: Detecting Multiple Features with Branches

**Task:** Write multiple `if` statements:
1. If `carYear` is before 1967, print "Probably has few safety features." (without quotes).
2. If after 1970, print "Probably has seat belts.".
3. If after 1992, print "Probably has electronic stability control.".
4. If after 2001, print "Probably has airbags.".

End each phrase with a period and a newline. Example: For `carYear = 1995`, the output should be:

```
Probably has seat belts.
Probably has electronic stability control.
```

### Example Code

Below is a sample C program implementing the above logic:

```c
#include <stdio.h>

int main(void) {
    int carYear;

    scanf("%d", &carYear);

    if (carYear <= 1967) {
        printf("Probably has few safety features.\n");
    }
    if (carYear >= 1970) {
        printf("Probably has seat belts.\n");
    }
    if (carYear >= 1992) {
        printf("Probably has electronic stability control.\n");
    }
    if (carYear >= 2001) {
        printf("Probably has airbags.\n");
    }

    return 0;
}
```

**Explanation of the Code:**
- The program includes the standard input-output library `stdio.h`.
- The main function defines an integer variable `carYear`.
- The `scanf` function is used to take an integer input from the user which is stored in `carYear`.
- Multiple `if` statements are used to check the conditions specified:
  - If the car year is before or equal to 1967, it prints "Probably has few safety features.".
  - If the car year is 1970 or later, it prints "Probably has seat belts.".
  - If the car year is 1992 or later, it prints "Probably has electronic stability control.".
  - If the car year is 2001 or later, it prints "Probably has airbags.".

**Notes:**
- The program uses `<=` and `>=` operators to ensure the correct comparison of the car year.
- Each `printf` function outputs the respective safety feature status followed by a newline character for proper formatting.
Transcribed Image Text:## If-else Statements in C Programming ### Challenge Activity: Detecting Multiple Features with Branches **Task:** Write multiple `if` statements: 1. If `carYear` is before 1967, print "Probably has few safety features." (without quotes). 2. If after 1970, print "Probably has seat belts.". 3. If after 1992, print "Probably has electronic stability control.". 4. If after 2001, print "Probably has airbags.". End each phrase with a period and a newline. Example: For `carYear = 1995`, the output should be: ``` Probably has seat belts. Probably has electronic stability control. ``` ### Example Code Below is a sample C program implementing the above logic: ```c #include <stdio.h> int main(void) { int carYear; scanf("%d", &carYear); if (carYear <= 1967) { printf("Probably has few safety features.\n"); } if (carYear >= 1970) { printf("Probably has seat belts.\n"); } if (carYear >= 1992) { printf("Probably has electronic stability control.\n"); } if (carYear >= 2001) { printf("Probably has airbags.\n"); } return 0; } ``` **Explanation of the Code:** - The program includes the standard input-output library `stdio.h`. - The main function defines an integer variable `carYear`. - The `scanf` function is used to take an integer input from the user which is stored in `carYear`. - Multiple `if` statements are used to check the conditions specified: - If the car year is before or equal to 1967, it prints "Probably has few safety features.". - If the car year is 1970 or later, it prints "Probably has seat belts.". - If the car year is 1992 or later, it prints "Probably has electronic stability control.". - If the car year is 2001 or later, it prints "Probably has airbags.". **Notes:** - The program uses `<=` and `>=` operators to ensure the correct comparison of the car year. - Each `printf` function outputs the respective safety feature status followed by a newline character for proper formatting.
### Section 3.7 - CIS 161: Introduction to C Programming
#### 3.7: Detecting multiple features with branches

**Example Shown:**
```c
return 0;
}
```

**Possible Solution:**
- Use four if statements, one for each possible year.

**Error Message:**
```plaintext
Compilation error:
main.c:27:4: error: expected identifier or ‘(’ before ‘return’
    return 0;
    ^
main.c:28:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
```

**Note:** Although the reported line number is in the uneditable part of the code, the error actually exists in your code. Tools often don't recognize the problem until reaching a later line.

---

### Nested if-else Statements

A branch's statements can include any valid statements, including another if-else statement, which are known as **nested if-else statements**. Nested if statements are commonly used to make decisions that are based on multiple features. For example, to calculate a discount based on both the number of items purchased and the total cost of those items, one if statement checks the number of items purchased and a nested if statement can check the total cost.

**Figure 3.7.2: Nested if-else**

```c
if (numItems > 3) {
    if (totalCost > 100) {     
        saleDiscount = 20;     // numItems > 3 and totalCost > 100
    }
    else if (totalCost > 50) { 
        saleDiscount = 10;     // numItems > 3 and 50 < totalCost <= 100 
    }
}
else {
    saleDiscount = 0;          // numItems <= 3        
}
```

This code above demonstrates how nested if-else statements work. Here, the outer if statement checks if the number of items purchased (`numItems`) is greater than 3. If this condition is true, the inner if-else statements will then check the `totalCost`. Depending on whether `totalCost` is greater than 100 or between 50 and 100, the `saleDiscount` will be set to 20 or 10, respectively. If `numItems` is not greater than 3, the `saleDiscount` will be set to 0.
Transcribed Image Text:### Section 3.7 - CIS 161: Introduction to C Programming #### 3.7: Detecting multiple features with branches **Example Shown:** ```c return 0; } ``` **Possible Solution:** - Use four if statements, one for each possible year. **Error Message:** ```plaintext Compilation error: main.c:27:4: error: expected identifier or ‘(’ before ‘return’ return 0; ^ main.c:28:1: error: expected identifier or ‘(’ before ‘}’ token } ^ ``` **Note:** Although the reported line number is in the uneditable part of the code, the error actually exists in your code. Tools often don't recognize the problem until reaching a later line. --- ### Nested if-else Statements A branch's statements can include any valid statements, including another if-else statement, which are known as **nested if-else statements**. Nested if statements are commonly used to make decisions that are based on multiple features. For example, to calculate a discount based on both the number of items purchased and the total cost of those items, one if statement checks the number of items purchased and a nested if statement can check the total cost. **Figure 3.7.2: Nested if-else** ```c if (numItems > 3) { if (totalCost > 100) { saleDiscount = 20; // numItems > 3 and totalCost > 100 } else if (totalCost > 50) { saleDiscount = 10; // numItems > 3 and 50 < totalCost <= 100 } } else { saleDiscount = 0; // numItems <= 3 } ``` This code above demonstrates how nested if-else statements work. Here, the outer if statement checks if the number of items purchased (`numItems`) is greater than 3. If this condition is true, the inner if-else statements will then check the `totalCost`. Depending on whether `totalCost` is greater than 100 or between 50 and 100, the `saleDiscount` will be set to 20 or 10, respectively. If `numItems` is not greater than 3, the `saleDiscount` will be set to 0.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Concept of Parenthesis
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.
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