0% found this document useful (0 votes)
2 views19 pages

CS101_Lab_11

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

CS101 Introduction to Programming Lab session - # 11

Instructors: Khumoyun Aminaddinov, Yuliya Durova, Oybek Abdullaev, Nar-


giza Khudoykulova Term: Fall 2024

Topic: Arrays, Strings

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);

• length() - returns length of string.


• size() - returns length of string.
• at() - get character in string

Page 2
Problems

Page 3
Problem 1

Write a C++ program to enter n integer numbers, store them


and find their sum.
Example:
Input: n=5 Input: n=8
6 1 -9 4 2 3 12 8 0 -7 -1 4 -1
Output: sum=4 Output: sum=18

Page 4
Problem 2

Write a function that returns an average number of elements


in array. First input size n, and then the elements.
Example:
Input: Input:
4 6
4441 90 -3 -25 44 33 -10
Output: 3.25 Output: 21.5

Page 5
Problem 3

Write a function that takes an array as parameter and finds the


minimum element. User should input the size and all elements.
Example:
Input: Input:
5 4
5 3 -2 -4 1 90 -3 33 -10
Output: Min=-4 Output: Min=-10

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:

int indexOfLargestElement(double array[], int size)

Write a test program that prompts the user to enter 15 numbers,


invokes this function to return the index of the largest element,
and displays the index.

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):

bool strictlyEqual(const int list1[], const int list2[],


int size)

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

Write a C++ program that stores n characters in an array


and determines their type: uppercase, lowercase, digit or special
character.
Example:
Input:
n=9
5Tx? 4Mw%5
Output:
Uppercase:2
Lowercase:2
Digit:3
Special:2

Page 10
Problem 7

Write a program that sorts elements of array of integers, that


are entered by user.
Example:
Input: n=4 Input: n=6
5143 4 0 -3 5 2
Output: 1 3 4 5 Output: -3 0 2 4 5

Page 11
Problem 8

(Game: Rolling a die) A die has six faces representing val-


ues 1, 2, . . . , 6, respectively. Write a program that rolls a
die 10000, times and displays the number of occurrences of each
value.

Page 12
Problem 9

Write a function to reverse an array using iteration. Function


header:

void reverseArray(int arr[], int start, int end)

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

In your program there is an integer variable x, that is equal to 0.


A user inputs a number n, that is a number of operations. Next,
n strings are given and according to them, x should be changed.
In total there are four operations: x + +, x − −, + + x, − − x.
Example:
Input: 4 Input: 4
++x −−x
x++ x++
+ +x x−−
x++ + +x
Output: 4 Output: 0

Page 15
Problem 12

Write a program to convert a Roman number to integer.

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

Write a program that would change the letters’ register in every


word so that it either only consisted of lowercase letters or, vice
versa, only of uppercase ones. At that as little as possible letters
should be changed in the word. For example, the word HoUse
must be replaced with house, and the word ViP — with VIP.
If a word contains an equal number of uppercase and lowercase
letters, you should replace all the letters with lowercase ones.
For example, maTRIx should be replaced by matrix.
Example:
Input: ViP Input: MatRIx
Output: VIP Output: matrix

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)

You are given an array a consisting of n (n ≥ 3) positive inte-


gers. It is known that in this array, all the numbers except one
are the same (for example, in the array [4,11,4,4] all numbers
except one are equal to 4).

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

You might also like