This program is for question 4. When the area code is 262 program is executing well but program is not working for other area codes. Please correct the error. I attach the question.
using System;
namespace _6e4
{
class Program
{
static void Main(string[] args)
{
int[] code = {262, 414, 608, 715, 815, 920};
double[] rate = {0.07, 0.10, 0.05, 0.16, 0.24, 0.14};
double cost;
Console.Write("Enter the area code ");
string s = Console.ReadLine();
Console.Write("Number of minutes talk ");
string t = Console.ReadLine();
for (int x = 0; x < code.Length; ++x)
{
if(Convert.ToInt32(s)==code[x])
{
cost = rate[x] * Convert.ToDouble(t);
Console.WriteLine("Total cost = {0}", cost.ToString("C"));
x = code.Length;
}
Console.ReadKey();
}
}
}
}
Loading

VulpesRank #55 · 29.2K reputationPosted Dec 22, 2011, 8:45 AM EST
What makes you think it's not working properly for codes other than 262?
Posted Dec 22, 2011, 1:23 PM EST
while (x >= 0 && miles < distance[x]) - working
while (x >= 0 && miles < 500) – not working
using System;
public class WhippetBus
{
public static void Main()
{
double[] distance = { 0, 100, 300, 500 };
int[] ticketprice = { 25, 40, 55, 70 };
int x = 3;
Console.Write("Enter Distance in miles: ");
double miles = Convert.ToDouble(Console.ReadLine());
while (x >= 0 && miles < distance[x])
--x;
Console.WriteLine("Ticket Price for {0} mile is {1}", miles, ticketprice[x].ToString("c"));
Console.ReadKey();
}
}
/*
Enter Distance in miles: 400
Ticket Price for 400 mile is $55.00
Enter Distance in miles: 700
Ticket Price for 700 mile is $70.00
*/
VulpesRank #55 · 29.2K reputationPosted Dec 23, 2011, 3:16 AM EST
Posted Dec 23, 2011, 8:02 AM EST
Error message
'double' does not contain a definition for 'percentcommission' and no extension method 'percentcommission' accepting a first argument of type 'double' could be found (are you missing a using directive or an assembly reference?)
using System;
public class Commission
{
public static void Main()
{
double[] carprices = {0, 15000, 24000};
double[] percentcommission = {0.05, 0.07, 0.10};
int x = 2;
Console.Write("Enter the car price: $");
double price = Convert.ToDouble(Console.ReadLine());
while (x >= 0 && price < carprices[x])
--x;
Console.WriteLine("Commission: {0}", (price.percentcommission[x]).ToString("c")); //dot(.) instead of *
Console.ReadKey();
}
}
VulpesRank #55 · 29.2K reputationPosted Dec 23, 2011, 8:18 AM EST
There isn't a member called 'percentcommission' and (if you know what extension methods are) there isn't one of those either which is in scope and can apply to a double instance.
Hence the error.
Posted Dec 23, 2011, 4:59 PM EST
Prices[] prices = new Prices[20];
or
double[] prices = new double[20];
VulpesRank #55 · 29.2K reputationPosted Dec 23, 2011, 11:57 PM EST
It only covers questions 4 to 8 inclusive.
Posted Dec 24, 2011, 1:19 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 24, 2011, 2:06 AM EST
So I'd use:
double[] prices = new double[20];
Posted Dec 24, 2011, 2:32 AM EST
For example in a question (Employee empArray = new Employee[7];) the object empArray contains employee ID and salary and you have to use these two properties to derive answer therefore separate class in necessary.
VulpesRank #55 · 29.2K reputationPosted Dec 24, 2011, 3:39 AM EST
Posted Dec 24, 2011, 5:54 PM EST
//pdf241
using System;
public class Prices
{
public static void Main()
{
Prices[] pArray = new Prices[20]; ;
double ePrice, sum = 0, average = 0;
for (int x = 0; x < pArray.Length; ++x)
{
pArray[x] = new Prices();
Console.Write("Prices #{0} $", x + 1);
ePrice = Convert.ToDouble(Console.ReadLine());
pArray[x].SetPrice(ePrice);
sum = sum + ePrice;
average = sum / pArray.Length;
}
Console.WriteLine("\nTotal: {0}", sum.ToString("c"));
Console.WriteLine("\nPrices less than $5.00");
Console.WriteLine("----------------------");
for (int x = 0; x < pArray.Length; ++x)
if (pArray[x].GetPrice() < 5)
Console.WriteLine("Prices #{0} ${1}", x + 1, pArray[x].GetPrice());
Console.WriteLine("\nAverage: ${0}", average.ToString("f2"));
Console.WriteLine("\nPrices higher than average");
Console.WriteLine("--------------------------");
for (int x = 0; x < pArray.Length; ++x)
if (pArray[x].GetPrice() > average)
Console.WriteLine("Prices #{0} ${1}", x + 1, pArray[x].GetPrice());
Console.WriteLine();
Console.ReadKey();
}
}
class Prices
{
double values;
public double GetPrice()
{
return values;
}
public void SetPrice(double p)
{
values = p;
}
}
VulpesRank #55 · 29.2K reputationPosted Dec 25, 2011, 12:08 AM EST
Posted Dec 25, 2011, 11:08 AM EST
using System;
namespace _6e11
{
class CarRecords
{
static void Main(string[] args)
{
Car[] car = new Car[5];
car[0] = new Car(1111, "Fiat", "11000", "Black", 5000);
car[1] = new Car(2222, "Honda", "2000", "Red", 6000);
car[2] = new Car(3333, "Camery", "XYZ100", "Green", 7000);
car[3] = new Car(4444, "Nissan", "JXA300", "Blue", 8000);
car[4] = new Car(5555, "Moris", "3BS100", "White", 9000);
DisPlayFleet(car[0], car[1], car[2]);//18,000
DisPlayFleet(car[0], car[1], car[2], car[3]);//26,000
DisPlayFleet(car[0], car[1], car[2], car[3], car[4]);//35,000
Console.ReadKey();
}
public static void DisPlayFleet(params Car[] cArray)
{
int sum=0, carPrice=0;
for (int x = 0; x < cArray.Length; ++x ) //how to use foreach instead of for
{
sum = sum + cArray[x].price;
carPrice = cArray[x].price;
Console.WriteLine("Price of Car[{0}] = {1}", x, carPrice.ToString("C"));
}
Console.WriteLine("Sum = {0}", sum.ToString("C"));
Console.WriteLine();
}
}
}
class Car
{
public int idNumber { get; set; }
public string make { get; set; }
public string model { get; set; }
public string colour { get; set; }
public int price { get; set; }
public Car(int idNumber, string make, string model, string colour, int price)
{
this.idNumber = idNumber;
this.make = make;
this.model = model;
this.colour = colour;
this.price = price;
}
}
VulpesRank #55 · 29.2K reputationPosted Dec 25, 2011, 2:44 PM EST
If you want to use foreach instead of for, the code will be:
Posted Dec 25, 2011, 3:07 PM EST
"THERE IS NO ENROLLMENT FIGURE MORE THAN....." This message must come on the console screen when appropriate. How it is possible because if-else statement can't be used in this for loop.
VulpesRank #55 · 29.2K reputationPosted Dec 27, 2011, 12:56 PM EST
Posted Dec 27, 2011, 1:31 PM EST
VulpesRank #55 · 29.2K reputationPosted Dec 27, 2011, 1:38 PM EST
This code:
should be:
{
}
Posted Dec 28, 2011, 3:56 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 28, 2011, 4:34 AM EST
Posted Jan 2, 2012, 10:37 AM EST
if (!zipNo)
{
Console.WriteLine("Company not delivered to Zip {0}", s);
}
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] zip = new string[5] { "1000", "2000", "3000", "4000", "5000" };
Console.Write("Entre a ZIP code ");
string s = Console.ReadLine();
bool zipNo = false;
for (int x = 0; x < zip.Length; ++x)
{
if (Equals(s, zip[x]))
{
Console.WriteLine("Company dilivers {0}", zip[x]);
zipNo = true;
x = zip.Length;
}
}
if (!zipNo)
{
Console.WriteLine("Company not delivered to Zip {0}", s); //?????????????????
}
Console.ReadKey();
}
}
}
VulpesRank #55 · 29.2K reputationPosted Jan 2, 2012, 1:56 PM EST
"Company not delivered to Zip "
followed by whatever the user input.
zipNo will be false if whatever zip code the user entered is not in the array.
Posted Jan 2, 2012, 2:28 PM EST
Suppose we say input does not match the given zip code. bool zipNo = false; is define in the program therefore if(!zipNo) means if(notFalse) as this is opposite to what was define in the program, in this circumstances how program can execute the statement within the if.
VulpesRank #55 · 29.2K reputationPosted Jan 2, 2012, 11:35 PM EST
So, if it's not true, it's false and, if it's not false, it's true.
An 'if' statement only executes if its condition is true.
When applied to a bool, the '!' operator changes a true value to false and vice versa.
If zipCode is false, then !zipCode is true and so the 'if' statement does execute.
Posted Jan 3, 2012, 3:56 AM EST
From this program I understood that IF statement represents the action that will take place only if the expression evaluates as true, whatever way the relevant variable is defined. Here the variable zipNo is defined as bool zipNo = false;
I wish to know whether my understanding is correct.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool zipNo = false;
if (!zipNo)
{
Console.WriteLine("Company not delivered to Zip");
}
Console.ReadKey();
}
}
}
VulpesRank #55 · 29.2K reputationPosted Jan 3, 2012, 4:11 AM EST
Here zipNo == false and so !zipNo == true.
Hence the 'if' statement will execute.
Posted Jan 7, 2012, 4:56 AM EST
if (this.sEnum > number.sEnum)
class School : IComparable
{
public string nameSchool { get; set; }
public int sEnum { get; set; }
public int CompareTo(School number)
{
int returnVal;
if (this.sEnum > number.sEnum) //???????????????
returnVal = 1;
else
if (this.sEnum < number.sEnum)
returnVal = -1;
else
returnVal = 0;
return returnVal;
}
}
VulpesRank #55 · 29.2K reputationPosted Jan 7, 2012, 8:28 AM EST
Posted Jan 7, 2012, 9:46 AM EST
number.sEnum (object dot sEnum) is indicating what?
VulpesRank #55 · 29.2K reputationPosted Jan 7, 2012, 9:54 AM EST
It must therefore have an sEnum property which we can access using the usual 'object.member' syntax.