# Calculating an average from input

Page 1 of 1

## 14 Replies - 13515 Views - Last Post: 08 February 2009 - 05:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=85363&amp;s=2336c38c2da63e93209178f6bb51e0b5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kaybear912

Reputation: 0
• 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
If average >= 90 Then
ElseIf average >= 80 And <90 then

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

• using System.Linq;

Reputation: 764
• 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.

'Contains the sum  of grades that are entered.

'Counter for the number of items.

Dim Average As Double

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

'Repeat the loop until the input is End.
Do Until Input = "End"

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

'Compute the letter grade based on the average.
Select Case Average
Case Is > 90
Case 80 < Average < 90
Case 70 < Average < 80
Case 60 < Average < 70
Case Else
End Select

Exit Sub
Else
'Compute the grade and total if the input is not End.
End If
Loop

```

### #3 kaybear912

Reputation: 0
• 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
If average >= 90 Then
ElseIf average >= 80 And average < 90 Then
ElseIf average >= 70 And average < 80 Then
ElseIf average >= 60 And average < 70 Then
ElseIf average < 60 Then
End If

End Sub

End Module
```

### #4 Core

• using System.Linq;

Reputation: 764
• 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.

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

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

### #5 kaybear912

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

## Re: Calculating an average from input

Posted 08 February 2009 - 03:56 PM

Core, 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.

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 Sum As Integer
Dim Average As Double
Average = Sum / grade = 2
Average = Sum / grade = 2
Console.WriteLine(Average & " Average = ")
End If

End Sub

End Module
```

### #6 Core

• using System.Linq;

Reputation: 764
• 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

### #7 kaybear912

Reputation: 0
• 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 Sum As Integer
Dim Average As Double
Average = Sum / grade = 2
Average = Sum / grade = 2
End If

End Sub

End Module
```

and where do i go to find out how to draw a flow chart for this?

### #8 Core

• using System.Linq;

Reputation: 764
• 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
Case 80 < Average < 90
Case 70 < Average < 80
Case 60 < Average < 70
Case Else
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")

```

The average will be calculated this way:

```Average = Sum / 2

```

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

### #9 kaybear912

Reputation: 0
• 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
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.?

### #10 Core

• using System.Linq;

Reputation: 764
• 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.

### #11 kaybear912

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

## Re: Calculating an average from input

Posted 08 February 2009 - 04:41 PM

I am not sure.
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

### #12 Core

• using System.Linq;

Reputation: 764
• 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.

### #13 Jayman

• Student of Life

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

## Re: Calculating an average from input

Posted 08 February 2009 - 04:49 PM

Modified title to be more descriptive of the problem.

### #14 kaybear912

Reputation: 0
• 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?
```

### #15 Core

• using System.Linq;

Reputation: 764
• 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?