Visual Basics Exercie

Post your questions regarding programming in Visual Basic 6 in here.
2 posts Page 1 of 1
Contributors
User avatar
lansky17
Just Registered
Just Registered
Posts: 1
Joined: Sat May 22, 2010 10:25 am

Visual Basics Exercie
lansky17
Hi anybody,

I have a VB exercie, can anybody help me???


A file of unknown length contains the temperature values of a chemical reaction. Such
values can have decimal digits, they appear on many rows and they are separated by one
or more spaces. Temperatures are measured every two minutes and the first measurement
is taken when the reaction starts. Therefore they refer to minutes 0, 2, 4, 6, …

Write a program that asks the user for the name of this file, asks the user for the name of
an output file to be created, reads all the values from the first file and writes to the second
file the temperature values of every minute, including odd minutes (1, 3, 5…). As these
values are not present in the input file, the program must estimate them as the average
between the previous and the following read (even placed) value. Each output value must
appear on a different row. See next example for reference.

Example:

Content of input file:
21.2 37.4 43.4 40.2 46.12

50.33 34.6 32.5

31.2 33.2 33.4 40.23
...etc...

Resulting output file:

21.2 -> first read value
29.3 -> average between 21.2 (previous value) and 37.4 (following value)
37.4 -> second read value

40.4 -> average between 37.4 (previous value) and 43. 4 (following value)
43.4 -> third value
...etc...
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Visual Basics Exercie
mandai
How about:
Code: Select all
        Dim lines() As String = File.ReadAllLines("input.txt")

        Dim outs As List(Of String) = New List(Of String)
        For i As Integer = 0 To lines.Length - 1
            Dim values As String() = lines(i).Split(" "c)
            For i2 As Integer = 0 To values.Length - 1
                If values(i2).Length > 0 Then
                    outs.Add(values(i2))
                End If
            Next

        Next

        For i As Integer = 0 To outs.Count - 1
            If i > 0 And i Mod 2 Then
                Dim last As Decimal = Decimal.Parse(outs(i - 1))
                Dim cur As Decimal = Decimal.Parse(outs(i))
                File.AppendAllText("output.txt", (last + cur / 2) & vbCrLf) 'average?
            End If

            File.AppendAllText("output.txt", outs(i) & vbCrLf)
        Next
(over PM messaging earlier)
lansky17 wrote:

Thanks u,

But here I study very easy structure, can u reduce it to an easy version???
mandai wrote:
I think thats the most reduced code I could come up based on what you asked for.

I can explain it though

1. Read the input file into an array (split by lines)
2. Make a list which we will use for storing the values (later)

3. For each line in our input array, split it by the space char, then for each space seperated value see if its length is > 0, if it is then add it to our list

4. For each gathered value in our list write it to a file (but before the write if it is not the first line and if the value index is an even number then process the last/current temperatures and find the average, and write that).
2 posts Page 1 of 1
Return to “General coding help”