:?: I got from the book: Write a piece of visual basic code to process the responses of twenty students who were asked to rate, on a scale from 1 to 10, the quality of the food in the cafeteria. A response of 1 denotes ‘dreadful’ and 10 denotes ‘excellent’. Allow the user input to be entered using a ComboBox. Place the responses in an array and determine the frequency of each rating. Display the frequencies in a histogram in a multiline TextBox. as it shown in the figure below
I started with:

Dim Array As Integer () = New Integer {1,2,3,4,5,6,7,8,9,10}
Dim output As String = “ “

‘ generate random numbers from 1 to 10 to simulate survey results
For Array = 1 To responses.getUpperBound (o)
Responses (Array) = RandomNumber (10)
Next

' response frequency array (indices 0 through 10)
Dim frequency As Integer () = New Integer (10) {}

'count frequencies
For array = 1 To responses.GetUpperBound (0)
Frequency (responses (array) += 1
Next

‘output
ListBox1.Items.Add("Rating" & ControlChars.Tab & "Frequency" & ControlChars.NewLine)

‘clear
ListBox1.Items.Clear()


i dont know how to solve this code i try but ... :confused:

im usin Mircosoft Visual Studio 2005

Help :(

Recommended Answers

All 20 Replies

to do this
>generate random numbers from 1 to 10 to simulate survey results
>use this code

dim array(10) as integer
dim counter as integer
for counter = 1 to 10
array(counter) = int(rnd * 10) + 1
Next counter

>to display the frequencies as it is on the screenshot
>do this

dim frequencies(10) as string
dim frequencyEntered as integer
dim counter as integer
dim secondcounter as integer
dim phrase as string

counter = 0
        While counter <> 10
            frequencies(counter) = frequencies(counter) & counter & "." & vbTab
            For secondcounter = 1 To array(counter)
                frequencies(counter) = frequencies(counter) & "*"
            Next secondcounter
            counter = counter + 1
        End While
        For counter = 1 To 10
            phrase = phrase & frequencies(counter) & vbCrLf
        Next
        TextBox1.Text = phrase

This is the overall code

Dim frequencies(10) As String
    Dim frequencyEntered As Integer
    Dim counter As Integer
    Dim secondcounter As Integer
    Dim phrase As String
    Dim array(10) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Randomize()
        For counter = 1 To 10
            array(counter) = Int(Rnd() * 10) + 1
        Next counter
        counter = 0
        While counter <> 10
            frequencies(counter) = frequencies(counter) & counter & "." & vbTab
            For secondcounter = 1 To array(counter)
                frequencies(counter) = frequencies(counter) & "*"
            Next secondcounter
            counter = counter + 1
        End While
        For counter = 1 To 10
            phrase = phrase & frequencies(counter) & vbCrLf
        Next
        TextBox1.Text = phrase
    End Sub

thanx dear Toko ill try now & back again :)

hi again

i tried but i got many errorzzz :( i attach my file can u plz check t 4 me :)

First of all - When submitting code please use comments inside the code so we know what you are doing or thinking.
1. Your code in TextBox1_TextChanged creates an infinite loop. You are changing the text in it and calls the TextBox1_TextChanged You are changing the text in it and calls the TextBox1_TextChanged You are changing the text in it and calls the TextBox1_TextChanged. See what I mean? I deleted the TextBox1_TextChanged.
2. To have access to variables in more that one subroutine declare it outside the subroutine.
3. A While loop is generally used when you don't know when a loop will quit. In this case we know it is 10 so just use a For loop.

Public Class Form1
    ' varioables that need to be referenced from more than one sub
    ' has to be declared outside the subs
    Private ary(10) As Integer
    Private counter As Integer
    Private frequencies(10) As String
    Private phrase As String = ""
    Private secondcounter As Integer
    Private rand As New Random

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim array(10) As Integer
        Dim counter As Integer

        For counter = 1 To 10
            array(counter) = Int(Rnd() * 10) + 1
        Next counter
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' DO NOT USE RANDOMIZE EVERYTIME YOU NEED A RANDOM NUMBER!!!!!!! Yes this is shouting! As a mater of fact don't use
        ' Randomize unless you need to start your count at the same
        ' place each time. Then use Randomize(seed number). 
        ' seed number being any long number.
        ' Instead use the net random number generator, which is defined outside the sub
        'Randomize()

        ' variables have not been declared

        For counter = 1 To 10
            ' can not use the word Array as an array also need to declare it
            'Array(counter) = Int(Rnd() * 10) + 1
            ary(counter) = rand.Next(1, 10)
        Next counter

        counter = 0
        'While (counter <> 10)
        For counter = 0 To 10
            frequencies(counter) = frequencies(counter) & counter & "." & vbTab
            For secondcounter = 1 To ary(counter)
                frequencies(counter) = frequencies(counter) & "*"
            Next secondcounter
        Next counter
        'End While

        For counter = 1 To 10
            phrase = phrase & frequencies(counter) & vbCrLf
        Next
        TextBox1.Text = phrase

    End Sub

End Class

There doesn't seem to be an edit so do this.
Delete this:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim array(10) As Integer
        Dim counter As Integer

        For counter = 1 To 10
            array(counter) = Int(Rnd() * 10) + 1
        Next counter
    End Sub

Apparently edit goes away after a certain period of time but anyway. You do not need this code as you have it in your button click that will change anything done here.

Hi waynespangler

1st sorry 4 forgettn to put comment for the code coz i did it quickly ;)

i used ur code & it seem its work correct but i have Q do i need use static to calculate the number of 20 student for each time they rate !!!? :?:

