Get Index Position from Array

  • Thread starter Thread starter whos_bally
  • Start date Start date
W

whos_bally

Guest
Hello all,

I'm currently writing a program that will read in 50 student names and their marks from a file, that will then display that highest mark to console.

I've so far managed to work out most of what I need to do, however I am stuck on trying to display the student's name & their mark.

The students name & their mark are two different arrays, however their index position is the same. So Joe Blogs is Index 0 and his score is also at Index 0.

I'm not sure how to adapt my code where it displays "names" within the "displayOutput" subprogram, I'm still very much learning the basics. Any help would be highly appreciated!

Here is my code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//needed for file work
using System.IO;

namespace Student_Marks_Checker
{
class Program
{
static void Main(string[] args)
{
const int MAXSIZE = 50; //sets 50 as maxsize

string[] names = new string[MAXSIZE]; // creates new string array using whatever the maxsize value is
int[] marks = new int[MAXSIZE]; // creates new int array using whatever the maxsize value is
int highestMark = topStudent(names, marks);
File.Exists("c:\\filestuff\\info.txt"); // checks if file exists
readData(names, marks); // reads data from file
topStudent(names, marks); // finds highest student mark
displayOutput(names, marks, highestMark); // displays student with highest mark
}//end main

static void displayOutput(string[] names, int[] marks, int highestMark)
{

for (int i = highestMark; i > names.Length; i++) ;
Console.WriteLine("{0} got the highest mark of ", names + highestMark + " out of 200");


//Console.WriteLine(names + "got the highest mark of " + marks + " out of 200"); // writes to console the student with the highest mark
}

static int topStudent(string[] names, int[] marks)
{
int highestMark = 0; // placeholder variable

for (int i = 0; i < marks.Length; i++) // For loop to check the highest mark, increments by 1
{
if (marks > highestMark)
{
highestMark = marks;
}
}// end for loop

return highestMark; // returns the highest value back to the placeholder variable
}
static void readData(string[] names, int[] marks)
{
StreamReader importText = new StreamReader("c:\\filestuff\\info.txt"); // creates new StreamReader object called importText, so file can be read
int i = 0; // Array's initial value
string line; // Placeholder variable for names array

while ((line = importText.ReadLine()) != null)
{
// reads in the name of student & their mark, then moves onto the next
names = line;
marks = Convert.ToInt32(importText.ReadLine());
i++;
}

importText.Close();
}

}//end class Program

}//end namespace

Continue reading...
 
Back
Top