Sei sulla pagina 1di 3

Venue:

Date:
Time:
Operating System:
Compiler:
Total points:

EC3041: Data Structures using C++


Assignment 1
Computer Centre
27 August, 2014
5pm 6pm
Linux
g++
10

Question 1 (1 point)
Write a program that reads numbers which are in the range 0 to 100, till it encounters -1. Print the sum
of all the integers that you have read before you have encountered -1.
INPUT:
A sequence of integers separated by whitespace. There may be other integers following -1.
OUPUT:
Sum of all integers in the sequence before you encounter -1. Any integer that is input after -1 in the
sequence should be ignored.
CONSTRAINTS:
Utmost 10 integers will be given in the input. One of them is guaranteed to be a -1.
Inputs will be in the range 0 to 100 (both included.)
Sample Test Cases

Test Case 1
Test Case 2
Test Case 3
Test Case 4

Input

Output

2 -1 2 3

-1 4 5

10 3 4 -1

17

2 2 3 4 5 6 -1 7 8 9

22

Question 2: Most Frequent Element in a Sequence (1 point)


Write a program to read a sequence on N integers and print the number that appears the maximum
number of times in the sequence.
INPUT:
Input contains two lines. First line in the input indicates N, the number of integers in the sequence.
Second line contains N integers, separated by white space.
OUTPUT:
Element with maximum frequency. If two numbers have same highest frequency, print the number that
appears first in the sequence.
CONSTRAINTS:
1 <= N <= 10000
The integers will be in the range [-100,100].

Sample Test Cases

Input

Output

Test Case 1

5
12131

Test Case 2

6
7 7 -2 3 1 1

Test Case 3

4
2323

Question 3: Print Elements of a Matrix in Spiral Order (3 points)


Write a program that reads an MxN matrix A and prints its elements in spiral order. You should start
from the element in the 0th row and 0th column in the matrix and proceed in a spiral order as shown
below.
1

7
8

9
10

11
12

13

14

15

16
Output for the above matrix: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
INPUT:
First line contains two integers M and N separated by whitespace. The next M lines contain elements of
matrix A, starting with the topmost row. Within each row, the elements are given from left to right.
OUTPUT:
Elements of the matrix printed in a spiral order. All the elements should be separated by whitespace.
CONSTRAINTS:
1 <= M <= 5, 1 <= N <= 5. Elements in the matrix will be in the range [-100,100]
Sample Test Cases
Input
Output
33
012
Test Case 1
012345678
783
654
22
Test Case 2
1 10
1 10 5 3
35
44
7812
Test Case 3
3431
7812167945134382
1286
5497
11
Test Case 4
20
20

Question 4: Substrings (5 points)


Write a program that takes two input strings S1 and S2 and finds if S2 is a substring of S1 or not. If S2 is a
substring of S1, the program should print the index at S1 at which there is a match. If S2 is not a
substring of S1, the program should print -1. If S2 appears in S1 multiple times, print the first index in S1
at which the match occurred.
INPUT:
Two strings S1 and S2, separated by whitespace.
OUTPUT:
One integer, which is either the index at which the match occurred or -1. Remember, the indices start at
0 for arrays.
CONSTRAINTS:
Each string contains at most 19 characters.
The strings will contain only letters from the alphabet (a-z and A-Z).
Match has to be case-sensitive.
Sample Test Cases
Input
Output
Test Case 1
football foot
0
Test Case 2
mickey mouse
-1
Test Case 3
abcdefghijklmnopqrs s
18
Test Case 4
helloworld helloworld
0
Test Case 5
FrodoBaggins bagg
-1
Test Case 6
Hell Hello
-1
Test Case 7
coolgoose oo
1

Potrebbero piacerti anche