The Student Room Group

COMP1 + COMP2 June 2013 - How did you find it? Any tips

Scroll to see replies

Reply 40
Original post by Theoldme
Hey I got 2 questions, the topic is repetition and these questions are related to "Executing a loop a specific number of times".

For my first one, it says "Expand your solution to Question 5 (Spoiler below) and ask the user how many rows of stars they want to see. Display the rectangle of stars as specified by the user. Hint you will need to nest one For loop inside another"

So I've already done question 5 which asks for how many stars the user wants to see in one row. My Code =

Spoiler


If possible please try helping me by giving hints rather than the answer and if I am still stuck by Wednesday ill let you know.

Anyway my second question "Ask the user to enter 10 numbers. display the largest number entered" I think I can do this but keeping in mind that this topic is "Repetition - For Loop" I'm not sure how it will apply to it. Any suggestions??


Okay so you've already got the code to produce one row of stars, so if you want to produce 2 rows of stars, then you just need to get the program to run that same block of code 2 times. Ergo, if your user wants to produce j rows of stars, then you just need to get the same block of code to run j times.

As for your second question, it's possible to use for loops for a lot of it.
First you'll need to ask the user to input their ten numbers - that can be done with a for loop. If you use an array, you can iterate your way through the array to get all ten entries in a couple of lines of code rather than writing a line for each input.

Then there's the actual finding of the largest number, again you can use for loops for that. I'll give you a vague idea which you can build on: Create a variable to store the current largest value. Then iterate your way through the array, replacing the current largest value each time you find a bigger value. By the time you reach the end of the array you should have found the largest value in there. And the iteration can be done using a for loop.
Reply 41
Original post by Jarred
Okay so you've already got the code to produce one row of stars, so if you want to produce 2 rows of stars, then you just need to get the program to run that same block of code 2 times. Ergo, if your user wants to produce j rows of stars, then you just need to get the same block of code to run j times.

As for your second question, it's possible to use for loops for a lot of it.
First you'll need to ask the user to input their ten numbers - that can be done with a for loop. If you use an array, you can iterate your way through the array to get all ten entries in a couple of lines of code rather than writing a line for each input.

Then there's the actual finding of the largest number, again you can use for loops for that. I'll give you a vague idea which you can build on: Create a variable to store the current largest value. Then iterate your way through the array, replacing the current largest value each time you find a bigger value. By the time you reach the end of the array you should have found the largest value in there. And the iteration can be done using a for loop.

I think I solved the first one thanks. As for the second one, I'm lost, you referred to 'array' but on this task I don't think arrays are meant to be used (don't know if that makes sense) but anyway all I got so far is:

Spoiler

Reply 42
Original post by theoldm3
I think I solved the first one thanks. As for the second one, I'm lost, you referred to 'array' but on this task I don't think arrays are meant to be used (don't know if that makes sense) but anyway all I got so far is:

Spoiler



I think arrays are the best way to do it in order to demonstrate your knowledge on how to use for statements as that seems to be the focus of your exercise.

Having taken a look at your code, I've noticed a slight error.

For count = 1 To 10
Console.Write(count & ": ") : numbers = Console.ReadLine
Next


With that, you're not storing ten numbers because you've only got one variable and you're overwriting it each time. You can only access the final number, which isn't useful when you need to be able to access all of them in order to find the largest.

But using an array implementation can quite easily fix that, I've very slightly adapted your code:


Dim numbers(10), count As Integer
Console.WriteLine("Enter your 10 numbers")
For count = 1 To 10
Console.Write(count & ": ") : numbers(count) = Console.ReadLine
Next
Console.ReadLine()


That gets the job done, storing all ten numbers in their own part of the array. It's also pretty efficient, without a for loop and arrays you'd probably have to do something like this:

Dim number1, number2, number3, number4, number5, number6, number7, number8, number9, number10 As Integer

Console.WriteLine("Enter your 10 numbers")
Console.Write(1 & ": ") : number1 = Console.ReadLine
Console.Write(2 & ": ") : number2 = Console.ReadLine
Console.Write(3 & ": ") : number3 = Console.ReadLine
Console.Write(4 & ": ") : number4 = Console.ReadLine
Console.Write(5 & ": ") : number5 = Console.ReadLine
Console.Write(6 & ": ") : number6 = Console.ReadLine
Console.Write(7 & ": ") : number7 = Console.ReadLine
Console.Write(8 & ": ") : number8 = Console.ReadLine
Console.Write(9 & ": ") : number9 = Console.ReadLine
Console.Write(10 & ": ") : number10 = Console.ReadLine

Console.ReadLine()


Which is clearly a horrible way of doing things.

So now going back to the actual problem in hand. Again, now that you have an array the solution is fairly simple to handle with a for loop.

What you need to do is loop through the array and find the largest number. The way I think the exercise wants you to do it in order to demonstrate knowledge of for loops is to start by making a currentLargestValue variable. First, you let that variable be equal to the first item in the array. You then loop through all of the other items, one by one, from 2 to 10. For each one you'll want to compare it to currentLargestValue. If a particular item is larger then it becomes the new currentLargestValue so you overwrite that variable.

By the end of the process, the value of currentLargestValue will represent the actual largest value of the ten items. Effectively what you're doing is going through each number and each time you find a larger one, you store it.

I don't think I explained that particularly well, it's easier to explain it simply through code, though that kind of defeats the process. So what I've done is I've typed out the algorithm in Structured English below. Have a look if you've tried everything and still haven't reached a solution, it'll still need converting to VB.NET though:

Spoiler

does anyone here have the paper and the mark scheme for june 2013 comp2? by any chance
Reply 44
Original post by Jarred
I think arrays are the best way to do it in order to demonstrate your knowledge on how to use for statements as that seems to be the focus of your exercise.

Having taken a look at your code, I've noticed a slight error.

For count = 1 To 10
Console.Write(count & ": ") : numbers = Console.ReadLine
Next


With that, you're not storing ten numbers because you've only got one variable and you're overwriting it each time. You can only access the final number, which isn't useful when you need to be able to access all of them in order to find the largest.

But using an array implementation can quite easily fix that, I've very slightly adapted your code:


Dim numbers(10), count As Integer
Console.WriteLine("Enter your 10 numbers")
For count = 1 To 10
Console.Write(count & ": ") : numbers(count) = Console.ReadLine
Next
Console.ReadLine()


That gets the job done, storing all ten numbers in their own part of the array. It's also pretty efficient, without a for loop and arrays you'd probably have to do something like this:

Dim number1, number2, number3, number4, number5, number6, number7, number8, number9, number10 As Integer

Console.WriteLine("Enter your 10 numbers")
Console.Write(1 & ": ") : number1 = Console.ReadLine
Console.Write(2 & ": ") : number2 = Console.ReadLine
Console.Write(3 & ": ") : number3 = Console.ReadLine
Console.Write(4 & ": ") : number4 = Console.ReadLine
Console.Write(5 & ": ") : number5 = Console.ReadLine
Console.Write(6 & ": ") : number6 = Console.ReadLine
Console.Write(7 & ": ") : number7 = Console.ReadLine
Console.Write(8 & ": ") : number8 = Console.ReadLine
Console.Write(9 & ": ") : number9 = Console.ReadLine
Console.Write(10 & ": ") : number10 = Console.ReadLine

Console.ReadLine()


Which is clearly a horrible way of doing things.

So now going back to the actual problem in hand. Again, now that you have an array the solution is fairly simple to handle with a for loop.

What you need to do is loop through the array and find the largest number. The way I think the exercise wants you to do it in order to demonstrate knowledge of for loops is to start by making a currentLargestValue variable. First, you let that variable be equal to the first item in the array. You then loop through all of the other items, one by one, from 2 to 10. For each one you'll want to compare it to currentLargestValue. If a particular item is larger then it becomes the new currentLargestValue so you overwrite that variable.

By the end of the process, the value of currentLargestValue will represent the actual largest value of the ten items. Effectively what you're doing is going through each number and each time you find a larger one, you store it.

I don't think I explained that particularly well, it's easier to explain it simply through code, though that kind of defeats the process. So what I've done is I've typed out the algorithm in Structured English below. Have a look if you've tried everything and still haven't reached a solution, it'll still need converting to VB.NET though:

Spoiler



Hey,I've done it now, if you have time, could you please explain why you do each step. The thing I dont really understand at all is everything to do with the max, like why do you make it = 0 when declaring it and then later on how does max < numbers(count) Then max = numbers(count) somehow make this max value the largest number. Sorry for wasting your time, could you please also explain the whole thing with the count.

Spoiler



Sorry if you still have any time, for the next question, instead of outputting the largest number, it wants the sum of all the numbers entered. It's basically the same thing as above so no need to re-explain (if you did before). The only part I am a little confused about is thesum = thesum + numbers(count) the part that is confusing me is, it says thesum + numbers(count) however as far as I'm aware, there is no value for thesum so what exactly is the numbers(count) being added to.
Thanks for your help, sorry for wasting your time, I think I am being given a tutor next week so this will be the last few times I will be disturbing your peace

Spoiler

Reply 45
Original post by theoldme3
Hey,I've done it now, if you have time, could you please explain why you do each step. The thing I dont really understand at all is everything to do with the max, like why do you make it = 0 when declaring it and then later on how does max < numbers(count) Then max = numbers(count) somehow make this max value the largest number. Sorry for wasting your time, could you please also explain the whole thing with the count.

Spoiler



Sorry if you still have any time, for the next question, instead of outputting the largest number, it wants the sum of all the numbers entered. It's basically the same thing as above so no need to re-explain (if you did before). The only part I am a little confused about is thesum = thesum + numbers(count) the part that is confusing me is, it says thesum + numbers(count) however as far as I'm aware, there is no value for thesum so what exactly is the numbers(count) being added to.
Thanks for your help, sorry for wasting your time, I think I am being given a tutor next week so this will be the last few times I will be disturbing your peace

Spoiler



No problem :smile: I think that in general the nature of this type of programming challenge make it hard to explain without me being there in the same room as you and showing you through the code, so it might be a good idea to ask your teacher or a mate or the tutor if you still don't get it after this explanation, but I'll give it a try anyway :smile:

So, first of all, I'm just going to rename "max" as "currentLargestValue" simply because I think it might be easier to visualise that way.
As an overview, what we'll be doing is go through the array, looking at each item one by one, and each time we find a value larger than the currentLargestValue then that value itself becomes the new currentLargestValue. By the time we reach the end of the list we'll have the actual largest.

Now let's take a look at the important chunk of your code:

Spoiler



Let's take a look at it again, but this time how about we take it out of the for loop just to make it easier to digest. We'll write it out line by line. This is basically what is being executed when we use the for loop, it's just repeating the same thing 10 times and incrementing the array index by 1:

Spoiler



Now let's actually write that out as a step by step "guide" in English:


> First set currentLargestValue to zero just so that we have a point to start from. We haven't done any comparing yet, so we don't have a largest value - so we may as well use zero to represent that.

>If currentLargestValue is smaller than the first number then the first number becomes the new currentLargestValue.

>If currentLargestValue is smaller than the second number then the second number becomes the new currentLargestValue.

>If currentLargestValue is smaller than the third number then the third number becomes the new currentLargestValue.

>If currentLargestValue is smaller than the fourth number then the fourth number becomes the new currentLargestValue.

...
...
...

>If currentLargestValue is smaller than the tenth (final) number then the tenth number becomes the new currentLargestValue.


Does that make sense to you a bit more? We're going through the list of numbers one by one and trying to find what the largest number is in the list at that point in time. Each time we find a larger number we replace the currentLargestValue with the value of that larger number. By the time we reach the end of the list we'll have found the actual largest number.

We start off by setting this currentLargestValue as zero, simply so we have a starting point. We need it to contain some kind of number, so that when we tell the computer to make the first comparison, it actually has two numbers to compare. We can't tell it to compare numbers(0) with an empty space, with nothingness - it would throw up an error.
Technically, you don't actually need to do this. VB.NET by default stores 0 as the intial value of any number that you declare, but a lot of languages don't, a lot of them leave them empty or at least force you to do the initialisation yourself, so as computer scientists it's a good idea that we do it even in VB.NET, it's a good habit.

Again, this number doesn't need to be zero. We choose zero to represent the fact that it's the smallest non-negative number. If you chose something like 27 and the largest number in your list was 24 then it wouldn't work, the variable would stay as 27. But with zero it'll always work if your list contains positive numbers because positive numbers are always larger than zero, so the program will always have a chance to replace currentLargestValue. The only case in which this program fails is if all of your numbers are negative. In that case, it'd be best to initialise your currentLargestValue variable as numbers(0) instead, you then follow the same process as above but only for count = 1 to 9.




For your next question, I think breaking it down will be a good way of looking at it again. I'm going to write out your code without the for loop again so that we can have a look at what's actually going on when we run the program:

thesum = thesum + numbers(0)
thesum = thesum + numbers(1)
thesum = thesum + numbers(2)
thesum = thesum + numbers(3)
thesum = thesum + numbers(4)
thesum = thesum + numbers(5)
thesum = thesum + numbers(6)
thesum = thesum + numbers(7)
thesum = thesum + numbers(8)
thesum = thesum + numbers(9)

Remember what I said earlier that VB.NET automatically initialises a numerical variable to zero when we first declare it? That happens with theSum too. When we first declare it, it automatically takes on the value of zero until we purposefully change it.

So when on that first line you set theSum = theSum + numbers(0), you're replacing 0 with 0+numbers(0)

Then when you do theSum = theSum + numbers(1) on the next line, you're actually setting theSum = 0 + numbers(0) + numbers(1)

Then when you do theSum = theSum + numbers(2) on the next line, you're actually setting theSum = 0 + numbers(0) + numbers(1) + numbers(2)

When we keep repeating that, we get:
theSum= 0 + numbers(0) + numbers(1) + numbers(2) + ... + numbers(8) + numbers(9)

It's fair to be confused by this sort of thing when you're quite new to programming, I know I certainly was when I was at AS-Level. I think the biggest problem might come with the fact that we use the = sign for variable assignment. Remember that when we say variable1 = variable2 + variable3, we're not saying variable1 EQUALS variable 2 plus variable 3, but rather something more similar to "Replace variable1 with the value of variable2+variable" Doing thisVariable = thisVariable + someOtherVariable is just adding someOtherVariable onto thisVariable.

VB.NET actually has it's own built in operator for this which I think makes it easier to comprehend, it's the += operator.

Rather than doing theSum = theSum + numbers(count), you can write:
theSum += numbers(count)

Either way works, but it might be easier to visualise it all if you use that.

If you have any other questions feel free to say, again I've probably not done the greatest job of explaining this :tongue:
Anyone who took COMP1 exam last year how was the programming part? I just want an idea of how it could be this year and my board is AQA.
Original post by sagar448
Anyone who took COMP1 exam last year how was the programming part? I just want an idea of how it could be this year and my board is AQA.


I did last year's paper as a practice for our Comp 1 & it was so difficult! Even our teacher said it was a really nasty paper. The Section D was slightly random and it took a few times reading it through to actually understand what it was asking you to do
Reply 48
Is it a 60% (comp1) and 40% (comp2) split for you AS Grade?

Quick Reply

Latest

Trending

Trending