Write a c++ statement that declares and initialized a 3 element int array named
ID: 3837094 • Letter: W
Question
Write a c++ statement that declares and initialized a 3 element int array named floors, use the following numbers to initialize the array. 10, 30, 20. Then write the c++ code to display the contents of the array on the screen using a for loop. Then the same but using a while loop Write a c++ statement that declares and initialized a 3 element int array named floors, use the following numbers to initialize the array. 10, 30, 20. Then write the c++ code to display the contents of the array on the screen using a for loop. Then the same but using a while loopExplanation / Answer
Hi below is the c++ code both using for loop and while loop.
1. using for loop.
#include <iostream>
using namespace std;
/*using for loops. */
int main()
{
const int floor_size = 3; //array size
int floor[floor_size] = {10,30,20}; //array with 3 elements
cout << "Displaying the Array..." << endl;
//for loop iterates through array and display all its elements
for(int i = 0; i < floor_size; i++)
{
cout << "Array[" << i << "] => " << floor[i] << endl;
}
return 0;
}
2. using while loop
#include <iostream>
using namespace std;
/*using for loops. */
int i;
int main()
{
const int floor_size = 3; //array size
int floor[floor_size] = {10,30,20}; //array with 3 elements
cout << "Displaying the Array..." << endl;
//while loop iterates through array and display all its elements tille i < floor_size
while ( i < floor_size)
{
cout << "Array[" << i << "] => " << floor[i] << endl;
++i;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.