Lab 05

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

COMPUTER SCIENCE PROGRAM

FACULTY OF COMPUTING AND INFORMATICS UNIVERSITI MALAYSIA SABAH


KT14303 PROGRAMMING PRINCIPLES

Lab 05 Functions

Student Matric No : BI22160523


Student Name: IRFA IRSYA BINTI SUHAIZAN
Program: HC00/ HC05 / HC14

Learning Objectives/Outcomes:
"Upon completion of this lab, the student should be able to:"
i. define a function prototype
ii. define function header and parameters
iii. call a function and pass arguments correctly
"iv. differentiate local and global variable's scope, as well as pass arguments
correctly between the functions."
v. apply the top-down algorithm and divide-and-conquer approach in solving the
complex programming problems
INSTRUCTION: ANSWER ALL QUESTIONS

Practice 1: Working with SmartV3 system (Estimate 15 minutes to 30 minutes)


"Write a C program that asks the user to enter a minimum value and a maximum value.
Then, the program will display all the even numbers between the range of minimum
and maximum value."
Sample Run :
Your program should include main function to invoke :
 getMaximum(): This method will prompt the user to enter the maximum value and
return it as integer.
 getMinimum() : This method will prompt the user to enter the minimum value and
return it as integer.
 findEven() : This method will accept two parameters : maximum and minimum
value and will display the even numbers between the maximum and minimum value.
"Enter a minimum value >> 11
Enter a maximum value >> 20
The even numbers between the 11 and 30 are: 12, 14, 16, 18"

Practice 2: (Estimate 15 minutes to 30 minutes)


"Write a program that reads three user inputs and then solve the values using a
function sorting (int a, int b, int c) that swaps its three arguments to arrange
them in increasing order. For example,"
int a = 3; int b = 4; int c = 1;
"sort3(a, b, c); // a is now 1, b is now 3, c is now 4"

Practice 3: (Estimate 30 minutes to 60 minutes)


Credit card numbers follow certain patterns. A credit card number must have between
13 and 16 digits. It must start with:
4 for Visa cards
5 for Master cards
37 for American Express cards 6 for Discover cards
"In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card
numbers. The algorithm is useful to determine if a card number is entered correctly
or if a credit card is scanned correctly by a scanner. Almost all credit card
numbers are generated following this validity check, commonly known as the Luhn
check or the Mod 10 check, which can be described as follows (for illustration,
consider the card number 4388576018402626):"
"1. Double every second digit from right to left. If doubling of a digit results
in a two-digit number, add up the two digits to get a single-digit number."
2 * 2 = 4
2 * 2 = 4
4 * 2 = 8
1 * 2 = 2
6 * 2 = 12 (1 + 2 = 3)
5 * 2 = 10 (1 + 0 = 1)
8 * 2 = 16 (1 + 6 = 7)
4 * 2 = 8
2. Now add all single-digit numbers from Step 1.
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
3. Add all digits in the odd places from right to left in the card number.
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
4. Sum the results from Step 2 and Step 3.
37 + 38 = 75
"5. If the result from Step 4 is divisible by 10, the card number is valid;
otherwise, it is invalid."
"For example, the number 4388576018402626 is invalid, but the number
4388576018410707 is valid."
Apply the divide and conquer problem solving skill to validate the credit card
number. Write a program that prompts the user to enter a credit card number as a
long integer (13 – 16 digits). Display whether the credit card number is valid or
invalid.
Here are sample runs of the program:
SAMPLE RUN 1:
SAMPLE RUN 2:
This program calculates the average of three test scores. Enter a credit card
number as a long integer: 4388576018410707 4388576018410707 is valid
"Enter a credit card number as a long integer: 4246345689049834
4246345689049834 is invalid"

ANSWER:

PRACTICE 1:

#include <iostream>
using namespace std;
int getMinimum(void);//function prototype
"int getMaximum(void); void findEven(int, int);"
int main()
{
int min = getMinimum();
"int max = getMaximum(); findEven(min, max); return 0;"
}
"//12,14,16,18"
"void findEven(int min, int max) {"
"cout << ""The even numbers between"" << min << ""and"" << max << ""are : "" <<
endl; if (min % 2 != 0) {"
min = min + 1;
}
else {
}
min = min + 2;
while (min < max) {
cout << min; min += 2;
}
if (min < max) {
"cout << "", "";"
}
}
int getMinimum(void) {
int input;
"cout << ""Enter a minimum value >> ""; cin >> input;"
return input;
}
int getMaximum(void) { int input;
"cout << ""Enter a maximum velue >> ""; cin >> input;"
return input;
}

PRACTICE 2:

#include <iostream>
using namespace std;
"void sort3(int, int, int);"
int main() {
"int a, b, c;"
"cout << ""int a = ""; cin >> a;"
"cout << ""int b = ""; cin >> b;"
"cout << ""int c = ""; cin >> c;"
"sort3(a, b, c);"
return 0;
}
"void sort3(int a, int b, int c) {"
if (a > c)
"swap(a, c); if (a > b)"
"swap(a, b); if (b > c)"
"swap(b, c);"
"cout << a << "" "" << b << "" "" << c;"
return;
}

PRACTICE 3:

#include <iostream>
using namespace std;
int sumDigits(int y)
{
"int x, sum = 0;"
while (y > 0)
{
x = y % 10;
sum = sum + x; y = y / 10;
}
return sum;
}
int main()
{
"cout << ""This program calculates the average of three test scores."" << endl;"
long int number;
"cout << ""Enter a credit card number as a long integer: ""; cin >> number;"
"int sum1 = 0, sum2 = 0;"
bool flag = false;
long int temp = number;
while (temp != 0)
{
if (flag)
{
sum1 += sumDigits((temp % 10) * 2);
}
flag = !flag;
temp = temp / 10;
}
temp = number;
flag = true;
while (temp != 0)
{
if (flag) {
sum2 += temp % 10;
}
flag = !flag;
temp = temp / 10;
}
if ((sum1 + sum2) % 10 == 0)
"cout << number << "" is valid"" << endl; else"
"cout << number << "" is invalid"" << endl; return 0;"
}

*************************** THE END ******************************

You might also like