157k views
4 votes
Write a function to output an array of ints on a single line. Funtion Should take an array and an array length and return a void. It should look like this {5,10,8,9,1}

User Lee Kang
by
8.1k points

1 Answer

5 votes

Answer:

void printarr(int nums[],int n)

{

cout<<"{";//printing { before the elements.

for(int i=0;i<n;i++) // iterating over the array.

{

cout<<nums[i];//printing the elements.

if(i==n-1)//if last element then come out of the loop.

break;

cout<<",";//printing the comma.

}

cout<<"}"<<endl;//printing } at the end.

}

Output:-

5

1 2 3 4 5

{1,2,3,4,5}

Step-by-step explanation:

I have created a function printarr of type void which prints the array elements in one line.I have used for loop to iterate over the array elements.Everything else is mentioned in the comments.

User Narnik Gamarnik
by
8.7k points