Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cs-142:2d-array-fun [2014/10/21 13:49] (current)
kseppi created
Line 1: Line 1:
 +<code cpp>
 +#include <​string>​
 +#include <​iostream>​
  
 +using namespace std;
 +
 +const int HEIGHT = 2;
 +const int WIDTH = 3;
 +
 +void myprint(double an_array[][WIDTH]) {
 + for (int i = 0; i < HEIGHT; i++) {
 + if (0 == i) {
 + cout << "{ {";
 + }
 + else {
 + cout << " ​ {";
 + }
 + for (int j = 0; j < WIDTH; j++) {
 + if (WIDTH - 1 == j) {
 + cout << an_array[i][j] << "​}";​
 + }
 + else {
 + cout << an_array[i][j] << ", ";
 + }
 + }
 + if (HEIGHT - 1 == i) {
 + cout << " }" << endl;
 + }
 + else {
 + cout << ","​ << endl;
 + }
 + }
 +}
 +
 +int main() {
 + const int HEIGHT = 2;
 + const int WIDTH = 3;
 + double values[HEIGHT][WIDTH] = { { 2, 3, 4 }, { 7, 8, 9 } };
 +
 + cout << "​Beginning ...\n\n";​
 +
 + cout << "​Original:​\n\n";​
 +
 + for (int i = 0; i < HEIGHT; i++) {
 + if (0 == i) {
 + cout << "{ {";
 + }
 + else {
 + cout << " ​ {";
 + }
 + for (int j = 0; j < WIDTH; j++) {
 + if (WIDTH - 1 == j) {
 + cout << values[i][j] << "​}";​
 + }
 + else {
 + cout << values[i][j] << ", ";
 + }
 + }
 + if (HEIGHT - 1 == i) {
 + cout << " }" << endl;
 + }
 + else {
 + cout << ","​ << endl;
 + }
 + }
 +
 + cout << "​Modifiying:​\n\n";​
 + for (int i = 0; i < HEIGHT; i++) {
 + for (int j = 0; j < WIDTH; j++) {
 + if ((0 != j) && (0 != i)) {
 + values[i][j] = values[i][j] + values[i - 1][j - 1];
 + }
 + }
 + }
 +
 + cout << "​Modified:​\n\n";​
 + myprint(values);​
 +
 + cout << "​\n...end\n";​
 + system("​pause"​);​
 + return 0;
 +}
 +</​code>​
cs-142/2d-array-fun.txt ยท Last modified: 2014/10/21 13:49 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0