1 / 18

LAB # 8

LAB # 8. What is output by the following program segment when function f1 is invoked?. 10 5. What is the output of cout << mystery( 6, 2, 5 ) << endl ; assuming the following definition of mystery?. 6. What is output by the following program?. Initially, x = 0 During call to f1, x = 3

mmaurice
Download Presentation

LAB # 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LAB # 8

  2. What is output by the following program segment when function f1 is invoked? 10 5

  3. What is the output ofcout << mystery( 6, 2, 5 ) << endl;assuming the following definition of mystery? 6

  4. What is output by the following program? Initially, x = 0 During call to f1, x = 3 At the end, x = 0

  5. Determine if there is an error in the code, and specify whether it is a logic or compilation error Line 1: Placing a semicolon at the end of the parameter list of a function definition results in a compilation error.

  6. Determine if there is an error in the code, and specify whether it is a logic or compilation error void f1(); Line 7: There is no function prototype for function f1 and it is defined below the main function. Therefore, when compiling this program, a compilation error will occur because the function is not declared before it is used.

  7. Determine if there is an error in the code, and specify whether it is a logic or compilation error Line 5: You cannot define a function inside of another function. This is a syntax error.

  8. Write a program that prints the square roots of the numbers 0 through 5.

  9. Write a program with a function named fact() to find the factorial from 0 to the number that reads from the user. Enter the number 4 fact(0) = 1 fact(1) = 1 fact(2) = 2 fact(3) = 6 fact(4) = 24

  10. Write a program consists of 3 functions as follows: • v_swapfunction • Has no return value • Takes two parameters of type int(pass by value) • Swaps the value of the two variables • displays the variables after swapping. • r_swap function • Same as v_swap but the parameters pass by reference • the main function: • Declares and initiates two int variables • Calls v_swap • Displays the two variables • Calls r_swap • Displays the two variables

  11. #include<iostream> • using namespace std; • void v_swap(int , int ); • void r_swap(int &, int &); • void main() • { • int x = 11, y = 22; • cout << "Before calling any function, x = "<< x <<" y = "<< y << endl; • v_swap( x, y); • cout << "After calling v_swap, x = "<< x <<" y = "<< y << endl; • r_swap( x, y); • cout << "After calling r_swap, x = "<< x <<" y = "<< y << endl; • } • void v_swap(int x, int y) • { • int temp = x; • x = y; • y = temp; • cout << "Inside the function v_swap, x = "<< x <<" y = "<< y << endl; • } • void r_swap(int &x, int &y) • { • int temp = x; • x = y; • y = temp; • cout << "Inside the function r_swap, x = "<< x <<" y = "<< y << endl; • }

  12. UsingHeader Files

  13. Header File fac.h

  14. Source File main.cpp

More Related