|
Hello there,
sorry this is my first post.
I really urgently need someones help. Ive got this question for some work and I have no idea how to do it. The teacher hasn't shown us and I cant find anything to help me on the net. I need to complete this question as not doing it will mean I fail my course. PLease could somebody help me out. I Would be so appreciative!!
Here is the question -
This problem concerns some simple array handling.
Problem specification
Given an array of integers, you have to perform some integer calculations, and display some information concerning their number, their sum and average values, their minimum and maximum values, and the number of even and odd integers.
Method
Create an empty integer array that will hold up to 20 integers. Then, repeatedly read integer values from the keyboard until the user types zero. Afterwards, loop through the array and display the number of integers read (excluding the terminating zero), their sum (an integer), their average (a double), the minimum and maximum values encountered
(both integers), and finally the number of even and odd integers read (these should be declared as integers too).
Always exclude the terminating zero from your calculations.
Error conditions: If you encounter a negative number (e.g. -151), display the message
Negative number: -151
Don't take it into account in your calculations.
If you encounter a number greater than 100 (e.g. 199), display the message Value exceeds 100: 199 and exit immediately.
If there are no valid numbers in the array to process, display the message No numbers to process! and exit immediately.
To me it is really complicated and I have no idea where to start! Im completely stumped on how to do the calculations and incorporate them into the array! I have had a try and ive managed to build the array, but, it is rubbish and doesn't work very well.
I know this is asking a lot of someone but I really have no idea what to do. I've tried google and everything, searched the help files etc.
Please, please, please help.
John
|
|
|
|
|
new_rez wrote: The teacher hasn't shown us
This means one of three things.
1 - the teacher HAS shown you
2 - the teacher expects you to be able to find the info online or in your texts
3 - you have a bad teacher
I recommend asking some fellow students, if they agree on 3, complain to the school.
new_rez wrote: To me it is really complicated and I have no idea where to start!
I'm afraid that it's sufficiently trivial that I really don't see how I can offer any help without doing it all. I also am certain that the web abounds with information on how to create arrays, on how to read input from the keyboard, and on how to iterate over an array.
new_rez wrote: Im completely stumped on how to do the calculations and incorporate them into the array!
You don't incorperate anything in the array, you step over the array and you render the calculations to the screen.
new_rez wrote: I have had a try and ive managed to build the array, but, it is rubbish and doesn't work very well.
We love to help people, but we don't like doing people's homework verbatim. How about if you post your code, define 'doesn't work very well', and we can offer some help from there ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Sorry for my bad post.
Here is my code. The trouble I am having is working out how to find out the amount of odd/even numbers in the array and finding the maximum and the minimum amounts in the array.
'global variables
Dim intNumbers(19) As integer 'array with 10 elements
Dim intCount As Integer 'control the array
Private Sub txtNumbers_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumbers.KeyPress
'if not enter - do nothing
If Asc(e.KeyChar) <> 13 Then
Exit Sub
End If
'add one to count
intCount = intCount + 1
'add number entered to the correct array index
intNumbers(intCount) = CDbl(txtNumbers.Text)
txtNumbers.Clear()
txtNumbers.Focus()
'when ten values have been entered - stop input
If intCount = 9 Then
MessageBox.Show("Array full. Please choose next option", "")
lblNumbers.Visible = False
txtNumbers.Visible = False
lstNumbers.Visible = True
btnAdd.Visible = True
btnDisplay.Visible = True
btnStats.Visible = True
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'set intcount at -1
intCount = -1
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'add array elements to listbox
For intCount = 1 To 10
lstNumbers.Items.Add(intNumbers(intCount - 1))
Next
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
'display the element that the user chooses
intCount = InputBox("Please enter position of number", "", "", 240, 210)
MessageBox.Show("Number at postion " & intCount & _
" is " & intNumbers(intCount - 1), "")
End Sub
Private Sub btnStats_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStats.Click
'declare the total and average of the numbers entered
'variables
Dim dblTotal As Double
Dim dblAverage As Double
'total
For intCount = 0 To 9
dblTotal = dblTotal + intNumbers(intCount)
Next
'average
dblAverage = dblTotal / 10
MessageBox.Show("Your totals are:" & vbCrLf & _
"Total: " & Format(dblTotal, "0.00") & vbCrLf & _
"Average: " & Format(dblAverage, "0.00"))
End Sub
End Class
|
|
|
|
|
The main thing is that once the numbers are stored in the array I don't know how to use them to do the calculations. I also don't know how to check through and label the negative numbers.
|
|
|
|
|
Error conditions: If you encounter a negative number (e.g. -151), display the message
Negative number: -151
Don't take it into account in your calculations.
If you encounter a number greater than 100 (e.g. 199), display the message Value exceeds 100: 199 and exit immediately.
If there are no valid numbers in the array to process, display the message No numbers to process! and exit immediately.
OK, basically, what you need to do, is do what you're doing now ( adding the numbers to get an average). But, you need to use a counter to remember how many numbers you added. If you find a negative number, don't add it, don't increment the counter, and display a message that you found such a number. Again, on the console this would be trivial. As it stands, you need to build it into your GUI, so you probably want to use a string builder to build the end result message with all the info you want to show, and then assign it to a labgl. Use Environment.NewLine to put newlines in your string.
If you find a number greater than 100, then put your string into the display message and break out of your processing. You may need a bool to tell you if you still need to calculate the average or not.
If the number of valid numbers found at the end of processing is 0, then you can show the 'no valid numbers found' message instead of an average.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I will give incorporating those steps, in code, into my code. That really does help me so much. I have a tendency to confuse myself easily and panic and then not know what im doing!
I did use an if statement for finding the Odd and Even amount of numbers, but, it wasn't working correctly. Every time Odd would be 11 and even would be 10...even if i had only entered 5 numbers. I think i've figured out the max and min thing. It shouldn't be really hard.
Any suggestions with the odd/even number amount?
Thank you
John
|
|
|
|
|
new_rez wrote: That really does help me so much. I have a tendency to confuse myself easily and panic and then not know what im doing!
Yeah, the hard part is often working out exactly the steps you need to take.
new_rez wrote: Any suggestions with the odd/even number amount?
I didn't spot that question ? The mod operator will do that, I think in VB it's actually Mod (it's % in C# or C++ ).
So, set a variable called oddnumbercount. Then, on the way through add one to it if yourvalue Mod 2 > 0. Then, subtract this from your total for the number of even numbers.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
First question - Why are you writing this as a Windows Forms application? At your level (and from reading the assignment) this should be a console application.
In a console application you can read from the keyboard more easily
Dim line As String
line = Console.ReadLine()
|
|
|
|
|
This is how we have been taught to write programs. I have no idea about console applications. The teacher has never mentioned it.
|
|
|
|
|
new_rez wrote: This is how we have been taught to write programs. I have no idea about console applications. The teacher has never mentioned it.
Really? If that is the case the state of your course is terrible. You should be writing simple console applications to learn about simple stuff like arrays. Learning about an event driven paradigm first is a bit crazy because you lose the sense of the flow of control of your program becuase you are placing that in the hands of the user.
|
|
|
|
|
Yeah, I despair at the people who self teach in this manner ( I've had people told me 'I wrote hello world, I don't need to write console apps', his posted sample code told a different story ), but for a course to teach like this is inexcusable.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
OK, your teacher IS an idiot. You don't need a GUI for this app, and having to write GUI code will just distract you from the exercise at hand.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Like Colin said : why use forms ?
new_rez wrote: InputBox
Don't do this, it's VB6 rubbish. Don't use forms at all, but when you do, don't use stuff hanging over from VB6. The easy way to avoid it - never use the visual basic namespace.
new_rez wrote: Dim intNumbers(19) As integer 'array with 10 elements
This array plainly has 19 elements, not 10.
new_rez wrote: For intCount = 1 To 10
lstNumbers.Items.Add(intNumbers(intCount - 1))
Next
why not start your loop at 0, instead of doing -1 every time ?
new_rez wrote: Dim dblTotal As Double
Dim dblAverage As Double
'total
For intCount = 0 To 9
dblTotal = dblTotal + intNumbers(intCount)
Next
'average
dblAverage = dblTotal / 10
You're on the right track, here. I need to post this, so I can refer back to the question, it's the negative values you have trouble with ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
If it's so urgent, why are you not replying ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Christian Graus wrote: If it's so urgent, why are you not replying ?
Good question!
|
|
|
|
|
new_rez wrote: I have had a try and ive managed to build the array, but, it is rubbish and doesn't work very well.
Then show us your code so far, and we can guide you on how to continue. But, as Christian said it is a very trivial problem and to explain it to you would be to do it for you. You wouldn't learn anything if we did that. It might be great for my job security and salary negotiations, but I'd rather you were able to do your job properly.
|
|
|
|
|
how to remove a string like '............344' from the text
here is the sample text:
'1. visual basic.net ............344'
so the result would be like this:
'1. visual basic.net'
i am using regular expression in vb.net
this is the sample code
sText = Regex.Replace(sText, " (\\.+)\\d+", "");
thanx
|
|
|
|
|
That looks fine to me. Did you test it in Expresso ? You don't actually ask a question, I presume there's a problem with this code ? What is the problem ?
If the string is always exactly the same, just use string.Replace.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
|
It took a week for you to say 'it doesn't work' ? Did you download Expresso ? What does it do ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi,
I just can't access the information in the AppSettings part of my appConfig file.
I have used the following statements -
1/ strKeys = ConfigurationManager.AppSettings.AllKeys
and strKeys.Count = 0
2/ ConfigurationManager.AppSettings.Count
this equals 0 too
3/ ConfigurationManager.AppSettings("theName")
this returns 'nothing'
The appConfig file has been created through the Settings of the IDE and compiles just fine.
Please can anyone help. I would be very grateful.
Many thanks,
Polly Anna
|
|
|
|
|
Without seeing your App.Config file (make sure you enable the "Ignore HTML tags in this message" checkbox before you post it!), it's impossible to tell you why it failed. The App.Config is nothing but an XML text file that is NOT compiled into your app. Therefore, you won't get any error message if it's not formatting properly.
Dave Kreskowiak
Microsoft MVP
Visual Developer - Visual Basic 2006, 2007
|
|
|
|
|
When you make changes to your app.config file you need to build your app and then your exename.exe.config file gets created.
So I am guessing that either you changed your app.config file and didn't recompile. Or you changed your exename.exe.config file and then built your app and it got wiped out.
Note you don't get an app.config file by default. If you add one to the project you still have to add the appSetting section.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
Hope that helps.
Ben
|
|
|
|
|
Hi Ben, Hi Dave,
Thank you for your responses.
I have used the VS2005 Express Edition IDE, Project > Properties > Settings to specify the Application Settings part of the config file.
I have put a copy of the original appconfig file below.
After what you said about what my config file should look like, I removed the following enclosing tags
<applicationSettings>
<EmailProcessor.My.MySettings>
bla bla bla settings
</EmailProcessor.My.MySettings>
</applicationSettings>
and replaced them with these -
<appSettings>
bla bla bla settings
</appSettings>
but it still did not work.
My VB.Net code that tries to access the information in the AppConfig file is as follows -
A/ i = System.Configuration.ConfigurationManager.AppSettings.Count
This outputs zero
B/ strMailServerName = ConfigurationManager.AppSettings("PW03MailServer")
This outputs nothing
C/ strMailServerName = My.Settings.PW03MailServer
This outputs the correct information - but I cannot use it as I
want to allow many subscribers to use this program.
Thank you very much for your thoughts to date. I would be very pleased if you could help me find out why this isn't working.
Kind regards,
Polly Anna
A/ Original config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="EmailProcessor.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="EmailProcessor.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
<applicationSettings>
<EmailProcessor.My.MySettings>
<setting name="MCMSCefasMailServerName" serializeAs="String">
<value>LOWEXPRESS</value>
</setting>
<setting name="MCMSCefasMailBoxName" serializeAs="String">
<value>MCMS_OLC_Cefas</value>
</setting>
<setting name="MCMSCefasMailBoxPassword" serializeAs="String">
<value>password</value>
</setting>
<setting name="MCMSCefasAttachmentType" serializeAs="String">
<value>xml</value>
</setting>
<setting name="MCMSCefasFolder" serializeAs="String">
<value>C:\Folder</value>
</setting>
<setting name="PW03MailServer" serializeAs="String">
<value>LOWEXPRESS</value>
</setting>
<setting name="PW03MailBoxName" serializeAs="String">
<value>PW03</value>
</setting>
<setting name="PW03MailBoxPassword" serializeAs="String">
<value>daniel1132</value>
</setting>
<setting name="PW03AttachmentType" serializeAs="String">
<value>xml</value>
</setting>
<setting name="PW03Folder" serializeAs="String">
<value>C:\Polly'sFolder</value>
</setting>
<setting name="PW03LogFile" serializeAs="String">
<value>C:\Polly'sFolder</value>
</setting>
<setting name="MCMSCefasLogFile" serializeAs="String">
<value>C:\Folder</value>
</setting>
</EmailProcessor.My.MySettings>
</applicationSettings>
</configuration>
|
|
|
|
|
I've found out what the problem was.
I was using a windows form in another project (i.e. Solution had 2 projects), so it was reading the appConfig file of the windows form and not of the Class library.
Really sorry to have wasted everybody's time - including my own and thank you very much for your suggestions which I have used - i.e. I have used <appsettings> as opposed to <applicationsettings>
Thank you once again.
Kind regards,
Polly Anna
|
|
|
|
|