137k views
1 vote
Write a program called array1.cpp file that use either regular for loop or range based for loop to display the contents of the array test []. You can also write a function to display the contents of array. Submit your code array1.cpp to Blackboard.

User SkarXa
by
4.4k points

1 Answer

2 votes

Answer:

The program in C++ is as follows:

#include <iostream>

using namespace std;

void display(int array_test [], int n){

for(int i = 0; i<n;i++){

cout<<array_test[i]<<" "; }

}

int main(){

int n;

cin>>n;

int array_test[n];

for(int i = 0; i<n;i++){

cin>>array_test[i];

}

display(array_test,n);

return 0;

}

Step-by-step explanation:

This defines the display function

void display(int array_test [], int n){

This iterates through the array

for(int i = 0; i<n;i++){

This prints each element of the array

cout<<array_test[i]<<" "; }

}

The main begins here

int main(){

This declares n as integer; n represents the length of the array

int n;

This gets input for n

cin>>n;

This declares the array

int array_test[n];

The following iteration gets input for the array

for(int i = 0; i<n;i++){

cin>>array_test[i];

}

This calls the display function to display the elements of the array

display(array_test,n);

return 0;

}

User DJ Tarazona
by
5.3k points