V.Vidhya Logo

V.Vidhya

Arrays


Q1.

What will be the output of the following programs?

You have to predict the output yourself without running/executing the code.


Q2.

Write a program that stores five grocery item names in one array and prints the last last item from grocery list, then stores their prices in a second array and prints the second item from price list.

No need to take user input. Just pre-initialise the values in the array.

Expected Output:

// item names: salt, banana, potato, butter, honey
// item prices: 200, 30, 150, 75, 325

Name of the last item: honey
Price of the second item: rs.30

Q3.

Write a program that allows a teacher to enter a student’s marks for 5 subjects, then displays all the marks, total and percentage.

Expected Output:

// input
Enter marks of
subject1: 75
subject2: 65
subject3: 80
subject4: 67
subject5: 73

// output
Student Marks for
subject1: 75
subject2: 65
subject3: 80
subject4: 67
subject5: 73

Total: 360
Percentage: 72

Q4.

What will be the output of the following programs?

You have to predict the output yourself without running/executing the code.


Q5.

Write a program that stores the temperatures (in celsius) of a city for a week (7 days) in an array. Allow the user to enter the temperatures and then:

Expected Output:

// input
Enter the temperature (°C) for
day1: 31
day2: 29
day3: 33
day4: 27
day5: 30
day6: 25
day7: 28

// output
Temperatures for
day1: 31°C
day2: 29°C
day3: 33°C
day4: 27°C
day5: 30°C
day6: 25°C
day7: 28°C

Average Temperature (°C): 29
Lowest Temperature of the week (°C): 25 on day6
Highest Temperature of the week (°C): 33 on day3

Q6.

A teacher wants to analyze how students performed in a multiple-choice test with 10 questions. The correct answers are stored in an array. The teacher then inputs a student’s answers into another array. Your program should:

Expected Output:

// correct answers
correct-answers: A, B, C, D, A, B, C, D, A, B

// input
Enter student answers: A B D D A C C D B B

// output
Total correct answers: 7
Score: 70

Q7.

A streaming service has just released a new movie and collected star-ratings from viewers. Your program should:

Expected Output:

// input
Enter 20 ratings (1 - 5):
1 3 3 4 2 2 2 3 4 3 1 4 3 4 5 2 3 3 4 5

// output
Rating distribution:
1 star: 2 votes (10%)
2 star: 4 votes (20%)
3 star: 7 votes (35%)
4 star: 5 votes (25%)
5 star: 2 votes (10%)

Q8.

A parent has collected a list of popular baby names but wants an easy way to see all the options that begin with a given letter. Write a program that helps them quickly get the names starting with the letter they choose.

Expected Output

// names: Amiya, Aarav, Aarna, Avyaya, Anura, Barkha, Chhavi, Chaah, Dhithi, Dvij, Falak, 
Hanita, Izaan, Jiyaan, Krishiv, Laya, Mahi, Moh, Navya, Nirvan, Nirmay, Nahar, Nadhiya, 
Ojal. Ojas, Parishi, Praan, Rahi, Sakhi, Shay, Yuti

// case 1
Enter the first letter: N

Names found:
Nahar
Navya
Nirmay
Nirvan

// case 2
Enter the first letter: V

No names found starting with 'V'.

Q9. (Adv.)

A popular arcade game keeps a list of the five highest scores ever achieved, in descending order. Now they want to automate updating this leaderboard whenever a new score arrives.

Instructions
  • Take the five previous scores as user input (in any order).
  • Sort them into descending order.
  • Take a new player’s score as user input.
  • Then update the top five scores in descending order. (When a new score comes in, it needs to be added to that list in the right place and the lowest score dropped if there are more than five.)
Expected Output
// input
Enter the previous 5 scores: 72 99 68 87 93

// output
High-score list:
1. 99
2. 93
3. 87
4. 72
5. 68

// input
Enter the new score:
75

// output
High-score list:
1. 99
2. 93
3. 87
4. 75
5. 72

Q10. (Adv.)

You are shopping for 5 items, each with a known price. The store offers a discount coupon that costs X rupees and reduces the price of every item by Y rupees. If an item’s price is Y or less, it becomes free.

Decide whether you should buy the coupon or not. You should buy it only if the total cost after applying the discount (including the coupon’s price) is strictly less than the total cost without the coupon.

Write a program that takes X and Y as input, followed by the prices of 5 items, and prints “DO use coupon” if buying the coupon is beneficial, otherwise “DO NOT use coupon”.

Expected Output
// input
Enter the coupon price (rs): 30
Enter the discount price of the coupon (rs): 15

Enter prices (rs) of
item1: 10
item2: 40
item3: 100
item4: 25
item5: 65

// output
DO use coupon

// explanation
// Total cost of items: 10 + 40 + 100 + 25 + 65 = 240

// Total cost (after applying coupon):
// item1: (10 - 15) = free
// item2: (40 - 15) = 25
// item3: (100 - 15) = 85
// item4: (25 - 15) = 10
// item5: (65 - 15) = 50
// 0 + 25 + 85 + 10 + 50 = 170

// Total cost with discount + coupon price = 170 + 30 = 200

// Total cost without coupon: 240

// 200 < 240, so it is beneficial to use the coupon.

Prev Post
Pointers
Next Post
Multi-Dimensional Arrays