21.8k views
5 votes
Write a loop to print all elements in hourly_temperature. Separate elements with a -> surrounded by spaces. Sample output for the given program with input: '90 92 94 95' 90 -> 92 -> 94 -> 95 Note: 95 is followed by a space, then a newline.

2 Answers

3 votes

Answer:

user_input = input()

hourly_temperature = user_input.split()

for i in range(len(hourly_temperature)):

if i != len(hourly_temperature) - 1:

print(hourly_temperature[i], '->', end = ' ')

else:

print(hourly_temperature[i],(''))

Step-by-step explanation:

User Marialisa
by
5.1k points
3 votes

Answer:

The program to this question can be described as follows:

Program:

#include <iostream> //defining header file

using namespace std;

int main() //defining main method

{

int hourly_temperature[] ={90,92,94,95}; //defining integer array

int len= sizeof(hourly_temperature)/ sizeof(hourly_temperature[0]); //calculate length of array

for(int i=0;i<len-1;i++) //loop to print its value

{

cout<<hourly_temperature[i]<<"->"; //print value

}

cout<<hourly_temperature[len-1]; //print last element value

return 0;

}

Output:

90->92->94->95

Step-by-step explanation:

In the given code an integer array "hourly_temperature" is declared, which holds some value, that is "90,92,94,95", in the next step, an integer variable "len" variable is declared, that holds array size.

  • Then a for loop is declare that uses the "i" variable, which starts from 0 and ends when the "i" value is less than "len-1".
  • Inside the loop array values are printed with the "->" sign, and outside the loop, the last variable value is printed.
User TheYogi
by
5.0k points