Write three statements to print the first three elements of array runTimes. Follow each statement with a newline. Ex: If runTime = {800, 775, 790, 805, 808}, print:
800
775
790
Note: These activities will test the code with different test values. This activity will perform two tests, both with a 5-element array (int runTimes[5]). See "How to Use zyBooks".
Also note: If the submitted code tries to access an invalid array element, such as runTime[9] for a 5-element array, the test may generate strange results. Or the test may crash and report "Program end never reached", in which case the system doesn't print the test case that caused the reported message.
#include
using namespace std;
int main() {
const int NUM_ELEMENTS = 5;
int runTimes[NUM_ELEMENTS];
int i;
for (i = 0; i < NUM_ELEMENTS; ++i) {
cin >> runTimes[i];
}
/* Your solution goes here */
return 0;
}
Please help me with this problem using c++.

Respuesta :

Answer:

Following are the code to this question:

for (i = 0; i <3; ++i)//defining loop to print value

{

cout<<runTimes[i]<<endl;//print value

}

Explanation:

Full program code to this question:

#include<iostream>//defining header file

using namespace std;

int main() //defining main method

{

const int NUM_ELEMENTS = 5;//defining const integer variable

int runTimes[NUM_ELEMENTS]; //defining integer array

int i;//defining integer variable

for (i = 0; i < NUM_ELEMENTS; ++i)//defining loop to input value

{

cin >> runTimes[i];//input value  

}

cout<<"print three elements value of array: "<<endl;

for (i = 0; i <3; ++i)//defining loop to print value

{

cout<<runTimes[i]<<endl;//print value

}

return 0;

}

Output:

Please find the attachment.

Description:

  • In the above code, an integer const variable "NUM_ELEMENTS" is declared, that assign value "5" which is array size, in the next step, an array "runTimes" is declared.
  • In the next step, for loop is used, that uses an integer variable "i", which is used to input all array elements.
  • In the next line, another for loop is used, which uses "i" variable, which starts from 0 and ends when its value less than 3, inside the loop print method, used that prints 3 array elements.  
Ver imagen codiepienagoya