pointermanipulationofarray

pointermanipulationofarray - Display a blank line...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 // CS 31 Example Program // February 20, 2008 // John A. Rohr // Pointer Examples #include <iostream> #include <iomanip> using namespace std int main() { int *pa1 // Pointer to array 1 int *pa2 // Pointer to array 2 int *ps1 // Pointer to scan array 1 int *ps2 // Pointer to scan array 1 int *p1last // Pointer last element of array 1 int count // Number of array elements // Prompt for a number of array elements cout << "Number of array elements:" // Get the number of array elements cin >> count // Display a blank line cout << endl // Allocate the first array pa1= new int[count] // Allocate the second array pa2 = new int[count] // Set the pointer to the last element of the first array p1last = pa1 + count - 1 // Fill the first array with successive integers for (ps1 = pa1 ps1 <= p1last ps1++) { // Set one value *ps1 = int(ps1 - pa1) + 1 } // Display a header for the first array cout << "Array 1" << endl
Background image of page 1

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

View Full Document Right Arrow Icon
Sheet1 Page 2 // Display the elements of the first array for (ps1 = pa1 ps1 <= p1last ps1++) { // Display one element cout << setw(4) << *ps1 << endl
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } // Display a blank line cout << endl // Accumulate partial sums for (ps1 = pa1, ps2 = pa2 ps1 <= p1last ps1++, ps2++) { // If the first element is being processed if (ps1 == pa1) { // Set the first element of the second array // to the first element of the first array *ps2 = *ps1 } // Else else { // Accumulate the value of the first array element *ps2 = *(ps2 - 1) + *ps1 } } // Display a header for both arrays cout << "Array 1 Array 2" << endl // Display the elements of both arrays for (ps1 = pa1, ps2 = pa2 ps1 <= p1last ps1++, ps2++) { // Display one element from array 1 cout << setw(4) << *ps1 // Display spaces cout << " " // Display one element from array 2 cout << setw(4) << *ps2 // End the line cout << endl } // Display a blank line cout << endl Sheet1 Page 3 // Delete the first array delete pa1 // Delete the second array delete pa2 // Display a completion message cout << "Done." << endl // Return return 0 }...
View Full Document

{[ snackBarMessage ]}

Page1 / 3

pointermanipulationofarray - Display a blank line...

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

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