Please provide me the proper solution since I am trying to do this lab exercise with the below solution to the provided question but unable to complete it

Q ] mapFunction takes the integer input from command line. A list must be generated with the input number of elements in the list starting from zero.
Each number in the List generated numberList must be incremented by 5 using the map and incrementer function, and the resultant list must be printed.
Solution:
object mapFunction{
def main(args :Array[String]){
val input = args(0).toInt
val numberList = 0 to input toList
def incrementer = { input*5 }
val incrementList = List(5,6,7)
println(incrementList)
}
}

Compile the program using:

scalac mapFunction.scala

Execute the program to print the outputs:

scala mapFunction.scala 3

Output will be List(5,6,7)

We don’t give exercise solutions, but we do offer help and answer questions. Where are you getting stuck when you attempt to solve this yourself? We need more information in order to help you.

Is the code you have so far giving you a compile-time error, or a runtime error, or unexpected runtime behavior?

1 Like

Hi,
I was just about to post a solution but then i read your post. I am new here, so i shouldn’t post a solution even though i’ll be explaining in detail what it entails?

Considering that it looks like a homework problem for a class, I don’t think simply posting a solution is appropriate. We don’t have a formal rule, but it’s an informal policy we normally follow here, and it’s typical elsewhere on the net as well. For example, Stack Overflow has a rule: “Questions asking for homework help must include a summary of the work you’ve done so far to solve the problem, and a description of the difficulty you are having solving it.”

3 Likes

As a professor who teaches a lot of Scala I’m going to second this. You don’t learn things by just asking other people to solve them for you.

1 Like

It’s giving compile time error.

I have not asked directly a solution to the question. Please have a look at the description where I have written the solution which I have tried on my own and giving some error. I have asked to correct me where I am wrong in solving the problem. Don’t just comment .

The problem is, you’re not really asking a question, you’re just saying “Fix this!”.

For example, you say later that there is a compile-time error. Exactly what error? What line in the code does it point to? “Compile-time error” doesn’t really tell us anything helpful.

If you want useful answers, you need to provide details about the problem; then you’re much more likely to get useful guidance…

2 Likes

Actually when I try to give the same piece of code, I am not able to proceed, what may be wrong here!

Hello Sir, I am getting the same output for this lab exercise, but I am not able to proceed further, do u know what may be going wrong!

I am unable to guess that unless you explain what you did and where you got stuck.

object mapFunction{
def main(args :Array[String]){
val input = args(0).toInt
val numberList = List(0,1,2)
def incrementer {input*5}
val incrementList = numberList.map { x => x+5 }
println(incrementList)
}
}
I had typed this code,its not proceeding, do u know why, i m getting the same output! List(5,6,7)

you are always getting the same output because your program is not using the input :wink:

if you read it you are essentially doing println(List(0,1,2).map(x => x + 5))

btw if you wrap code blocks in triple backticks it maintains it’s formatting

def example : Int = {
  3 + 1
}

So will it work now,are u sure! So how do I write the code? plz guide me

rather than using a fixed input of List(0,1,2) you should generate one using the first command line argument. In your original post you had this btw

can u tell me how to generate using what?

[quote=“sourav1311, post:12, topic:5869”]
object mapFunction{
def main(args :Array[String]){
val input = args(0).toInt
val numberList = List.range(0, input+1)
def incrementer {input*5}
val incrementList = numberList.map { x => x+5 }
println(incrementList)
}
}

this is he correct code

Thank you very much, nw its working!