Question

In: Computer Science

What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...

  1. What is the output of the following program?

#include <iostream>

using namespace std;

void showDouble(int); //Function prototype

int main()

{

int num;

for (num = 0; num < 10; num++)

showDouble(num);

return 0;

}

// Definition of function showDouble

void showDouble(int value)

{

cout << value << ‘\t’ << (value * 2) << endl;

}

Please do the following Program and hand in the code and sample runs.

  1. Write a program using the following function prototypes:
    • double getLength();
    • double getWidth();
    • double getArea(double len, double wid);
    • void displayData(double len, double wid, double area);

Write function main which calls these four functions. In your function definitions make sure that the length and width are positive numbers.

Hand in 2 runs showing valid and invalid data.

Solutions

Expert Solution

/*If you any query do comment in the comment section else like the solution*/

import java.util.Scanner;

public class Main {     
        public static Scanner keyboard = new Scanner(System.in);
        public static void main(String[]args) {
                double length;
                double width;           
                double area;    
                length = getLength();           
                width = getWidth();
                area = calculateArea(length, width);
                printResults(length, width, area);
                System.out.println();   
        }
        private static void printResults(double length, double width, double area) {
                System.out.printf("Dimensions: %.1f X %.1f\n", length, width);          
                System.out.printf("Area: %.2f\n", area);
                
        }
        private static double calculatePerimeter(double length, double width) {
                return (2*(length + width));            
        }
        private static double calculateArea(double length, double width) {
                return (length*width);
        }
        private static double getWidth() {
                double width;
                do {
                        System.out.print("Enter the width: ");
                        width = keyboard.nextDouble();
                        if(width <= -1) {
                                System.out.println("Width must be positive! Try Again");
                        }
                }while(width <= -1);
                return width;   
        }
        private static double getLength() {
                double length;
                do {
                        System.out.print("Enter the width: ");
                        length = keyboard.nextDouble();
                        if(length <= -1) {
                                System.out.println("Width must be positive! Try Again");
                        }
                }while(length <= -1);
                return length;          
        }       
}


Related Solutions

What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int...
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int main() { int ind_square(int &); int x, *p; x=15; p = &x; ind_square(*p); } int ind_square(int &p) { *p = *p **p; } Question 1 options: C. No return for non-void function A. Indirection for the variables in ind_square requires pointer operand A and B B. Invalided use of address (&) symbol as a parameter for ind_squared A and C Question 2 Which statement...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ;...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ; int p = abs(-90); cout << ::p + p - var << endl; system("pause"); } double var = 5.5;
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT