ELET 2300 Lesson 06 Sample Programs Fall 2015(1) (4).docx - \/SP6_0.cpp\/This program uses a simple function to display a logo#include<iostream> using

ELET 2300 Lesson 06 Sample Programs Fall 2015(1) (4).docx -...

This preview shows page 1 - 6 out of 26 pages.

//SP6_0.cpp //This program uses a simple function to display a logo #include <iostream> using namespace std; void displayLogo( void ); //function prototype int main() { cout << "Hello from:" << endl; displayLogo(); //function invocation (or call) return 0; } //Function definition //Function takes no arguments and returns nothing void displayLogo( void ) { cout << "Learning About Functions" << endl; } /* Hello from: Learning About Functions */ 1
Image of page 1
// File Name ................. SP6_1.CPP // Course .................... ELET 2300 Introduction to C++ Programming // Programmer ................ Dr. Farrokh Attarzadeh // Compiler .................. Microsoft Visual Studio 2010 .NET // Operating System .......... Windows Vista // Environment ............... Console Applications // Copyright ................. © 2013 Dr. Farrokh Attarzadeh // Date ...................... 01/01/2013 // // // Program Description // ___________________ // // This program demonstrates two simple function declarations with no // parameters being passed to the functions and no value returned by // the functions. // Include files used #include <iostream> #include <iomanip> #include <cstdlib> using namespace std; // User-defined function prototypes extern void header( void ); // function header() is declared extern void sum( void ); // function sum() is declared int main( void ) { header(); // function header() is invoked sum(); // function sum() is invoked return EXIT_SUCCESS; } // // The following function is user defined and is invoked by the function // main(). The function displays the sample program number. // extern void header( void ) { cout << "\n\n" << "Sample Run for SP6_1" << endl; cout << "____________________" << endl; } // // The following function prompts the user to input two positive integers. // The program will then compute the sum and will print the numbers and // their sum. The program will terminate when the user inputs the first // number in negative form. Otherwise, the addition process will continue. // 2
Image of page 2
extern void sum( void ) // definition of function sum() { int i, j; // i and j are local (auto) variables do { cout << "Please input two positive integer numbers.\n\n" << endl; cout << "To exit, enter the first integer as negative. ==> " ; cin >> i >> j; if ( i < 0 ) { cout << "\n\nProgram terminated per user request..." << endl; return; // return to the calling program with no value! } cout << "\t\ti\t\tj\t\tsum\n\n" ; cout << "\t\t--\t\t--\t\t---\n\n" ; cout << "\t\t" << setw(2) << i << "\t\t" << setw(2) << j << "\t\t" << setw(2) << (i + j) << endl; } while ( i >= 0 ); } Solution:- Sample Run for SP6_1 ____________________ Please input two positive integer numbers. To exit, enter the first integer as negative. ==> 3 4 i j sum -- -- --- 3 4 7 Please input two positive integer numbers. To exit, enter the first integer as negative. ==> 3
Image of page 3
//SP6_1a.cpp //This program shows the behavior of variables in two functions #include <iostream> using namespace std; void doArithmetic(); //function prototype int main() { int num = 10; cout << "At the start of main(), num is " << num << endl; doArithmetic(); //function invocation cout << "At the end of main(), num is " << num << endl; return 0; } //Function definition void doArithmetic() { int num = 40; //this num is different than that shown in main() cout << "At the start of doArithmetic(), num is " << num << endl; num += 15; cout << "At the end of doArithmetic(), num is " << num << endl; } /* At the start of main(), num is 10 At the start of doArithmetic(), num is 40 At the end of doArithmetic(), num is 55 At the end of main(), num is 10 */ 4
Image of page 4
//SP6_1b.cpp //This program prompts the user for your initial and returns it to //main function where it is displayed.
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 26 pages?

  • Fall '15
  • Marinolent

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture