Computer Science, asked by ananyasinha51, 15 days ago

Write a program to store eight numbers in an array from the user and print all the numbers which are greater than 40.​

Answers

Answered by suhani1879
0

Answer:Since you didn’t specify the language, I am absolutely sure you wanted a solution for this problem in J. As a bonus, it will actually work on an array of arbitrary length, as long as you split all entries by spaces!

+/@:(".;._2&:(,&' ')&:(1!:1)&1:) ''  

+/ - sum array elements

".;._2 - split string along its last symbol and parse into numbers

,&' ' - concatenate a single space onto the end of the string

(1!:1) - read from a given file handle (1 is keyboard)

1: - ignore the input argument (see below) and output 1

'' - this is just a phoney argument to get the function started, as J has no functions of 0 arguments.

I hope this helps! Otherwise, on a slim chance you’re using some boring language for your homework, consider reading associated material in the textbook and using it to come up with a solution. I am fairly sure for-loops and array indexing are covered thoroughly.

P.S. If you’re really insistent about it being 10 numbers and no more or fewer, you will need to pipe in a function, which will throw an error on length mismatch:

[:^:(10&:~:&:#)  

NB. [: - throw error  

NB. ^: - if  

NB. 10 - ten  

NB. ~: - is not equal to  

NB. #  - length of array  

This will produce the final function

+/@:([:^:(10&~:&:#))@:(".;._2&:(,&' ')&:(1!:1)&1:) ''  

   ^^ HERE IT IS ^^  

P.P.S. I used a boring language, but I forgot to use the array :(

public static void main(String... args) {  

   final Scanner scanner = new Scanner(System.in);  

   System.out.println(  

           IntStream.generate(scanner::nextInt).limit(10).sum()  

   );  

}  

Explanation:

Similar questions