RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Simple array program

Ruined_Xz

Wanderer
Simple array program

Ok, this is fairly simple I feel but Im doing something wrong,

Im trying to complete an excercise in a book of mine, basically says write a program with an array that a teacher coud use to keep track of 30 students test scores, it should be able to randomly assign grades from 1, 100 and then determine the average of the grades.

Well here is what I have but for some reason, I can't get it to generate the random number and add it to the array?

Code:
using System;

public class teachAssist
{
    public static void Main()
    {
        int[] testScore = new int[30];


        foreach (int grade in testScore)
        {
            System.Random rnd = new System.Random();

            testScore = (int)rnd.Next(1, 101);

            Console.WriteLine("Test Score  of {0}.", grade);
        }
        
    }
}
 

Jeff

Lord
Code:
using System;

public class teachAssist
{
    public static void Main()
    {
        // Create an array of 30 test scrores
        int[] testScores = new int[30];

        // Create a random number generator
        Random rand = new Random();

        // This basically says, create i, and while i is less then the number of test scores, do what is inside the { }
        // at the end of every loop, 'i' will have 1 added to it.
        for(int i = 0; i < testScores.Length; i++)
        {
            //  Get the grade, random number from 1 to 100
            int grade = rand.Next(1, 100);

            //  Set the test score to the array at index 'i'
            testScores[i] = grade;

            //  Write out the grade to the console.
            Console.WriteLine("Test Score  of {0}.", grade);
        }
        
    }
}
 

Ruined_Xz

Wanderer
Bah, that was more then I bargained for, some of that wasn't even discussed in the chapter. But then again I guessed it was implied that I know that as far as I am. Thanks Jeff
 

Jeff

Lord
Ruined_Xz;851014 said:
Bah, that was more then I bargained for, some of that wasn't even discussed in the chapter. But then again I guessed it was implied that I know that as far as I am. Thanks Jeff

I added comments, so re-read it.
 

Ruined_Xz

Wanderer
Hey actually this line confused me somewhat, I understand it's purpose but could you explain it some more? Like why the "i" variable is used?

Code:
//  Set the test score to the array at index 'i'
            testScores[i] = grade;

If I had to guess the i variable basically is the generated number that gets added each time by the for loop? Erm I think
 

Pure Insanity

Sorceror
The "i" variable can be any variable, as long as it's an integer. The for loop will loop through the code in the brackets, increasing the "i" variable by one each time. Till "i" reaches the end of the array, which is also defined. So the for loop will execute 30 times. The last time "i" should be 29, the reason for this being is because arrays start at 0. Confused yet?
 

Ruined_Xz

Wanderer
No not confused, you actually just provided some clarity for me haha. I knew how the array worked and the for loop, my only concern was why the i variable was used in the specific line of code I posted.
 

Pure Insanity

Sorceror
Mhmm, no problem. It's just the loop variable, so it's ever changing each time the loop is executed. Just be careful with for loops and make sure that they always finish, nothing worse then a never ending loop in programming.
 

Jeff

Lord
Just for fun, anyone know why the variable name 'i' is used?
Bonus Question: Anyone know why traditionally the variable 'j' is used for loops inside an 'i' defined loop?
Example :

Code:
for(int i = 0; i < countA; i++)
    for(int j = 0; j < countB; j++)
    {
        ....
    }
 

Jeff

Lord
Back in the days of Fortran, integer variables had to be defined as I -> N. thus 2 for loops (on inside the other) naturally went i, j :)
 
Top