1. I guess i forgot to include that the function randomize() initializes the random number generator and should only be used once throughout the program. The perfect place for this i think is in the form.load event because it only happens onces and at the beginning of the program.
2. Instead of randomizing the input of the rating from the user or using a combo box to record the ratings, i suggest using the inputbox() function. This will easily be able to record the users rating more easier then the combo box way.

dim rating(9) as integer
'even though there are 10 frequencies the 0 plus the 9 makes 10. This is explaned more thoroughly in 4.
dim counter as integer
for counter = 0 to 9
rating(counter) = inputbox("Enter rating number " & counter, "Rating")
next

3. What do u mean as "use static to calculate the number of 20 student for each time they rate"?
4. Arrays always start from 0. thus, to use them correctly always declare one less array then u need to account for 0. eg. if i need to store student's name and last name i would declare.

dim studentInfo(1) as string
' studentInfo(0) would equal the name
' studentinfo(1) would equal the last name
dim studentInfo(2) as string
' studentInfo(1) would equal the name
' studentinfo(2) would equal the last name
' if u do it like this, it would still compile but it would be using arrays wrong and also be wasting memory space

hi again

the program should work like this there were 20 student each student rate and it should store ex. 1st student chose 5, 2nd student chose 10 ... etc till the last student (20) so the program should show how many student chose 1, or 2 or ..... till 10 and show as star (*)

my instructor told me to used static (static count as integer)

i really gt confuse with this :S

A static variable is used when you leave a subroutine and return to it the variable hasn't changed. If it were 5 then the next time you called the routine it would still be 5. It is not a public variable as you can't change it after you leave the routine. I don't see any place in your code you can use a static variable.

This is a copy of my assignment

[IMG]http://up1.m5zn.com/photo/2008/12/21/01/q4btgp4nz.jpg/jpg[/IMG]

I just start with this

Dim intChoices As Integer   ‘create array of 10 consecutive
Dim intResponses As Integer   ‘store the number of responses
Dim intResponseCounter As Integer   ‘how many responses have been input
.

.
.
So what I should do for the rest!!!?

Okay, i understand what you are trying to do now.
First of all, if your instructor hasn't taught arrays yet, use normal variables. For example

dim studentRating1 as integer
dim studentRating2 as integer
'and so on

now, to record the ratings do this:

studentRating[whatever number] = inputbox("enter the first student's rating.")

You wrote this:

Dim intChoices As Integer   ‘create array of 10 consecutive
Dim intResponses As Integer   ‘store the number of responses
Dim intResponseCounter As Integer   ‘how many responses have been input

I'll tell you what is wrong with this code:
1. the way you declared the array is wrong; to properly decalre an array do this:

Dim intChoices(19) As Integer

its 19 because its an array and always starts with 0; i gave a little tutorial in my last post about arrays.

2. As i undrestand, the way you approached this question is wrong. The question asks to get the rating of 1 to 10 from 20 students nad then organize them with "*".
To do this you would need to declare 20 variables or array with the index of 19

dim students(19) as integer

then ask to enter the ratings with

inputbox("enter the rating")

and then after that you would just need to arrange the inputs by "*". The code to do that was provided by me and waynespangler in earlier posts

a copy of my assignment

[img]http://www.up-00.com/bzfiles/rdO18636.jpg[/img]

i just know the basic n array that y i gt confuse with this assignment

i wrote ths code coz n assignment told us to do it 1st create array for rating 2nd create array to store the responses 3rd count the number of response

Dim intChoices As Integer   ‘create array of 10 consecutiveDim intResponses As Integer   ‘store the number of responsesDim intResponseCounter As Integer   ‘how many responses have been input

so what code should i used to gt the final ireally gt confuuuse
:confused:

use asterisks to indicate how many students surveyed submitted the corresponding rating !!!

Ok, for some reason i can't view that image.
Now for the programing part, i understand the 1st, and the 3rd but the 2nd one consuses me. Are you sure it doesn't say store responses. Because if it doesn't then the 1st and the 2nd seem kind of the same.

i attach full document 4 my assignment it is in page 380 - 17.13 (cafeteria survey application)

k, this is the whole program

Public Class Form1
    Dim studentRate(20) As Integer
    Dim counter As Integer
    Dim starCounter As Integer
    Dim frequencies(20) As String
    Dim phrase As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        counter = 0
        phrase = "Rating" & vbTab & "Frequencies" & vbCrLf
        TextBox1.Text = phrase
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ComboBox1.Text = "" Then
            MsgBox("select rating...")
        Else
            counter += 1
            If counter > 20 Then
                MsgBox("all students have rated the food...")
            Else
                'register student rating
                studentRate(counter) = ComboBox1.Text
                phrase &= counter & vbTab
                'write number of stars
                For starCounter = 1 To studentRate(counter)
                    phrase &= "*"
                Next
                phrase &= vbCrLf
                TextBox1.Text = phrase
                If counter > 20 Then
                    MsgBox("all students have rated the food...")
                End If
            End If
        End If
    End Sub
End Class

its work :icon_cheesygrin:

but it missing somethin .. after i click OK for the msgbox showd that all student have rated .. i want to show another message box show how many student rate for this #, for example after all 20 student rated i want to show mesbox show that there were 2 student rated for number 4, there were 5 student ratef 4 number 10 .. etc

so, do i need to use another array to show this!!?

just doin this off the top of my head, i would write:

dim ratings(10) as integer
dim counter as integer
dim secondcounter as integer
'also frequecies would be needed from the code i already posted
for counter = 1 to 10
for secondcounter = 1 to 20
if counter = frequecies(secondcounter) then
ratings(counter) += 1
end if
next
next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.