Sunteți pe pagina 1din 2

ISE- 103 Fundamentals of Computer Programming – Lab # 6

Task 1 – Using Arrays


Write a C++ program that stores the cubes of first ten integers from 0…9 in an array.
Also display the sum of all values in the array as shown at the last line of following
sample output.

Sample output:
Value at s [ 0 ] is 0
Value at s [ 1 ] is 1
Value at s [ 2 ] is 8
Value at s [ 3 ] is 27
Value at s [ 4 ] is 64
Value at s [ 5 ] is 125
Value at s [ 6 ] is 216
Value at s [ 7 ] is 343
Value at s [ 8 ] is 512
Value at s [ 9 ] is 729
The sum of all elements of array is: 2025

Task 2 – Searching a value from Arrays


Write a program that searches an array for the given integer and returns its position
(index) in the array. This type of searching is also known as Linear Searching. Compare
each element of an array with a search value using some loop. If the search value is
present in the array, display its index in the array. If the search value is not present in
the array, display some message indicating that element is not present in the array.

Sample array: int s[] = { 2, 34, 18, 45, 89, 100, 23, 67 }

Sample output:
Enter search value: 18
Search completed! Value found at index 2 of array.
Enter search value: 95
Search completed! Value not found.

Task 3: (Searching using function)
Write a program that searches an array for the given integer using functions and that
function returns its position in the array.

• You need to declare an array of 10 elements as given below.


• Then ask the user to enter search value.
• Call the search() function and pass both array and search value to the function.
• Compare each element of an array with a search value.
• If the search value is present in the array, return its index in the array.
• If the search value is not present in the array, return some value indicating that
element is not present in the array. (e.g. ‐1)

Sample Output: For example, your program output should be as follows.


Enter search value: 18
Search completed! Value found at index 2 of array.
Enter search value: 95
Search completed! Value not found.

S-ar putea să vă placă și