0

Please don't laugh at my newbieness, this is an 11th grade CS assignment. Constructive feedback would be higly appreciated

Ok, so I have an assignement due for next week. It's fairly straight forward.

Task 1
A school keeps records of the weights of each pupil. The weight, in kilograms, of each pupil is recorded on the first day of term. Input and store the weights and names recorded for a class of 30 pupils you must store the weights in a one-dimensional array and the names in another one-dimensional array. All the weights must be validated on entry and any invalid weights rejected. You must decide your own validation rules. You may assume that the pupils’ names are unique. Out put the names and weights of the pupils in the class.
Task 2
The weights, in kilograms, of each pupil are recorded again on the last day of term. Calculate and store the difference in weights for each pupil.
Task 3
For those pupils who have a difference in weight of more than 2.5 kilograms, output, with a suitable message, the pupil’s name, the difference in weight and whether this is a rise of a fall.

My pathetic attempt. (Please note that the syntax doesn't matter that much, this is pre-highschool stuff, some elements of my code are slightly VB-like)

Task 1

dim Studentnames[1:30] as string
dim Studentweights[1:30] as integer

For count=1 to 30
Input name
name = studentname[count]
Input weight
  If weight>100 OR weight<20 
   THEN PRINT "ERROR: Weight not within range"
Else weight1 = studentweight[count]
Output studentweight[count], studentnames[count]
next
endfor

Task 2

dim weightdiff[1:30] as integer
dim diff as integer= 0

For count= 1 to 30
Input studentweights[count], finalweight
diff= studentweights[count] - finalweight
diff= weightdiff[count]
next
end for

Task 3

dim loss as integer= 0
dim gain as integer=0
For count = 1 to 30
Input studentname[count], weightdiff[count]
  If weightdiff[count] >2.5 
    THEN gain= weightdiff[count]
       PRINT studentname[count], "has lost", loss, "kilograms of weight"
Else if weightdiff[count] <2.5
    THEN loss= 0 - weightdiff[count]
      PRINT studentname[count], "has gained", gain, "kilograms of weight" 
next
endfor

Since a negative weightdiff means a gain in weight, I subtracted the weightdiff from 0 (to make it positive) and then stored it in a variable called gain.

Edited by sharjeelmumtaz

5
Contributors
5
Replies
33
Views
2 Years
Discussion Span
Last Post by Reverend Jim
0

Hi

Below is some code that will satisfy Task 1 (although you will need to modify to suit your needs). This is in VB.NET as you have some VB like syntax so figured it may be easier to understand. Note, this is not the final solution as it is prone to errors in certain places.

        'Arrays in VB.NET are zero based, your language may treat them differently
        Dim studentNames(29) As String
        Dim studentWeights(29) As Integer

        'Create a loop for 30 iterations
        For studentIndex As Integer = 0 To 29

            Dim validInput As Boolean = False
            Dim studentName As String = String.Empty
            Dim studentWeight As Integer = 0

            'Keep looping until input is validated for this single iteration of the
            'outer loop
            While validInput = False

                validInput = True   'Signal all good unless told otherwise below

                'Ask for name and weight: NOTE that weight will cause an exception
                'if a string or empty string is entered so you NEED to cater for this
                studentName = InputBox("Enter your name")
                studentWeight = InputBox("Enter your weight")

                'Basic validation
                If String.IsNullOrWhiteSpace(studentName) Then
                    MessageBox.Show("You need to enter your name")
                    validInput = False
                End If

                If Not IsNumeric(studentWeight) Then
                    MessageBox.Show("You need to enter a valid weight")
                    validInput = False
                Else
                    If studentWeight < 0 OrElse studentWeight > 100 Then
                        MessageBox.Show("You need to enter a valid weight")
                        validInput = False
                    End If
                End If

            End While

            'Add valid inputs to arrays
            studentNames(studentIndex) = studentName
            studentWeights(studentIndex) = studentWeight

        Next

        'Print out values
        For studentIndex As Integer = 0 To 29
            Console.WriteLine(String.Format("Name: {0}, Weight {1}",
                                            studentNames(studentIndex), studentWeights(studentIndex).ToString))
        Next

Your task now, is to take this and do Task 2 and 3 yourself. The above contains enough code to demonstrate how to do those tasks.

If you get stuck, post the code that you have tried. If you haven't already you can download a free version of Visual Studio (which includes VB.NET) to test this code out and apply it to your other tasks.

HTH

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.