passingArrays - // now pass 2D array to function that will...

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

View Full Document Right Arrow Icon
#include <iostream> using namespace std; void printCharArray(char []); int sumArray(int []); bool printedArray(int [2][3]); int main(){ int sum; // local array defined explicitly int array1[] = {23, 24, 25, 26}; int array2[2][3] = {{45, 46, 47},{47,48, 49}}; // store up to 40 characters in following array char className[40] = "Intro to Computers for Engineers"; // print char array // this will not work: cout << className << endl; so, use a loop int j=0; while(className[j] != '\0'){ cout << className[j]; j++; } cout<<endl; // now pass array to function that print it printCharArray(className); // boolean variable that denote a true or false situation bool printed = false; // pass 1D array to function that will sum it and return the sum sum = sumArray(array1); cout << "The sum of the elements in array1 = " << sum << endl;
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: // now pass 2D array to function that will print it and return true when done printed = printedArray(array2); if (printed == true) cout &lt;&lt; printed; else // won't print this because the array is always going to print cout &lt;&lt; &quot;Didn't print&quot;; return 0; } // print char array function void printCharArray(char array){ int j=0; while(array[j] != '\0'){ cout &lt;&lt; array[j]; j++; } cout &lt;&lt; endl; } int sumArray(int array){ int total = 0; for(int k = 0; k &lt; 4; k++){ total = total + array[k]; } return total; } bool printedArray(int array[2][3]){ // need to know the size of array // to traverse rows for(int k = 0; k &lt; 2; k++){ // to traverse columns for(int t = 0; t &lt; 3; t++){ cout &lt;&lt; array[k][t] &lt;&lt; &quot;\t&quot;; } cout &lt;&lt; endl; } // printed return true; }...
View Full Document

Page1 / 2

passingArrays - // now pass 2D array to function that will...

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