1
Answer

what is wrong with my code??

Ask a question
Wendy Swager

Wendy Swager

15y
1.9k
1

This is the code that I have...

using System;

using System.IO;

using System.Text;

using Util;

namespace Program_11

{

class Program

{

const string INPUT_FILE_NAME = "\\CIS110\\prg11Dat.txt";

//static uint valueArray;

static double[] numArray = new double[25];

static StreamReader fileIn;

static void ReadFiles()

{

if (File.Exists(INPUT_FILE_NAME))

{

fileIn = File.OpenText(INPUT_FILE_NAME);

Console.WriteLine("{0}" ,INPUT_FILE_NAME);

}

else

{

Console.WriteLine("Error: {0} does not exit\n",INPUT_FILE_NAME);

ConIO.Exit();

}

}

static void DisplayArray()

{

uint i;

string lineIn;

i = 1;

while ((lineIn=fileIn.ReadLine())!=null)

{

numArray[i] = Double.Parse(lineIn);

i++;

}

numArray[i] = --i;

}

static void SortValuesAscending()

{

uint i,j,k;

double tempValue;

for (i=1; i<=(numArray.Length-1); i++)

{

k = i;

for (j=(i+1); j<=numArray.Length; j++)

if (numArray[j]<numArray[k])

k = j;

if (k>i)

{

tempValue = numArray[k];

numArray[k] = numArray[i];

numArray[i] = tempValue;

}

}

}

 

static void DisplayArray(double ascending)

{

uint i;

Console.WriteLine("Array Processing");

Console.WriteLine();

for (i=1; i<=numArray.Length; i++)

Console.WriteLine("{0,6:f}",numArray[i]);

}

static void CloseFiles()

{

fileIn.Close();

}

static void Main()

{

ConIO.ClrScr();

ReadFiles();

DisplayArray();

SortValuesAscending();

CloseFiles();

}

}

}

 

I am supposed to write a program to read and display numbers from a file.  I need 3 methods, ReadFile(),that will read 25 nums stored in a file and copy to an array named numArray.  Then I need a method called DisplayArray(), and one named SortValuesAscending.  I keep getting an error when I run the program, but am not understanding what I am doing wrong.  this is the error I get...

\CIS110\prg11Dat.txt was opened

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the boun
ds of the array.
   at Program_11.Program.SortValuesAscending() in C:\CIS110\Program 11\Program.c
s:line 59
   at Program_11.Program.Main() in C:\CIS110\Program 11\Program.cs:line 97
Press any key to continue . . .

Should I be sending the values from readfile to display file using pass by copy??  I am apparently new to this and confused,  a dangerous combination for me!!

HELP...I know that it is probably an easy fix, but I am not sure what it is.


Answers (1)