0% found this document useful (0 votes)
45 views4 pages

Lab (MidTerm)

jj

Uploaded by

zoyan2705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

Lab (MidTerm)

jj

Uploaded by

zoyan2705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Fall Session 2024-25

Course Name & Code: Computer Programming (CSE104)


Program & Sem: B.Tech 1st Sem
Section: D

MID-TERM LAB EXAM

Instructions:
• Students with the last digit of their Roll No as the odd number should answer
Questions 1. 3 and 5 while those with the last digit of their Roll No as the even number
(or 0) should answer Questions 2, 4 and 6. (Be very careful which question you are
answering. If you attempt the wrong question, you will be marked a penalty of 50% of
the marks assigned for the corresponding part).

Question 1: Write a C Program to find the kth occurrence of an odd integer in a sequence of
non-negative integers.

Input Data:

You will provide the input in 2 lines:


The first line contains a positive integer k.
In the next line, you will give a sequence of numbers separated by a single space and terminated
by -1.
You have to find the kth occurrence of an odd integer in the sequence.
Note: The -1 is not part of the sequence.

Output Data:
If there are k odd numbers in the sequence, then output the kth occurrence of the odd number
in the sequence. If there are less than k odd numbers in the sequence, output -1.

Sample Input

1 4 8 3 6 5 2 3 4 1 -1

Sample Output

Explanation: The second odd integer in the list is 3. [5]

Question 2: Write a C Program to find “two-moving average” of a sequence of non-negative


numbers.

The two moving average is the sequence of averages of the last 2 entries.
For the first number, no average is output.
For example, if the sequence of numbers is a1, a2, a3, a4, a5
The 2-moving average is: (a1+a2)/2, (a2+a3)/2, (a3+a4)/2, (a4+a5)/2

Input Data:

The input is a sequence of non-negative numbers separated by a single space and terminated
by a -1.
There will be at least 3 numbers in the sequence.
Note: The -1 is not part of the sequence. It is just to indicate that the input has ended.

Output Data:

You have to output the moving average of the sequence. The output should be printed correct
to one digit after the decimal.

Sample Input
1 3 2 4 -1

Sample Output
2.0 2.5 3.0

Explanation:
(1+3)/2 = 2
(3+2)/2 = 2.5
(2+4)/2 = 3.0 [5]

Question 3: A series of n numbers is given: a1, a2, …, an. Calculate the length of the longest
increasing sequence of consecutive elements in the series of numbers.

Input Data:
The input data is read from the console. The first line holds an integer n (0 ≤ n ≤ 1000). On the
next n lines, you have to give n integers in the range [-1000 … 1000]: a1, a2, …, an.

Output Data:
On the console, we must print one number – the length of the longest increasing sequence.

Sample Input and Output.

Input 1
3
5
2
4
Output 1
2

Input 2
4
1
2
4
4
Output 2
3

Input 1
4
5
6
7
8
Output 1
4 [5]

Question 4: We are given n integers: a1, a2, …, an. Calculate the sums as per the following
logic:

sum1 = a1 + a4 + a7 + … (sum of the numbers, starting from the first one with step of 3).
sum2 = a2 + a5 + a8 + … (sum of the numbers, starting from the second one with step of 3).
sum3 = a3 + a6 + a9 + … (sum of the numbers, starting from the third one with step of 3).

Input Data:
The input data is read from the console. The first line holds an integer n (0 ≤ n ≤ 1000). On the
next n lines, you have to give n integers in the range [-1000 … 1000]: a1, a2, …, an.

Output Data:
On the console, the program should print 3 lines containing the 3 sums in a format such as in
the example.

Sample Input and Output.

Input 1
2
3
5
Output 1
sum1 = 3
sum2 = 5
sum3 = 0

Input 2
4
7
-2
6
12
Output 2
sum1 = 19
sum2 = -2
sum3 = 6

Input 1
5
3
5
2
7
8
Output 1
sum1 = 10
sum2 = 13
sum3 = 2 [5]

Question 5: Write a C Program to print the following pattern: [5]

* * (3 spaces b/w stars)


** (1 space b/w star)
*

Question 6: Write a C Program to print the following pattern: [5]

*
** (1 space b/w star)
* * (3 spaces b/w stars)

--------------------------------------------End of Paper---------------------------------------------------

You might also like