Professional Documents
Culture Documents
Your program should ask for the range of x. Make sure your program check for the ending
values of x must be greater than the starting values of x.
Using the range, find and display all values of y.
The program should prompt the user whether to continue with different range of x or exit.
The output may looks as follows:-
Enter the starting values of x: 2
Enter the ending values of x: 4
When x = 2, y= -13
When x = 3, y= 6.5
When x = 4, y= 3.9
Do you want to continue? Y
Enter the starting values of x: 5
Enter the ending values of x: 4
The range is incorrect.
Do you want to continue? N
HINT: You may use Math.Pow() method to calculate the expression.
9
Answer:
class Program
{
static void Main()
{
int startX, endingX; // 2 marks
double Y;
string cont = "Y";
while (cont == "Y") // 2 marks for while loop
{
Console.Write("Enter the starting values of x: "); // 0.25 mark
startX = int.Parse(Console.ReadLine()); // 0.25 mark
Console.Write("Enter the ending values of x: "); // 0.25 mark
endingX = int.Parse(Console.ReadLine()); // 0.25 mark
if (startX > endingX) // 4 marks for if statement block
{
Console.WriteLine("Starting values must be less than ending values");
continue;
}
for (int i = startX; i <= endingX; i++) // // 4 marks for for statement block
{
Y = ((2 * Math.Pow(i, 2)) + (3 * i) - 1) / (Math.Pow(i, 2) - 5); // 2 marks
Console.WriteLine("When x = {0}, y={1} ", i, Y);
}
Console.Write("Do you want to continue (Y/N)? :");
cont = Console.ReadLine();
}
}
}
2. Write a program to enter 15 test marks into one dimensional array of type int. Give the name of
array is Score[ ].
Find the average mark
Find the lowest mark
Find the highest mark
HINT: Set the initialization of lowest and highest to the first element of Score[ ].
The output may look as below:
Enter Score 1: 90
Enter Score 2: 80
Enter Score 3: 70
Enter Score 4: 60
Enter Score 5: 85
Enter Score 6: 75
Enter Score 7: 88
Enter Score 8: 92
Enter Score 9: 53
Enter Score 10: 67
Enter Score 11: 81
Enter Score 12: 59
Enter Score 13: 64
10
Enter Score 14: 69
Enter Score 15: 82
Average: 74.33
Lowest score: 53 Highest score: 92
(15 marks)
using System;
namespace MyScore
{
public class MyScoreMark
{
public static void Main()
{
int Total = 0; // 1 mark
double Average; // 1 mark
int[] score = new int[15]; // 1 mark
//Values are entered.
for (int i = 0; i < score.Length; i++) // 3 marks
{
Console.Write("Enter Score {0}: ", i + 1);
score[i] = int.Parse(Console.ReadLine());
}
//Values are summed and check for lowest and high marks.
int high = score[0], low = score[0];
for (int i = 0; i < score.Length; i++) // 3 marks
{
Total += score[i]; // Total = Total + score[i];
if (low > score[i]) low = score[i]; // 2 marks
if (high < score[i]) high = score[i]; // 2 marks
}
Average = Total / score.Length;
Console.WriteLine();
Console.WriteLine("Average: {0:f} ", Average); // 1 mark
Console.WriteLine("Lowest score: {0} \tHighest score: {1} ", low, high);
// 1 mark
Console.ReadLine();
}
}
}