Você está na página 1de 2

Lab01

Objectives
To expose students to the following concepts of :
A basic and primitive review to functions in C
!unction Overloading
"eferences in !unctions
Arra#s
$tructure in Arra#s
QUESTION 1: %rite a complete structural C++ program that inputs & integers from the
'e#board( then calculates and prints the summation( average( product( smallest( and largest of these
numbers) *se separate functions to calculate the summation( average( product( smallest and the
largest) +ass the & integers to the respective functions and call the functions to calculate and return
the results to the main function) The screen dialogue should appear as follows:
,nter three numbers -a b c.: 1& /0 11
$um of numbers 2 31
Average of numbers 2 14
+roduct of numbers 21511
$mallest number 2 1&
Largest number 2 /0
QUESTION 2: 6o the following:
1) Compile and run the following program and stud# the output)
/) 7otice that the swapping does not wor' correctl#8 ,xplain wh#)
&) 9odif# the Swap function to ma'e the swapping wor') %hat techni:ue should #ou use8
1) 7otice that the calcArea function for rectangle does not wor') Add another function to
calculate the area of a rectangle using function overloading)
#include <iostream>
using namespace std;
double calcArea (double);
void Swap (double, double);

int main ()
{
double x = ;

cout << !"engt# = ! << x << endl;
cout << !Area o$ a s%uare = ! << calcArea (x) << !&n&n!;

x = ';
double ( = );
cout << !"engt# = ! << x <<!, ! << !*eig#t = ! << ( << endl;
Swap (x, ();
cout << !A$ter swapping t#e lengt# and #eig#t+++! << endl;
cout << !"engt# = ! << x << !, !<<!*eig#t = ! << ( << !&n&n!;
cout << !Area o$ a rectangle = ! << calcArea (x);
1
return ,;
-
double calcArea (double lengt#) {
return lengt# . lengt#;
-
void Swap (double a, double b) {
double temp = a;
a = b;
b = temp;
-
QUESTION 3: Complete the following program which can search through the following arra# to
find the position of an integer)
int x/01 = {2, 0), 30, ),, ),-;

$ample run 1:
,nter the integer to search: 31
+osition 1: 31
$ample run /:
,nter the integer to search: 10
+osition &: 10
+osition 1: 10
*********************************************************
#include<iostream>
using namespace std;
int main() {
int x/01 = {2, 0), 30, ),, ),-;
int target;

44 5rite (our code #ere
44 +++

return ,;
-
QUESTION 4: %rite a program to define a structure named CustomerRecord that contains name(
account_number and balance) *se arra# to store 3 records of the structure) Create / functions: one for
reading and another for printing the content of the structure in the atra#)
/

Você também pode gostar