Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 loop

Explanation / 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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote