Calculating an average from input

I don't understand!!!! Please Help!

Page 1 of 1

14 Replies - 13515 Views - Last Post: 08 February 2009 - 05:33 PM Rate Topic: -----

#1 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Calculating an average from input

Posted 08 February 2009 - 05:56 AM

I am a beginner - just a few weeks into this. Am taking this online. I need someone to help me - please!!!!!
here is my problem. i have to enter two grades, each grade should be an integer between 0 and 100 inclusive. Calculate the average of the two grades. If the average is greater than or equal to 90, display "Grade is an A!". It goes down the line for "if average is _______, display " ______is a/n __!" I get the last part and I know how to figure an average...but i am not sure how to type it in code. He did an elseif example, but it wasn't with 2 numbers and didn't have to average anything. So I don't know how to write that code....is there anywhere to look to find it? Here is what I have so far.............
Module Module1

	Sub Main()
		Dim grade, sum, average As Double
		Dim numberofgrades As Integer = 2
		Console.WriteLine("Please enter a grade")
		grade = Convert.ToDouble(Console.ReadLine())
		sum += grade
		Console.WriteLine("Please enter a grade")
		grade = Convert.ToDouble(Console.ReadLine())
		sum += grade
		average = sum / numberofgrades
		If average >= 90 Then
			Console.WriteLine("Grade is an A!")
		ElseIf average >= 80 And <90 then
			Console.WriteLine("Grade is an B!")

		End If





	End Sub

End Module


I fiddled around till I got the dim, grade, sum, average as double and so forth. BUT am still very confused at how I would know how to write these programs. Am working out of visual basic.net programming from problem alalysis to program design. (Doke and Williams)

Is This A Good Question/Topic? 0
  • +

Replies To: Calculating an average from input

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 764
  • View blog
  • Posts: 5,095
  • Joined: 08-December 08

Re: Calculating an average from input

Posted 08 February 2009 - 12:02 PM

Try using this code. The comments will explain you which part of the code is doing a specific task.

'Contains the student grade.
		Dim Grade As Integer

		'Contains the sum  of grades that are entered.
		Dim SumOfGrades As Integer

		'Counter for the number of items.
		Dim NumberOfGrades As Integer

		'The average value of grades.
		Dim Average As Double

		'The letter grade value.
		Dim LetterGrade As Char

		'The current user input.
		Dim Input As String = ""

		'Repeat the loop until the input is End.
		Do Until Input = "End"
			'Ask the user to enter a grade.
			Console.WriteLine("Enter a grade:")
			Input = Console.ReadLine()

			'Verify the input
			If Input = "End" Then
				'If the input is End, compute the average grade.
				Average = SumOfGrades / NumberOfGrades
				Console.WriteLine("The average grade is {0}", Average.ToString())

				'Compute the letter grade based on the average.
				Select Case Average
					Case Is > 90
						LetterGrade = "A"
					Case 80 < Average < 90
						LetterGrade = "B"
					Case 70 < Average < 80
						LetterGrade = "C"
					Case 60 < Average < 70
						LetterGrade = "D"
					Case Else
						LetterGrade = "F"
				End Select

				'Display the letter grade.
				Console.WriteLine("Letter grade: {0}", LetterGrade.ToString())
				Console.ReadLine()

				Exit Sub
			Else
				'Compute the grade and total if the input is not End.
				Grade = Convert.ToInt32(Input)
				SumOfGrades += Grade
				NumberOfGrades += 1
			End If
		Loop


Was This Post Helpful? 0
  • +
  • -

#3 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: Calculating an average from input

Posted 08 February 2009 - 02:54 PM

Here is the exact instructions - then I will give you what I have - which I think is right. If anyone can help figure out how to do a flow chart for this mess....that would be helpful. AGAIN...no answers are wanted....just help.

1. Write a Visual Basic (VB) console application to solve the following problem...
Enter two grades. Each grade should be an integer between 0 and 100 inclusive.
Calculate the average of the two grades. If the average is greater than or equal to 90, display "Grade is an A!".
If the average is greater than or equal to 80 and less than 90, display "Grade is a B!". If the average is greater than or equal to 70 and less than 80, display "Grade is a C!". If the average is greater than or equal to 70, display "Grade is a D!". If the average is less than 60, display "Grade is an F!".
Regardless of the value calculated for the average, also display both of the two grades that were input and the average that was calculated.


2. Compile and execute your VB program. Here is mine.....is shows the grades but not the averages.
Module Module1

	Sub Main()
		Dim grade, sum, average As Double
		Dim numberofgrades As Integer = 2
		Console.WriteLine("Please enter a grade")
		grade = Convert.ToDouble(Console.ReadLine())
		sum += grade
		Console.WriteLine("Please enter a grade")
		grade = Convert.ToDouble(Console.ReadLine())
		sum += grade
		average = sum / numberofgrades
		If average >= 90 Then
			Console.WriteLine("Grade is an A!")
		ElseIf average >= 80 And average < 90 Then
			Console.WriteLine("Grade is an B!")
		ElseIf average >= 70 And average < 80 Then
			Console.WriteLine("Grade is a C!")
		ElseIf average >= 60 And average < 70 Then
			Console.WriteLine("Grade is a D!")
		ElseIf average < 60 Then
			Console.WriteLine("Grade is an F!")
		End If


	End Sub

End Module

Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 764
  • View blog
  • Posts: 5,095
  • Joined: 08-December 08

Re: Calculating an average from input

Posted 08 February 2009 - 03:50 PM

First of all you have a variable that is not needed - numberofgrades. If you know that the number of grades will always be 2, then just divide the sum of grades by two, when calculating the average.

When you try to find out what letter grade it is, I would recommend using the Select Case statement to find out if the average value meets specific criteria. In my opinion it makes the code a bit more organized.

Also, please note that your question refers to VB.NET. Please, post your future VB.NET questions in the VB.NET forum:

http://www.dreaminco...showforum67.htm

This post has been edited by Core: 08 February 2009 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: Calculating an average from input

Posted 08 February 2009 - 03:56 PM

View PostCore, on 8 Feb, 2009 - 02:50 PM, said:

First of all you have a variable that is not needed - numberofgrades. If you know that the number of grades will always be 2, then just divide the sum of grades by two, when calculating the average.

When you try to find out what letter grade it is, I would recommend using the Select Case statement to find out if the average value meets specific criteria. In my opinion it makes the code a bit more organized.

Also, please note that your question refers to VB.NET. Please, post your future VB.NET questions in the VB.NET forum:

http://www.dreaminco...showforum67.htm


Ok, here is what I have now. PROBLEM: I can't get the average to display.
Module Module1

	Sub Main()
		Dim grade As Integer
		Dim Sum As Integer
		Dim Average As Double
		Average = Sum / grade = 2
		Console.WriteLine("Please enter a grade")
		grade = Convert.ToDouble(Console.ReadLine())
		Console.WriteLine("Please enter a grade")
		grade = Convert.ToDouble(Console.ReadLine())
		Average = Sum / grade = 2
		Console.WriteLine(Average & " Average = ")
		If grade >= 90 Then
			Console.WriteLine("Grade is an A!")
		ElseIf grade >= 80 And grade < 90 Then
			Console.WriteLine("Grade is an B!")
		ElseIf grade >= 70 And grade < 80 Then
			Console.WriteLine("Grade is a C!")
		ElseIf grade >= 60 And grade < 70 Then
			Console.WriteLine("Grade is a D!")
		ElseIf grade < 60 Then
			Console.WriteLine("Grade is an F!")
		End If


	End Sub

End Module

Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 764
  • View blog
  • Posts: 5,095
  • Joined: 08-December 08

Re: Calculating an average from input

Posted 08 February 2009 - 03:58 PM

You can display it this way:

Console.WriteLine("The average grade: {0}", Average.ToString())


This post has been edited by Core: 08 February 2009 - 03:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: Calculating an average from input

Posted 08 February 2009 - 04:04 PM

also, I am not that far into the program to have even heard of what you said. Well actually he had us read to page 131 and that "Select Case" starts on page 132.....so don't really know that yet.

here is what i have now. And it keeps reading "0" for the average. How do i get it to actually calculate the average and display it?
Module Module1

	Sub Main()
		Dim grade As Integer
		Dim Sum As Integer
		Dim Average As Double
		Average = Sum / grade = 2
		Console.WriteLine("Please enter a grade")
		grade = Convert.ToDouble(Console.ReadLine())
		Console.WriteLine("Please enter a grade")
		grade = Convert.ToDouble(Console.ReadLine())
		Average = Sum / grade = 2
		Console.WriteLine("The average grade: {0}", Average.ToString())
		If grade >= 90 Then
			Console.WriteLine("Grade is an A!")
		ElseIf grade >= 80 And grade < 90 Then
			Console.WriteLine("Grade is an B!")
		ElseIf grade >= 70 And grade < 80 Then
			Console.WriteLine("Grade is a C!")
		ElseIf grade >= 60 And grade < 70 Then
			Console.WriteLine("Grade is a D!")
		ElseIf grade < 60 Then
			Console.WriteLine("Grade is an F!")
		End If


	End Sub

End Module



and where do i go to find out how to draw a flow chart for this?
Was This Post Helpful? 0
  • +
  • -

#8 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 764
  • View blog
  • Posts: 5,095
  • Joined: 08-December 08

Re: Calculating an average from input

Posted 08 February 2009 - 04:04 PM

The Select Case is not as hard as it seems. It looks like this:

Select Case Average
	Case Is > 90
	LetterGrade = "A"
	Case 80 < Average < 90
	LetterGrade = "B"
	Case 70 < Average < 80
	LetterGrade = "C"
	Case 60 < Average < 70
	LetterGrade = "D"
	Case Else
	LetterGrade = "F"
End Select



It makes the code a bit easier to read and saves the time needed to write the If statements. I don't think it would be a big issue if you will use Select Case instead of If.

For a detailed description on Select Case, take a look at this MSDN article:

http://msdn.microsof...14y(VS.80).aspx

When you calculate the average, you incorrectly use the statement. You don't have to divide the average by the number of grades and then assign 2 to the variable (which is incorrect anyway). You use the average calculation twice in your code, but you need it only once, after the grades were entered. Also, you need to calculate the sum, so for each input you should increment it this way:

Console.WriteLine("Please enter a grade")
grade = Convert.ToDouble(Console.ReadLine())
Sum += Convert.ToDouble(grade)



The average will be calculated this way:

Average = Sum / 2


This post has been edited by Core: 08 February 2009 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

#9 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: Calculating an average from input

Posted 08 February 2009 - 04:24 PM

I THINK I GOT IT!!!!!!!!!!!!!!!! I will attempt to do the Case Select - but he wants it this way because this was an "elseif" lab. But I agree - i like the other way much better!!!!!!!

Now.....about the flow chart. Do you know anything about that - that would help me figure that one out?

What I do know is...
oval - start / end
diamond is - decision
rectangle is - one or more statements
connectors - begin and end logical flow
flow lines or arrows - depict the control flow.

I know that I would start off with something like
oval = enter two grades
diamond = integer? (not sure that one is necessary)
rectangle = average >=90
yes/no = yes rectangle = display grade A (not sure what to put if it is no. Maybe keep putting diamonds asking the average score and yes off to the side with the grade listed.?
Was This Post Helpful? 0
  • +
  • -

#10 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 764
  • View blog
  • Posts: 5,095
  • Joined: 08-December 08

Re: Calculating an average from input

Posted 08 February 2009 - 04:29 PM

Do you have to do a flow chart by hand or make your application draw it? If it is by hand, then it's pretty easy as you already outlined the main elements and concepts. You will have to either draw it on the paper or use an UML tool. If you need to do it by code, you will either have to draw it in a WinForms project or use a third-party control.
Was This Post Helpful? 0
  • +
  • -

#11 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: Calculating an average from input

Posted 08 February 2009 - 04:41 PM

I am not sure.
It reads ...
Become familiar with the If-Then and If-Then-Else conditional logic constructs.
Be able to draw a flowchart of a logical solution involving the use of an If-Then and and If-Then-Else construct.
Be able to use the If-Then and If-Then-Else constructs in a Visual Basic Program.

So - all it says is to "draw".

something isn't right...........i just used the grades 90 and 40 gave me the average of 65 - then assigned "grade is an f"

that should be a "D"

AAAGGGHHHHHHHHH
Was This Post Helpful? 0
  • +
  • -

#12 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 764
  • View blog
  • Posts: 5,095
  • Joined: 08-December 08

Re: Calculating an average from input

Posted 08 February 2009 - 04:45 PM

Quote

So - all it says is to "draw".


Most likely you will have to draw it manually.

Quote

something isn't right...........i just used the grades 90 and 40 gave me the average of 65 - then assigned "grade is an f"

that should be a "D"


Check your If or Select Case statements and make sure that they verify the correct values before assigning a letter grade.
Was This Post Helpful? 0
  • +
  • -

#13 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Calculating an average from input

Posted 08 February 2009 - 04:49 PM

Please use :code: tags when posting your code.

Modified title to be more descriptive of the problem.
Was This Post Helpful? 0
  • +
  • -

#14 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: Calculating an average from input

Posted 08 February 2009 - 05:28 PM

i don't know what that means. Is this right? Are you able to help with my problem at all? 

Was This Post Helpful? 0
  • +
  • -

#15 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 764
  • View blog
  • Posts: 5,095
  • Joined: 08-December 08

Re: Calculating an average from input

Posted 08 February 2009 - 05:33 PM

What is the question? You don't know how to draw a flowchart?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1