Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
Hello I am having a bit of an issue understanding how to recall and compare separate array values. I am writing a program that requires the user to enter times for each of 8 Chevy and 8 Ford race cars. Any help with this would be greatly appreciated. Thanks in advance!

First I must display the values which I have done.

Second I must compare the entered values and display the winner i.e. lowest time.

Third I must calculate the whether the Chevy team or Ford team had the most wins i.e. largest amount of lowest times.

Basically I cannot figure out how to recall the values from two separate arrays in order to compare the values.


C#
static void Main(string[] args)
        {
                int chevyTime;
                int fordTime;

                double[] chevy = new double [7];
                double[] ford = new double [7];

                for (chevyTime = 0; chevyTime < 7; chevyTime++)
                {
                    Console.Write("Please enter the times for the Chevy cars ");
                    chevy[chevyTime] = Convert.ToDouble(Console.ReadLine());
                }
                for (fordTime = 0; fordTime < 7; fordTime++)
                {
                    Console.Write("Please enter the times for the Ford cars ");
                    ford[fordTime] = Convert.ToDouble(Console.ReadLine());
                }
                Console.WriteLine("Chevy Times: ");
                for (chevyTime = 0; chevyTime < 7; chevyTime++)
                {
                    Console.Write(" {0} ", chevy[chevyTime]);
                    //How do I make this work for every number stored in the array rather than just 0?
                    if (ford[0] == chevy[0])
                    {
                        string result = "Tie!";
                        Console.WriteLine(result);
                    }
                    else if (ford[0] < chevy[0])
                    {
                        string result = "Chevy Lost...";
                        Console.WriteLine(result);
                    }
                    else if (ford[0] > chevy[0])
                    {
                        string result = "Chevy Won!";
                        Console.WriteLine(result);
                    }

                }
                Console.WriteLine("\nFord Times: ");
                for (fordTime = 0; fordTime < 7; fordTime++)
                {
                    Console.Write(" {0} ", ford[fordTime]);
                    //How do I make this work for every number stored in the array rather than just 0?
                    if (ford[0] == chevy[0])
                    {
                        string result = "Tie!";
                        Console.WriteLine(result);
                    }
                    else if (ford[0] > chevy[0])
                    {
                        string result = "Ford Lost.";
                        Console.WriteLine(result);
                    }
                    else if (ford[0] < chevy[0])
                    {
                        string result = "Ford Won!";
                        Console.WriteLine(result);
                    }
                }
                Console.ReadLine();
Posted
Updated 10-Oct-14 10:32am
v5
Comments
[no name] 10-Oct-14 14:05pm    
Okay so what is the one specfic problem with your code that you expect people to help you with? Just dumping your code here and saying that you have a problem does not tell us anything.
Member 11136294 10-Oct-14 16:36pm    
Thank you for your input. I have updated the post in order to be more precise. I would appreciate any further assistance. Thanks
[no name] 10-Oct-14 17:17pm    
Okay.... I am not sure what the problem is. You access the elements of an array using an index which is what youare supposed to be using chevyTime and fordTime for. It's not clear to me if you want to only compare chevy[0] to ford[0] or every chevy to every ford or what exactly it is that you want to do.

Oh and "not working" tells us nothing about the problem you see on your screen. We can't possibly know what you think is not working or the data you put into the program or what output you got unless you tells us.
Member 11136294 10-Oct-14 19:15pm    
You hit the nail on the head. I am trying to compare every chevyTime in the chevy array to every fordTime in the ford array. As of right now the program is outputting every chevyTime and every fordTime alongside the outcome of the IF statement. However, it is only comparing chevy[0] and ford[0].

I tried using chevy[chevyTime] and ford[fordTime] in place of chevy[0] and ford[0]. The program accepts each time input, and then I get the message below...

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Console VS Sand Box.exe

Additional information: Index was outside the bounds of the array.
[no name] 10-Oct-14 21:26pm    
If that is really what you want, then the example found at http://www.tutorialspoint.com/csharp/csharp_nested_loops.htm will show you an example of a nested for loop. Using a nested for loop would allow you to loop through both arrays instead of using 2 different for loops like you are using, comparing each "time" against all of the times in the other array.

The index out of range exception is odd. Your code works fine for me without producing the exception. It simply means that you are trying to access an element of an array using an index the is either larger than or smaller than the number of elements in the array.

With respect; your question shows lack of understanding of the classes you are using; as such, array usage in general and reference to array index. You could also simplify your use of code by using easier methods.

Dictionary, List Of T, Or Array - all of which I will reference links for you to study to identify the errors in your logic, and to demonstrate other methods as alternatives. From there, if you still have questions regarding these links, feel free to reply, and I will be happy to help answer them for you.

List Of T: http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx?cs-save-lang=1&amp;cs-lang=csharp#code-snippet-1[^]

Dictionary: http://www.dotnetperls.com/dictionary[^] (Best option in my opinion.)

Create a declaration for either of the methods above at class level; example:
C#
namespace Test1C_Sharp
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        int chevyTime = 4; //Put your input values here.
        int fordTime = 2;
        Dictionary<int, string> eDiction = new Dictionary<int, string>();
        string chevy = "chevy"; //Put your input values here.
        string ford = "ford";
        private void Main_Load(object sender, EventArgs e)
        {
            {
                eDiction.Add(chevyTime, chevy); //Adds the time and car to the dictionary.
                eDiction.Add(fordTime, ford);
                docheck();
            }
        }
        private void docheck()
        {
            //For and check Dictionary here using the link i provided.
        }     
    }
}


While in your first loop: Adding chevy and the time to a list. You can then add the same key/values from the first for statement to your list. And do the same for the for ford statement too....

In the third and forth for statements, you can then access your list/dictionary by iterating and checking the keys and values. Thus giving you access to the values of the first for statement.

Your logic needs some touching up. How are you changing the times?

If you rethink your logic from your first four declarations, you could do this from with-in one for statement using the links I gave you.

If you do not wish to do this any other way, you will need to read up on the array class: http://msdn.microsoft.com/en-us/library/system.array%28v=vs.110%29.aspx?cs-save-lang=1&amp;cs-lang=csharp#code-snippet-1[^] for a better understanding of the basics and how to achieve this.

Personally, I think you should rewrite your logic from scratch once you've read the documents I provided. I hope this answer was helpful to you and if there is something you don't understand, let me know if you have any other questions.
 
Share this answer
 
v2
static void Main(string[] args)
        {
        int i = 0;
        double []chevy = new double[8];
        double []ford = new double[8];
        int chevyWon = 0;
        int fordWon = 0;

       for (i = 0; i < 8; ++i)
       {
           Console.Write("Enter the time " + (i + 1) + " for Chevy: ");
           chevy[i] = Double.Parse(Console.ReadLine());
       }
            for(i = 0; i < 8; ++i)
        {
              Console.Write("Enter the time "+(i+1)+" for Ford: ");
              ford[i] = Double.Parse(Console.ReadLine());
        }
            Console.WriteLine("\nThe individaul winners are...");
            for (i = 0; i < 8; ++i)
            {
                if (chevy[i] < ford[i])
                {
                    Console.WriteLine("Chevy by " + (ford[i] - chevy[i]) + " ");
                    chevyWon++;
                }
                else if (chevy[i] > ford[i])
                {
                    Console.WriteLine("Ford by " + (chevy[i] - ford[i]) + " ");
                    fordWon++;
                }
                else
                {
                    Console.WriteLine("Tie!");
                }
 
            }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900