1
Reply

Teachers code is wrong/ not working will u help me fix it?

Carl Palsson

Carl Palsson

Oct 23 2010 3:33 AM
6.6k
Hello everybody!

My first post here, so please be kind if I by accident broken any of the rules.

Thing is that im doing some C# classes at the moment, and for this project we got handed code from our teacher, as we(the programmning class) are fairly new and only been coding for like 3 weeks we aren´t really that good.

anyway the code was just to be copied and pasted into our C# development. We are working the consoleapplication at the moment. The code does not work, and ive been trying to fix it myself(not included in the task but hey you gotta try). Ive been sitting for like 2 days just to get started with the homework. Can someone take a look at the code and maybe fix it so it works... The problems i get is that the array is  not the current context.. Ive been getting that error alot and it confuses me, cause i really dont understand whats the problem.. Only that i sometime when i code can escape it by declaring the variable at the top and make it public. Thanks ;)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            public void StartTask2()
      {
         try
         {
            using (StreamReader reader = new StreamReader("MyTestFile.txt"))
            {
               ProcessFile(reader);
               DisplayRelativeFrequence();
            }
         }
         catch (IOException ex)
         {
            Console.WriteLine("Error, StreamReader failed with error {0}", ex.Message);
         }
      }

private void ProcessFile(StreamReader reader)
      {
         char tecken;
         int index;

         foreach (char ch in reader.ReadToEnd())
         {
            if (Char.IsLetter(ch))
            {
               totalCount++;
               tecken = Char.ToLower(ch);
               index = tecken - 97; //index starting with 0 for a and last index for ö

               switch (tecken)
               {
                  case 'å': array[array.Length - 3]++;
                            break;
                  case 'ä': array[array.Length - 2]++;
                            break;
                  case 'ö': array[array.Length - 1]++;
                            break;
                  default:  array[index]++;
                            break;
               }
            }
         }
      }

private void DisplayRelativeFrequence()
      {
         for (char ch = 'a'; ch <= 'z'; ch++)
            Console.WriteLine("The relative frequency for letter = {0} is {1:F3}",
               ch, (double)array[ch - 97] / totalCount);
      
         //Check reelative frequency for åäö
         Console.WriteLine("The relative frequency for letter = å is {0:F3}",
               (double)array[array.Length - 3] / totalCount);
         Console.WriteLine("The relative frequency for letter = ä is {0:F3}",
               (double)array[array.Length - 2] / totalCount);
         Console.WriteLine("The relative frequency for letter = ö is {0:F3}",
               (double)array[array.Length - 1] / totalCount);
      }




        }
    }
}






Answers (1)