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...

  • You are creating a new data type for tracking student’s grades. Create a C++ project in...

    You are creating a new data type for tracking student’s grades. Create a C++ project in your development tool. Add the header comment and the header files from your C++template.cpp file. Your program should do the following: • Declare an Enum named Grade. The Enum should have the grades A-F and have each grade correspond with A = 90, B = 80, C = 70, D = 60, F = 50. • Declare a variable of Grade type in your...

  • The statement in the following program is in the incorrect order. Rearrange the statements so that...

    The statement in the following program is in the incorrect order. Rearrange the statements so that they prompt the user to input the shape type (rectangle, circle, or cylinder) and the appropriate dimension of the shape. The program then outputs the following information about the shape: For a rectangle, it outputs the area and perimeter, for a circle, it outputs the area and circumference; and for a cylinder, it output the volume and surface area. After rearranging the statements, your...

  • 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...

  • Create a C++ Console project called Lab3a and add the following source document rect_struct.cpp to the...

    Create a C++ Console project called Lab3a and add the following source document rect_struct.cpp to the project. #include <iostream> #include <iomanip> using namespace std; // This program uses a structure to hold data about a rectangle // PLACE YOUR NAME HERE // Fill in code to declare a structure named rectangle which has // members length, width, area, and perimeter all of which are floats int main() {        // Fill in code to define a rectangle variable named box...

  • JAVA Code Requried Copy the file java included below. This program will compile, but, when you...

    JAVA Code Requried Copy the file java included below. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Below the main method, but in the Geometry class, create a static method called printMenu that has no parameter list and does not return a value. It will...

  • 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;...

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