0
0
C Sharp (C#)programming~15 mins

Why arrays are needed in C Sharp (C#) - See It in Action

Choose your learning style9 modes available
Why arrays are needed
📖 Scenario: Imagine you run a small shop and want to keep track of the prices of exactly 5 different fruits you sell every day.
🎯 Goal: You will create a program that stores the prices of 5 fruits using an array, then calculates the total cost if you buy one of each fruit.
📋 What You'll Learn
Create an array called fruitPrices with exactly 5 decimal values representing prices.
Create a variable called totalCost to hold the sum of all prices.
Use a for loop with the variable i to add each price in fruitPrices to totalCost.
Print the totalCost with a message.
💡 Why This Matters
🌍 Real World
Stores lists of items like prices, names, or measurements to manage data efficiently.
💼 Career
Understanding arrays is essential for programming jobs that handle collections of data, like software development and data analysis.
Progress0 / 4 steps
1
Create the array of fruit prices
Create an array called fruitPrices with these exact decimal values: 1.20m, 0.50m, 0.75m, 1.00m, 1.50m.
C Sharp (C#)
Need a hint?

Use decimal[] fruitPrices = { ... }; to create the array with the exact prices.

2
Create a variable to hold the total cost
Create a variable called totalCost of type decimal and set it to 0m.
C Sharp (C#)
Need a hint?

Use decimal totalCost = 0m; to start with zero total cost.

3
Add all fruit prices using a for loop
Use a for loop with the variable i to add each price in fruitPrices to totalCost. The loop should run from i = 0 to less than fruitPrices.Length.
C Sharp (C#)
Need a hint?

Use a for loop from 0 to fruitPrices.Length and add each price to totalCost.

4
Print the total cost
Write a Console.WriteLine statement to print: Total cost of all fruits: followed by the value of totalCost.
C Sharp (C#)
Need a hint?

Use Console.WriteLine("Total cost of all fruits: " + totalCost); to show the result.