1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C# Assignment

Discussion in 'Software Development' started by gfxrelay, Jul 1, 2008.

Thread Status:
Not open for further replies.
  1. gfxrelay

    gfxrelay Thread Starter

    Joined:
    Oct 26, 2005
    Messages:
    588
    I am currently studying C# and received a practice assignment for this:

    Write a program for a teacher.The program should have an array that can hold the
    test scores for 30 students.The program can randomly assign grades from 1 to 100 .
    Determine the average score for the class.

    This is what I came up with:

    using System;
    class Aprog{
    public static void Main(){
    int[] grade= new int[31];
    int ctr;
    System.Random rnd=new System.Random();
    for(ctr=0;ctr<31;ctr++){
    grade[ctr]=rnd.Next(0,101);
    Console.WriteLine("Student{0}, Grade:{1}",ctr,grade[ctr]);
    if(grade[ctr]>0 && grade[ctr]<50){
    Console.WriteLine("F");
    }
    else if(grade[ctr]>50 && grade[ctr]<60){
    Console.WriteLine("D");
    }
    else if(grade[ctr]>60 && grade[ctr]<70){
    Console.WriteLine("C");
    }
    else if(grade[ctr]>70 && grade[ctr]<80){
    Console.WriteLine("B");
    }
    else{
    Console.WriteLine("A");
    }
    }
    }
    }

    It works but some how it does not feel right or kind off sloppy. Does anyone know of a better way to create this application?:confused:
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi gfxrelay,

    Never refrain from using methods to make the coding more readable :
    Code:
    [SIZE=2]using System;
       class Aprog{
          public static void Main(){
             int[] grade= new int[31];
             int ctr;
             System.Random rnd=new System.Random();
             for(ctr=0;ctr<31;ctr++){
                grade[ctr]=rnd.Next(0,101);
                Console.WriteLine("Student{0}, Grade:{1}",ctr,grade[ctr]);
                Console.WriteLine(getLevel(grade[ctr]));
             }
          }
    	
          private static string getLevel(int grade) {
             if (grade >= 80) return "A";
             if (grade >= 70) return "B";
             if (grade >= 60) return "C";
             if (grade >= 50) return "D";
             return "F";
          }
       }[/SIZE]
    ;)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/726210

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice