Finished Assignment - Polishing help requested

I have submitted this code and wonder if anyone can help with a few is

Page 1 of 1

5 Replies - 2326 Views - Last Post: 07 June 2009 - 07:35 AM Rate Topic: -----

#1 skavamareen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-May 09

Finished Assignment - Polishing help requested

Post icon  Posted 06 June 2009 - 05:44 AM

/**********************************************************/
/**********************************************************/
/*PROGRAM: REDACTED by author for posting on dream.in.code*/
/*****AUTHOR: skavamareen LAST MODIFIED 08_03_09***********/
/**********************************************************/
/**********************************************************/
		
//PURPOSE: TO TAKE AN UNSPECIFIED NUMBER OF STUDENTS (LIMITED 1 TO 20 FOR THE 
//PURPOSES OF THIS EXERCISE)
//VALIDATE THE ENTRY ON INPUT, THEN REQUEST THE GRADES (VALIDATED) FOR THE 
//STUDENTS IN THE RANGE 0 TO 99.
//VARIOUS CALCULATIONS ARE THEN CARRIED OUT AND THE RESULTS OUTPUT. EACH 
//SECTION OF CODE RELATING TO ITS FUNCTION IS ANNOTATED AS A HEADER.

//CODING HAS BEEN DONE ON WIDESCREEN DISPLAYS, SO WORD-WRAP MAY OCCUR, 
//ALTHOUGH EVERY EFFORT HAS BEEN MADE TO ENSURE THAT THIS IS CORRECT AND 
//WORD_WRAP SHOULD NOT OCCUR ON NON-WIDESCREEN DISPLAY.

//THE PROGRAM RUNS FROM A C# CONSOLE AND THE USER IS PROMPTED WITH CLEAR 
//AND SIMPLE INSTRUCTIONS ON HOW TO PROCEED. 
		
//RESTRICTIONS: THE CONSOLE ACCEPTS NUMERIC ENTRY ONLY. 
		
//ERROR HANDLING: ANY NON-NuMERIC ENTRY WILL RESULT IN AN 'UNHANDLED 
//EXCEPTION' ERROR. THIS HAS BEEN INVESTIGATED, BUT THERE IS CURRENTLY NO CODE 
//IN PLACE TO COUNTER THIS. THIS MAY BE ADDRESSED IF TIME CONTRAINTS PERMIT.
	   
//ALL CODE IS THE ORIGINAL WORK OF THE AUTHOR. CODE SNIPPETS HAVE NOT BEEN 
//USED, HENCE NO ACCREDITATION NOR CITATIONS MADE.
//

//Section of code to input user defined no_of_marks (>=1 or <=20) and validate inputs.//
// This produces a variable used as a counter in the input of the student marks in the next section of code.//

using System;
using System.Collections.Generic;
using System.Text;

namespace wildside_college_exam_statistics
{

	class Program
	{
	
	static void Main(string[] args)
		{
			//Variables have been grouped logically rather than as one long line.
			//Similar or relative variables on the same line.
			string myInput;
			string mode_group = "";
			int no_of_marks;
			int fail = 0, pass = 0, merit = 0, dist = 0;
			int g0 = 0, g1 = 0, g2 = 0, g3 = 0, g4 = 0, g5 = 0, g6 = 0, g7 = 0, g8 = 0, g9 = 0, gmax = 0;
			int min_mark = 100, max_mark = 0;
			int mark, total_mark = 0;
			Console.WriteLine("Please enter number of student marks, between 1 and 20: ");
			myInput = Console.ReadLine();
			no_of_marks = int.Parse(myInput);
			while ((no_of_marks < 1) || (no_of_marks > 20))
			{
				//There is currently no validation for user entry of non-numeric character - results in error.
				//I have researched this thoroughly but not come up with anything I can currently work with.
				Console.WriteLine("Your number {0}, is not within the specified range of 1 to 20, please re-enter.", myInput);
				myInput = Console.ReadLine();
				no_of_marks = int.Parse(myInput);
			}
			Console.WriteLine("Thank you. There are {0} student marks to be entered.", no_of_marks);
			Console.Write("\nMarks range from 0 to 99. ");
			for (int i = 1; i <= no_of_marks; i++)
			{
				Console.WriteLine("\nPlease enter student mark, number {0} of {1}:\n", i, no_of_marks);
				myInput = Console.ReadLine();
				mark = int.Parse(myInput);
				while ((mark <= -1) || (mark > 99))
				{
					//Validate in the 0 to 99 range
					Console.WriteLine("{0} is not within the specified range of 0 to 99, please re-enter.", mark);
					myInput = Console.ReadLine();
					mark = int.Parse(myInput);
				}
				//Calculate grade banding
				{
					if (mark < 40)
						fail += 1;
					else
						if (mark < 65)
							pass += 1;
						else
							if (mark < 75)
								merit += 1;
							else dist += 1;
				}
				//Calculate mark groupings.
				{
					if (mark < 10)
						g0 += 1;
					else
						if (mark < 20)
							g1 += 1;
						else
							if (mark < 30)
								g2 += 1;
							else
								if (mark < 40)
									g3 += 1;
								else
									if (mark < 50)
										g4 += 1;
									else
										if (mark < 60)
											g5 += 1;
										else
											if (mark < 70)
												g6 += 1;
											else
												if (mark < 80)
													g7 += 1;
												else
													if (mark < 90)
														g8 += 1;
													else g9 += 1;
				}
				{
					//Calculate Highest and Lowest Mark using initial values of 0 for Max and 100 for Min.
					if (mark <= min_mark)
						min_mark = mark;
					if (mark >= max_mark)
						max_mark = mark;
				}
				//Sort the lowest from the highest
				total_mark += mark;  
			}
			{
				//Output all ststistics
			}
			{
				//output total of all student marks
				Console.WriteLine("\n{0} student marks submitted, and the following statistics have been generated:\n", no_of_marks);
				//output the average student mark - NB the assignment did not specify to any decimal places,
				//so I have left the output as an Integer.
				Console.WriteLine("Average Student mark (Mean Mark)= {0}", total_mark / no_of_marks);
				//output the range of student marks
				Console.Write("\nThe student marks range from {0} to {1}", min_mark, max_mark);
				Console.WriteLine(" making a range of {0} ", max_mark - min_mark);
				//output totals for each grade e.g. fail, pass, merit, distinction
				Console.WriteLine("\n{0} of the students achieved a Distinction", dist);
				Console.WriteLine("{0} of the students achieved a Merit", merit);
				Console.WriteLine("{0} of the students achieved a Pass", pass);
				Console.WriteLine("{0} of the students Failed", fail);
				//output group counters
				Console.WriteLine("\nThere are {0} marks in the group 0-9", g0);
				Console.WriteLine("There are {0} marks in the group 10-19", g1);
				Console.WriteLine("There are {0} marks in the group 20-29", g2);
				Console.WriteLine("There are {0} marks in the group 30-39", g3);
				Console.WriteLine("There are {0} marks in the group 40-49", g4);
				Console.WriteLine("There are {0} marks in the group 50-59", g5);
				Console.WriteLine("There are {0} marks in the group 60-69", g6);
				Console.WriteLine("There are {0} marks in the group 70-79", g7);
				Console.WriteLine("There are {0} marks in the group 80-89", g8);
				Console.WriteLine("There are {0} marks in the group 90-99", g9);
				//Output the mark grouping indicators
					if (g0 > gmax)
					{
						gmax = g0;
						mode_group = "0-9";
					}
					if (g1 > gmax)
					{
						gmax = g1;
						mode_group = "10-19";
					}
					if (g2 > gmax)
					{
						gmax = g2;
						mode_group = "20-29";
					}
					if (g3 > gmax)
					{
						gmax = g3;
						mode_group = "30-39";
					}
					if (g4 > gmax)
					{
						gmax = g4;
						mode_group = "40-49";
					}
					if (g5 > gmax)
					{
						gmax = g5;
						mode_group = "50-59";
					}
					if (g6 > gmax)
					{
						gmax = g6;
						mode_group = "60-69";
					}
					if (g7 > gmax)
					{
						gmax = g7;
						mode_group = "70-79";
					}
					if (g8 > gmax)
					{
						gmax = g8;
						mode_group = "80-89";
					}
					if (g9 > gmax)
					{
						gmax = g9;
						mode_group = "90-99";
					}
				//output Mode Group(s)
				Console.WriteLine("Mode group = {0}", mode_group);
				Console.ReadLine(); //for validation testing purposes
			}
		}
	}
}

Hi,

I wrote this code for an assignment a few months ago, it took literally weeks for me to write and many sleepless nights. I do not take to C# coding like a fish to water (or any flavour of coding for that matter) by any stretch of the imagination. I was hoping that someone could offer some assistance in respect of a few questions I had. I will list them below. I am also in the process of writing my second assignment and would appreciate some feedback on that one too, though I am not at the stage where I would be prepared to post any code yet.

One of the (many) shortfalls of this program - and why I got a pass instead of merit/distinction (grrr) was that there was limited validation. I could not work out how to validate against non-numerical user input before it had to be handed in. For example, if you enter anything other than a number, you will get an 'unhandled exception' error. Also, this program is not written using 'methods'. Methods is something that was inadequately explained in the lessons and is almost a complete mystery to me and there does not seem to be an idiot's guide anywhere on the net, and what I need is an idiot's guide.

This brings me on to the other thing I should mention. When researching on the internet - looking for coding snippets etc - I found that there were extremely limited resources for beginner programmers. For example, it took me two days to work out that in order to find out the highest value and a lowest value for a selection of numerical inputs, the 'low value' variable needed to be initialised to the highest value and the 'high value' variable needed to be initialised to the lowest. I realise that this may be completely obvious for an experienced programmer, but this is the sort of concept I needed help with throughout. As I said, I'm not a natural programmer. Is anyone aware of any resources available online for absolute beginners in C# concepts in CONSOLE applications? The other problem I found is that most other assistance seemed to be for 'Visual C#' (not 'Console').

I apologise for the rather lengthy post, so I will summarise (and perhaps add some bits).

Summary:

* Help on non-numerical ReadLine validation / error handling
* My coding sorts out the mode group (variable: mode_group), but if more than one group has an equal mode value, it only shows the first. How can I make it show them all?
* We were not expected to use CASE / Arrays in the assignment, how could it have been applied to this program?
* How would this program have looed if it was split into 'methods' (I'm not sure if this is relevant if arrays were not used)

I apologise again for the length of this post - and if I've broken any rules/conventions, this is my first post to this forum (or any coding forum), so please be gentle with me!

I do not need a rapid response since this is a finished assignment - but I am curently working on a new assignment which needs to be in, in under three weeks, and any feedback would help me to complete that. I still foresee many sleepless nights ahead of me - specially since this is a part time 'Higher National' course and I have several other assignments to do (easier stuff like Systems Analysis and End User Support), as well as holding down a full time job with long hours and no day-release!!

Thanks for any help that anyone can give!!!

Regards

S

ps - I know that there is a semantic error in the line 'while ((mark <= -1) || (mark > 99))' - it should be '<1') but that's how it was handed in. I don't know WHAT I was thinking :)

Is This A Good Question/Topic? 0
  • +

Replies To: Finished Assignment - Polishing help requested

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7501
  • View blog
  • Posts: 15,544
  • Joined: 16-October 07

Re: Finished Assignment - Polishing help requested

Posted 06 June 2009 - 07:16 AM

View Postskavamareen, on 6 Jun, 2009 - 06:44 AM, said:

why I got a pass instead of merit/distinction (grrr)


You're lucky. With such an assignment, not using any arrays is basically a technical failure. Putting all the logic in a static main with no methods at all, also bad. If this is not the first assignment, you should have, at the very least, gone over loops, and arrays, and methods.

The solution to a most of your problems is an array. You can store both you "g's" and every single value a student enters for later analysis.


Just using the following code instead of what you have cuts you program in half:
int g[10];
//..
g[mark/10]++;
//..

for(int i=0; i<10; i++) {
	string groupName = string.Format("{1}-{2}", i*10, ((i+1)*10)-1);
	Console.WriteLine("There are {0} marks in the group {1}.", g[i], groupName);
	if (i==0 || g[i]>gmax) {
		gmax = g[i];
		mode_group = groupName;
	}
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#3 T3hC13h   User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Finished Assignment - Polishing help requested

Posted 06 June 2009 - 10:55 AM

First I would like to applaud the effort you are putting into increasing your knowledge of C# and programming. 99% of the time all I see is "Fix my app" and "Do my homework".

Character validation can be done in many ways, some better than others, here are three I cooked up.


This block uses the int.TryParse method which will return true or false and our integer value if true. You'll notice that I've moved the Console.Readline inside the while loop, this is personal preference and IMO makes the code cleaner.
 
		Console.WriteLine("Please enter number of student marks, between 1 and 20: ");
			while (true)
			{
					  myInput = Console.ReadLine();
					  if(!int.TryParse(myInput,out no_of_marks))
					  {
						  Console.WriteLine("Invalid characters entered!");
						continue;
					  }
					  if ((no_of_marks < 1) || (no_of_marks > 20)) 
						  Console.WriteLine("Your number {0}, is not within the specified range of 1 to 20, please re-enter.", myInput);
					  else
						  break;
			}




In this piece i have used a try catch block to handle the exception thrown by the int.Parse method. You may not have learned about exception handling yet but I thought I'd throw it in anyways. A try block works by "catching" any exceptions thrown by the code it contains. If the input is valid the exception the code will execute the IF statement and continue on its merry way. If it catches an exception the program will jump to the code in the catch block.
  int no_of_marks=0;
			while (true)
			{
				myInput = Console.ReadLine();
				try
				{
				no_of_marks = int.Parse(myInput);
				if(no_of_marks<1 || no_of_marks > 20)
				Console.WriteLine("Your number {0}, is not within the specified range of 1 to 20, please re-enter.", myInput);
				else
					break;
				}
				catch(FormatException)
				{
					Console.WriteLine("Invalid characters entered!");
				}
			}



Here I have used a method (wiki method programming for a full explanation of what a method is) that returns a boolean value that indicates the presence of any non numeric characters in my input.
	
		int no_of_marks = 0;			
			while ((no_of_marks < min_students) || (no_of_marks > max_students))
			{
				myInput = Console.ReadLine();
				if (ValidateInput(myInput))
					no_of_marks = int.Parse(myInput);
				if(!(no_of_marks < min_students || no_of_marks > max_students))
					Console.WriteLine("Your number {0}, is not within the specified range of 1 to 20, please re-enter.", myInput);
			}



Simply put a method is a named block of code that can accept a list of parameters and that can return a value or perform some operation. Since I have named my block of code I can call it from other parts of my application, this reduce the amount of code that I have to write.
	static bool ValidateInput(string input){
		foreach(char Char in input)
		{
			if(!char.IsDigit(Char))
				return false;
		}
		return true;
	
	}

Was This Post Helpful? 1
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7501
  • View blog
  • Posts: 15,544
  • Joined: 16-October 07

Re: Finished Assignment - Polishing help requested

Posted 07 June 2009 - 03:43 AM

Hmm... I was going to get into it, but using a method for that user entry was one of the things I was thinking when I mentioned cut and paste code. The goal of a method is to isolate logic, reduce code repetition, and have a possible bug in a single place, rather than copied all over the place.

If you're going to write a method, look at your current repetition. Here's an example:

int GetUserInt(int minValue, int maxValue) {
	int intValue;
	while(true) { // loop forever, until they get it right
		string myInput = Console.ReadLine();
		// try parse is a good idea, but a try catch is also good
		try {
			intValue = int.Parse(myInput);
			if ((intValue>=minValue) && (intValue<=maxValue)) { break; }
			Console.Write("Your number {0}", myInput);
			Console.Write(", is not within the specified range of {0} to {1}", minValue, maxValue);
		} catch {
			Console.Write("The value {0} is not an integer", myInput);
		}
		Console.WriteLine(", please re-enter.");
	}
	return intValue;
}

//...

Console.WriteLine("Please enter number of student marks, between 1 and 20: ");
no_of_marks = GetUserInt(1, 20);
Console.WriteLine("Thank you. There are {0} student marks to be entered.", no_of_marks);
Console.Write("\nMarks range from 0 to 99. ");
for (int i = 1; i <= no_of_marks; i++) {
	Console.WriteLine("\nPlease enter student mark, number {0} of {1}:\n", i, no_of_marks);
	mark = GetUserInt(0, 99);


Was This Post Helpful? 0
  • +
  • -

#5 skavamareen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-May 09

Re: Finished Assignment - Polishing help requested

Posted 07 June 2009 - 07:21 AM

baavgai and T3hC13h,

I'd like to thank you both for your responses - the 'method' aspect of my query is relevant to my current assignment (and no doubt everything else I will ever do), so I will take on board the advice you have given. Using the BASIC I tried learning (BBC BASIC), many years ago, am I right in thinking that a method is similar to a 'gosub'? I think I get the idea, it's just learning how to apply it now.

Using arrays is also something I need to get to grips with - I'll have to look into both. Being a new programmer - it's also quite difficult to take code snippets into context and splice them into my own coding, but I'm sure this is something I'll pick up.

What is the difference between try and catch, where would you use one and not the other?

Can you possibly point me towards a command list with syntax? I'm seeing all sorts of code/commands I've never seen before so it's difficult to know how to use them correctly.

Thanks again

S
Was This Post Helpful? 0
  • +
  • -

#6 skavamareen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-May 09

Re: Finished Assignment - Polishing help requested

Posted 07 June 2009 - 07:35 AM

Please ignore my previous question about command lists, I searched the MSDN site and found a code list: http://msdn.microsof...6bb(VS.85).aspx

S
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1