CS101_Lab_11
CS101_Lab_11
CS101_Lab_11
Review
Arrays
• To pass an array to a function as parameter it is possible to use
these ways:
– void myFunction(int param[10])-formal parameters as a sized
array
– void myFunction(int param[],int size)-pass an array and size
Strings
• Strings are objects that represent sequences of characters.
• Instead of using cin¿¿ function, you can get the entered line of
text using getline().
• getline() function takes the input stream as the first parameter
which is cin and str as the location of the line to be stored.
string s;
getline(cin,s);
Page 2
Problems
Page 3
Problem 1
Page 4
Problem 2
Page 5
Problem 3
Page 6
Problem 4
Write a function that returns the index of the largest element
in an array of integers. If there are more such elements than
one, return the largest index. If the size is equal or less than 0,
return -1. Use the following header:
Page 7
Problem 5
(Strictly identical arrays) Two arrays list1 and list2 are strictly
identical if they have the same length and list1[i] is equal to
list2[i] for each i. Write a function that returns true if list1 and
list2 are strictly identical using the following header, (If the size
is equal or less than 0, return false):
Write a test program that prompts the user to enter two lists of
integers and displays whether the two are strictly identical.
Page 8
Example:
Input: Input:
n1=5 n1=5
4 1 5 0 -1 4 1 5 0 -1
n2=5 n2=5
4 1 5 0 -1 4 1 4 0 -1
Output: Two lists are Output: Two lists are not
strictly identical strictly identical
Page 9
Problem 6
Page 10
Problem 7
Page 11
Problem 8
Page 12
Problem 9
Example:
Input: 1, 2, 3 Input: 7, 6, 8, 1, 0
Output: 3, 2, 1 Output: 0, 1, 8, 6, 7
Page 13
Problem 10
Given a string s that consists of uppercase characters (A-Z),
calculate how many authentic characters in this string.
Example:
Input: AABB Input: ABCZZ
Output: 2 Output: 4
Page 14
Problem 11
Page 15
Problem 12
Node:
• I can be placed before V (5) and X (10) to make 4 and 9.
• X can be placed before L (50) and C (100) to make 40 and
90.
• C can be placed before D (500) and M (1000) to make 400
and 900.
Example:
Input: III Input: CMXLIV
Output: 3 Output: 954
Page 16
Problem 13
Page 17
Problem 14(extra)
Given an array of integers nums and an integer target, print
indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one so-
lution, and you may not use the same element twice.
Example:
Input: Input:
nums = [2,7,11,15], target = 9 nums = [3,2,4], target = 6
Output: [0,1] Output: [1,2]
Page 18
Problem 15(extra)
Print the index of the element that does not equal others. The
numbers in the array are numbered from one.
Example:
Input: n=7 Input: n=5
6646666 11 11 11 11 1
Output: 3 Output: 5
Page 19