Question

C Programming Complete the following function by writing C statements that correspond to the description in...

C Programming

  1. Complete the following function by writing C statements that correspond to the description in

each comment in the code.

            #include <stdio.h>

intmain()

{

// Declare a floating point variable to store a rectangle’s width

// Declare a floating point variable to store a rectangle’s length

// Declare a third floating point variable that will store the area of the rectangle.

// Prompt the user to enter a width for the rectangle.

// Get the user’s input, and store it in your previously declared variable for width

// Prompt the user to enter a length for the rectangle.

// Get the user’s input, and store it in your previously declared variable for length

// Using an if statement, print out “Error: invalid side length” if either length or width are

// less than or equal to 0.

// Else…

// Calculate the area of the rectangle as a product of length times width, and store the

// result in your variable for storing the area.

                        // Print "The area of a rectangle with sides <width> and <length> is <area>” to the screen.

                        // Replace <width>, <length>, and <area> with the value of the associate variables.

                        // All floating point values should be printed out with 1 decimal place of precision.

return0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int main() {
// Declare a floating point variable to store a rectangle’s width
    float width;
// Declare a floating point variable to store a rectangle’s length
    float length;
// Declare a third floating point variable that will store the area of the rectangle.
    float area;
// Prompt the user to enter a width for the rectangle.
    printf("Enter width of rectangle: ");
// Get the user’s input, and store it in your previously declared variable for width
    scanf("%f", &width);
// Prompt the user to enter a length for the rectangle.
    printf("Enter length of rectangle: ");
// Get the user’s input, and store it in your previously declared variable for length
    scanf("%f", &length);
// Using an if statement, print out “Error: invalid side length” if either length or width are
// less than or equal to 0.
    if (length <= 0 || width <= 0) {
        printf("Error: invalid side length\n");
    } else {
// Else…
// Calculate the area of the rectangle as a product of length times width, and store the
        area = length * width;
        // result in your variable for storing the area.
        // Print "The area of a rectangle with sides <width> and <length> is <area>” to the screen.
        printf("The area of a rectangle with sides %.1f and %.1f is %.1f\n", width, length, area);
        // Replace <width>, <length>, and <area> with the value of the associate variables.
        // All floating point values should be printed out with 1 decimal place of precision.
    }
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
C Programming Complete the following function by writing C statements that correspond to the description in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • Question 4.4: Write an overloaded function of function area with 3 (float) parameters. This function should...

    Question 4.4: Write an overloaded function of function area with 3 (float) parameters. This function should calculate and print out the product of the 3 parameters. Question 4.4: Write the main function to test question 4.1, 4.2, 4.3, 4.4. Your main function should ask if the user want to calculate the area of a rectangle or a circle or a triangle then print out the result accordingly. (Use switch structure). For example: Please enter (r) for rectangle, (t) for triangle,...

  • Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...

    Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower. Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that represent the highest possible number to 100 (what type should this be?) Initialize a Boolean variable that represents if we’ve achieved the correct guess to false Initialize a variable in which to...

  • Computer Architecture Project Description: farh-to-cel.asm : # Revised and added code taken from Hennesey and Patterson...

    Computer Architecture Project Description: farh-to-cel.asm : # Revised and added code taken from Hennesey and Patterson 5th edition # to work with this simulator. # # Prompts a user to enter a Fahrenheit temperature as a floating point. # Displays the converted temperature in Celcius. # 10/28/2015 .data const5: .float 5.0 # store a floating point constant 5.0 const9: .float 9.0 const32: .float 32.0 .align 2 # align the next string on a word boundary .space 100 prompt: .asciiz "Please...

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings...

    C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings to double values. Calculate the sum,difference,and product of these values and print them. int main(){    // character string value array for user    char stringValue[10];       double sum=0.0;// initialize sum to store the results from 2 double values       double difference=0.0; // initialize difference to store the results from 2 double values       double product = 0.0; // intitialzie product...

  • Change your C++ average temperatures program to: In a non range-based loop Prompt the user for...

    Change your C++ average temperatures program to: In a non range-based loop Prompt the user for 5 temperatures. Store them in an array called temperatures. Use a Range-Based loop to find the average. Print the average to the console using two decimals of precision. Do not use any magic numbers. #include<iostream> using namespace std; void averageTemperature() // function definition starts here {    float avg; // variable declaration    int num,sum=0,count=0; /* 'count' is the int accumulator for number of...

  • package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task...

    package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task #1 before adding Task#2 where indicated. */ public class NumericTypesOriginal { public static void main (String [] args) { //TASK #2 Create a Scanner object here //identifier declarations final int NUMBER = 2 ; // number of scores int score1 = 100; // first test score int score2 = 95; // second test score final int BOILING_IN_F = 212; // boiling temperature double fToC;...

  • What if you had to write a program that would keep track of a list of...

    What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter to use in calculating square footage of walls that need paint, or for an advertising agency to keep track of the space available on billboards. The first step would be to define a class where one object represents one rectangle's length and width. Once we have class Rectangle, then we can make as many objects of...

  • in python please (1) Prompt the user to input a wall's height and width. Calculate and...

    in python please (1) Prompt the user to input a wall's height and width. Calculate and output the wall's area (integer). (Submit for 2 points). Enter wall height (feet): 12 Enter wall width (feet): 15 Wall area: 180 square feet (2) Extend to also calculate and output the amount of paint in gallons needed to paint the wall (floating point). Assume a gallon of paint covers 350 square feet. Store this value in a variable. Output the amount of paint...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT