Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
237 views

Lab 2 - Function and Array

This document provides instructions for a lab assignment on functions and arrays in C++. It is divided into three parts: Part A instructs how to create functions to add integers and return/not return results. Test questions involve printing even numbers and converting time formats. Part B teaches how to create and use arrays, with examples summing array values and numbers entered by the user. Test questions check for palindromes and sort entered digits. Part C assigns as homework: adding random numbers to an array without duplication, and analyzing a random number set by drawing a graph using functions and arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
237 views

Lab 2 - Function and Array

This document provides instructions for a lab assignment on functions and arrays in C++. It is divided into three parts: Part A instructs how to create functions to add integers and return/not return results. Test questions involve printing even numbers and converting time formats. Part B teaches how to create and use arrays, with examples summing array values and numbers entered by the user. Test questions check for palindromes and sort entered digits. Part C assigns as homework: adding random numbers to an array without duplication, and analyzing a random number set by drawing a graph using functions and arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CT077-3-2-DSTR Functions and Arrays

Lab 2: Functions and Arrays

Part A: Learn how to create a function in C++

Figure 1: Sample user-defined functions work in C++

1. Write a C++ program to add two integers. Make a function add() to add integers and
display sum in main() function. This function will not return any result back to the
main() function.

[Estimate Finish Time: 10 minutes]

2. Write a C++ program to add two integers. Make a function add() to add integers and
display sum in main() function. This function will return the result back to the
main() function.

[Estimate Finish Time: 10 minutes]

Test yourself questions for Part A:

1. Write a C++ program to print all even numbers between two numbers (entered by the
user) by making a user-defined function. Display the answer in main() function.
[Estimate Finish Time: 15 minutes]

2. A program is required to prompt for and accept a time and compute the number of
seconds elapsed since midnight. The time should be entered in the format
HH:MM:SS.
Output sample:

Please enter your elapsed time (in HH:MM:SS format) = 00:02:44


Elapsed time in seconds = 164 seconds
[Estimate Finish Time: 15 minutes]

Level 3 Asia Pacific University of Technology & Innovation Page 1 of 4


CT077-3-2-DSTR Functions and Arrays

Part B: Learn how to create and use Arrays in C++

1. Write a program that computes the summation of array values.


int billy [] = {16, 2, 77, 40, 12071, 51};

Output sample:

[Estimate Finish Time: 10 minutes]

2. Write a C++ program to store and calculate the sum of 5 numbers entered by the user
using arrays.

Output sample:

[Estimate Finish Time: 10 minutes]

Test yourself questions for Part B:

1. Write a program that checks if a given array is a Palindrome. Palindrome is a number


that remains the same when its digits are reversed. Like 16461, for example.

Output sample:

[Estimate Finish Time: 15 minutes]

Level 3 Asia Pacific University of Technology & Innovation Page 2 of 4


CT077-3-2-DSTR Functions and Arrays

2. Write a C++ program to store and sort 8 digit numbers that entered by the user. You
should use the arrays to store the digits. (No function is needed in this question.)
[Estimate Finish Time: 15 minutes]

3. Modify the question 2 to create a function to sort the given array.


[Estimate Finish Time: 10 minutes]

4. Write a program to create and add two matrices.

Output sample:

[Estimate Finish Time: 15 minutes]

Part C: Homework.
Submit your answer (in doc / pdf) to the Microsoft Teams. Your answer should include your
code and your program screenshot. Submission due date: 16 October 2019.

1. Using an array, add in 10 random numbers, which is in the between of 10 and


100. Do not include the duplication of numbers in the array. If the system find a
same number in that array, system should add another non duplicated number to
the array.

2. By using the concepts of function and array, develop an interactive program that
able to analyze a set of random numbers by drawing a graph as below.

Level 3 Asia Pacific University of Technology & Innovation Page 3 of 4


CT077-3-2-DSTR Functions and Arrays

Level 3 Asia Pacific University of Technology & Innovation Page 4 of 4

You might also like