Im working on a study guide for my Visual Basic final and im stuck up on this question. Which of the following code segments will copy the values of a 5 element array named intOldValues into another 5 element array named intNewValues? A) intIndex = 1 Do While intIndex <=5 intNewValues = intOldValues intIndex += 1 Loop B) intIndex = 0 Do While intIndex < 5 intNewValues = intOldValues intIndex += 1 Loop C) For intIndex = 1 To 5 intNewValues = intOldValues Next intIndex D) For intIndex = 0 To 4 intOldValues = intNewValues Next intIndex Any feedback would be appreciated
It's C or D, I just can't remember if VB is array base 0 or 1, let me check for you, hold on.
According to a quick search, VB has base 0 array indexes (unless overridden). So the answer is D. If you need an explanation, let me know.
I thought it was D too but I was unsure since it says intOldValues = intNewValues. I figured that since we want to copy it to NewValues it would have to be intNewValues = intOldValues
Oh, I didn't even see that at first. You may be right, it could be C. I remember VB being weird with arrays, and they start at 1 instead of 0. I'm actually going to change my answer to C. Do you have any more code that accompanies this question?
Yeah because it says intOldValues = intNewValues, that doesn't make any sense.
Okay, I'm looking on the MSDN, and yes VB arrays *DO* start at 1 when looping. So yes, the answer is in fact C http://msdn.microsoft.com/en-us/library/5z06z1kb.aspx
Thanks Im glad we caught that trick.
Yes, sorry if I mislead you. I apologize for not seeing it at first.
Not a problem. Are you good with VB? I might have a few more questions.
I've programmed in VB for years, and I have never once been happy doing so :)
Haha, yes I am. Ask away, I'm just doing physics homework.
That'd be awesome. And just to clarify this isnt my homework. Its a study guide for my final so its not for a grade. I know some people get weird about giving answers to homework.
Yeah, if you need an explanation let me know. VB's not like other languages it's .... different (read: "frustrating"). So even if you just need clarification on just the answer, that's fine. :)
But feel free to ask away, I'll be up for another 20 or 30 minutes.
ok. Which of the following is the correct definition for a procedure that accepts a String array as an argument? So the professor didnt give us options for some of the questions. I looked this up in the book and all I could find is to use a function but im not sure if thats what the answer would be
So, an array of strings?
Let me see....
MSDN Link: http://msdn.microsoft.com/en-us/library/vstudio/63y5ksfs(v=vs.100).aspx Public Function PrintMyMessages(messages() As String) As Double ' ... End Function
Oh ok so basically I need to just add the As String
You need to add the () after "messages" That lets the function know it is an array
The As String is used to tell the function it is a string array
For example, if you had an array of doubles, "myDoubles() as Double"
Ok that makes sense and lines up with the book
Which of the following statements defines a two-dimensional Integer array named intTwoDArray with 6 rows and 4 columns? Dim intTwoDArray(5,3) Is that right?
I believe it should be (6,4) (just like in any other language). Let me verify.
I think 0 counts as a column
See ... yeah VB always has to be the weird one. (5,3) declares the array indexes 0 through 5 as the col, and 0 through 3 for each row.
In any other language (like C), if you declare arr[4][3], it declares arr[0 through 3][0 through 2]
So you are correct.
Dim intTwoDArray(5,3)
Nice. Do have any idea what to make of this question? Which of the following could be used to iterate through each element of a two-dimensional array.
"Which of the following" -> Does it give any multiple choice answers? I should know which one if you can provide them.
No it doesnt. Unfortunately.
I found options on google. . A single Do While loop A single Do Until loop A single For Next loop Nested loops For Next loops
Well what you normally do is this: Say you have the array Dim intTwoDArray(5,3) For the columns, we use the variable "i", and for the rows we use the variable "j" Loop i from 0 to 5 Loop j from 0 to 3 'Do code Next Next
So is that D?
So the program would access the array like this: Column 0, row 0 Column 0, row 1 Column 0, row 2 Column 0, row 3 Column 1, row 0 Column 1, row 1 Column 1, row 2 Column 1, row 3 Column 2, row 0 Column 2, row 1 Column 2, row 2 Column 2, row 3 ...etc
Yes, D
Nice. I found some options for that As String question if you want to make sure i have the right answer. . Private Sub ArrayParam(ByVal strStudents As String) Public Sub ArrayParam(ByVal strStudents()) Sub ArrayParam(ByVal strStudents() As String) Private Sub ArrayParam(ByRef strStudents As String)
Let me do a search. Normally in any language, arrays are automatically passed by reference (ByRef) regardless of type. But VB is .... special :) brb.
Oh wait I don't even have to look it up. A and D don't have the parenthesis.
So C im guessing?
Yes it's C. I wanted to make sure. MSDN (see "example"): http://msdn.microsoft.com/en-us/library/dz1z94ha.aspx
btw in case you need to know, ByVal means it makes a copy to the array. That means that anything you do to that array does not affect the original array. On the other hand, if you pass ByRef, you are passing "By Reference", meaning you are referencing the original array. If you make any changes, they WILL affect the original array.
ByVal means it makes a copy of that array*
Oh ok so you can change values with by reference.
Right
So like, say for every value in the array I wanted to subtract 1. I would pass it by reference.
But say I REALLY don't want to affect any values in the array, I would pass by copy (ByVal)
Almost any language passes an array by reference, because it costs memory and time to make a copy of an array. But VB you must explicitly state which one you want every time.
Ya VB is killing me. Im taking C++ at the same time so you can only imagine the confusion
Would this one be B? Which statement is the best solution for the following problem? Write a statement that initializes a two dimensional array of Integers named intGrades. The array will have 2 rows, with 93, 91, and 84 in the first row and 85, 89, and 91 in the second row. Dim intGrades(2,3) As Integer = { {93, 91, 84}, {85, 89, 91} } Dim intGrades(,) As Integer = { {93, 91, 84}, {85, 89, 91} } Dim intGrades() As Integer = { 93, 92, 84,85, 89, 91} Dim intGrades(1, 2) As Integer = { {93, 91, 84}, {85,89, 91} }
I don't even know what school would still offer VB as a language - it's nearly a dead language. C# and VB.NET are its replacements.
I took Java in highschool and I have to take VB C++ and C++ II as far as coding goes
D intGrades(0,0) = 93 intGrades(0,1) = 91 intGrades(0,2) = 84 intGrades(1,0) = 85 intGrades(1,1) = 89 intGrades(1,2) = 91
ok. What would happen if i coded B?
I think it would give you a syntax error. I don't think that's legal to let the compiler assume the size of the array based on the right hand side. Although, I could be wrong.
They do it in the book but they declare MAXRows and MAXColumns
It seems like it's only either Dim intGrades(1, 2) As Integer = { {93, 91, 84}, {85,89, 91} } or Dim intGrades = { {93, 91, 84}, {85,89, 91} }
They use (,) in the book?
For one of the example problems it goes. Const intMAX_ROW = 5 Const intMAX_COL = 3 Dim decPrices( , ) = {{10D, 10D, 10D, 10D}, {20D, 20D, etc......
I just compiled all of these online, and they all work.
I know for certain it's not C
A is also incorrect, because (2,3) will give you 0-2 (3 columns) and 0-3 (4 rows)
Does the way the array is written define how many rows and columns it needs maybe?
It is (,) when you're specifying values. MSDN: http://msdn.microsoft.com/en-us/library/y13tek7e.aspx
Always trust the MSDN, lol.
I have to head to bed, good luck on your final! (Please choose "best answer" if you thought I was helpful)
So is it B then? And i definetly will you were really helpful
It is B according to the MSDN... but I would write a quick program to check if both B and D work.
Ok Will do. Thanks for all your help
No problem at all :) Anything to lessen the suffering of VB upon others, haha.
Haha right?! Night! Thanks again
It would seem that testing the options would be the best way to be 100% sure of the correct solution, and it would be fairly quick also
Join our real-time social learning platform and learn together with your friends!