quiz3_solution

quiz3_solution - cout<< a[m--]<<’ ’;...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//Quiz 3 //This program uses the concept of passing arrays to functions //and generates a pattern of numbers (stored in the array b). //Analyze the program and Complete the Out Put. #include<iostream.h> void printpattern(int [], int ); int main() { int b[] = {0, 9 , 8 ,5 ,3 ,7 ,2 ,6 , 1 , 3}; printpattern(b,10); return 0; } void printpattern(int a[] , int size) { int m = 0; for(int i = 1; i<=4;++i) { for(int j = 1; j<=i ;++j) cout<< a[m++]<<’ ’; cout<<endl; } m = size - 1; for(int i = 4; i>0;--i) { for(int j = 1; j<=i ;++j)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cout<< a[m--]<<’ ’; cout<<endl; } } Solution: Array b is passed to the function by reference, this array is addressed as a inside the function. The index variable i keeps track of the line numbers, index variable j keeps track of the number of elements to be printed on each line and the variable m keeps track of the array indices. The looping process arranges the elements as shown below a[0] a[0] a[1] a[0] a[1] a[2] a[0] a[1] a[2] a[3] a[3] a[2] a[1] a[0] a[2] a[1] a[0] a[1] a[0] a[0] 0 9 8 5 3 7 2 6 1 3 3 1 6 2 7 3 5 8 9 0...
View Full Document

This document was uploaded on 07/06/2011.

Page1 / 2

quiz3_solution - cout<< a[m--]<<’ ’;...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online