151 lines
21 KiB
HTML
151 lines
21 KiB
HTML
<html>
|
|
<head>
|
|
<title>twoDarraysChar.cpp</title>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<style type="text/css">
|
|
.s0 { color: #7a7e85;}
|
|
.s1 { color: #bcbec4;}
|
|
.s2 { color: #b3ae60;}
|
|
.s3 { color: #6aab73;}
|
|
.s4 { color: #cf8e6d;}
|
|
.s5 { color: #bcbec4;}
|
|
.s6 { color: #2aacb8;}
|
|
</style>
|
|
</head>
|
|
<body bgcolor="#1e1f22">
|
|
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
|
|
<tr><td><center>
|
|
<font face="Arial, Helvetica" color="#000000">
|
|
twoDarraysChar.cpp</font>
|
|
</center></td></tr></table>
|
|
<pre><span class="s0">//</span>
|
|
<span class="s0">// Created by caleb on 3/20/24.</span>
|
|
<span class="s0">//</span>
|
|
|
|
<span class="s2">#include </span><span class="s3"><iostream></span>
|
|
|
|
<span class="s4">char</span><span class="s5">** </span><span class="s1">create2DarrayChar</span><span class="s5">(</span><span class="s4">int </span><span class="s1">rows</span><span class="s5">, </span><span class="s4">int </span><span class="s1">columns</span><span class="s5">) {</span>
|
|
<span class="s4">char </span><span class="s5">**</span><span class="s1">pp </span><span class="s5">= </span><span class="s4">new char</span><span class="s5">* [</span><span class="s1">rows</span><span class="s5">];</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">] = </span><span class="s4">new char</span><span class="s5">[</span><span class="s1">columns </span><span class="s5">+ </span><span class="s6">1</span><span class="s5">];</span>
|
|
<span class="s5">}</span>
|
|
<span class="s4">return </span><span class="s1">pp</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
|
|
|
|
<span class="s4">void </span><span class="s1">populateChar</span><span class="s5">(</span><span class="s4">char</span><span class="s5">** </span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">, </span><span class="s4">int </span><span class="s1">columns</span><span class="s5">) {</span>
|
|
<span class="s4">int </span><span class="s1">ascii </span><span class="s5">= </span><span class="s3">'A'</span><span class="s5">;</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s4">int </span><span class="s1">j </span><span class="s5">= </span><span class="s6">0</span><span class="s5">;</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s1">j </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">j </span><span class="s5">< </span><span class="s1">columns </span><span class="s5">-</span><span class="s6">1</span><span class="s5">; ++</span><span class="s1">j</span><span class="s5">) {</span>
|
|
<span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j</span><span class="s5">] = </span><span class="s1">ascii </span><span class="s5">+ </span><span class="s1">i </span><span class="s5">+ </span><span class="s1">j</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j</span><span class="s5">] = </span><span class="s3">'</span><span class="s4">\0</span><span class="s3">'</span><span class="s5">;</span>
|
|
<span class="s1">ascii </span><span class="s5">+= </span><span class="s6">10</span><span class="s5">;</span>
|
|
<span class="s4">if </span><span class="s5">(</span><span class="s1">ascii </span><span class="s5">> </span><span class="s6">100</span><span class="s5">) {</span>
|
|
<span class="s1">ascii </span><span class="s5">= </span><span class="s6">65</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
|
|
<span class="s4">void </span><span class="s1">populateCharReverse</span><span class="s5">(</span><span class="s4">char</span><span class="s5">** </span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">, </span><span class="s4">int </span><span class="s1">columns</span><span class="s5">) {</span>
|
|
<span class="s4">int </span><span class="s1">ascii </span><span class="s5">= </span><span class="s3">'A'</span><span class="s5">;</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s0">//for (int i = rows - 1; i >= 0; --i) {</span>
|
|
<span class="s4">int </span><span class="s1">j </span><span class="s5">= </span><span class="s1">columns</span><span class="s5">;</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">j </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">j </span><span class="s5">< </span><span class="s1">columns</span><span class="s5">; ++</span><span class="s1">j</span><span class="s5">) {</span>
|
|
<span class="s0">//for (j = columns - 1; j >= 0; --j) {</span>
|
|
<span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j</span><span class="s5">] = </span><span class="s1">ascii </span><span class="s5">- </span><span class="s1">i </span><span class="s5">- </span><span class="s1">j</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j</span><span class="s5">] = </span><span class="s3">'</span><span class="s4">\0</span><span class="s3">'</span><span class="s5">;</span>
|
|
<span class="s1">ascii </span><span class="s5">+= </span><span class="s6">10</span><span class="s5">;</span>
|
|
<span class="s4">if </span><span class="s5">(</span><span class="s1">ascii </span><span class="s5">> </span><span class="s6">100</span><span class="s5">) {</span>
|
|
<span class="s1">ascii </span><span class="s5">= </span><span class="s6">65</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
|
|
<span class="s0">///////////////////////////////////////////////////////////////////////////</span>
|
|
<span class="s0">/* traverses */</span>
|
|
<span class="s4">void </span><span class="s1">traverseChar</span><span class="s5">(</span><span class="s4">char</span><span class="s5">** </span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">, </span><span class="s4">int </span><span class="s1">columns</span><span class="s5">) {</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">j </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">j </span><span class="s5">< </span><span class="s1">columns</span><span class="s5">; ++</span><span class="s1">j</span><span class="s5">) {</span>
|
|
<span class="s1">std</span><span class="s5">::</span><span class="s1">cout </span><span class="s5"><< </span><span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j</span><span class="s5">] << </span><span class="s3">" "</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s1">std</span><span class="s5">::</span><span class="s1">cout </span><span class="s5"><< </span><span class="s1">std</span><span class="s5">::</span><span class="s1">endl</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
|
|
<span class="s4">void </span><span class="s1">traverseCharSingleLoop </span><span class="s5">(</span><span class="s4">char</span><span class="s5">** </span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">) {</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s1">std</span><span class="s5">::</span><span class="s1">cout </span><span class="s5"><< </span><span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">] << </span><span class="s1">std</span><span class="s5">::</span><span class="s1">endl</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s0">/////////////////////////////////////////////////////////////////////////////</span>
|
|
<span class="s0">/* frees */</span>
|
|
<span class="s4">void </span><span class="s1">freeMemoryChar</span><span class="s5">(</span><span class="s4">char </span><span class="s5">**</span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">) {</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s4">delete</span><span class="s5">[] </span><span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">];</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
|
|
|
|
<span class="s0">/////////////////////////////////////////////////////////////////////////////</span>
|
|
<span class="s0">/* Dereference versions */</span>
|
|
|
|
<span class="s4">char</span><span class="s5">** </span><span class="s1">create2DarrayCharDereference</span><span class="s5">(</span><span class="s4">int </span><span class="s1">rows</span><span class="s5">, </span><span class="s4">int </span><span class="s1">columns</span><span class="s5">) {</span>
|
|
<span class="s4">char </span><span class="s5">**</span><span class="s1">pp </span><span class="s5">= </span><span class="s4">new char</span><span class="s5">* [</span><span class="s1">rows</span><span class="s5">];</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s5">*(</span><span class="s1">pp </span><span class="s5">+ </span><span class="s1">i</span><span class="s5">) = </span><span class="s4">new char</span><span class="s5">[</span><span class="s1">columns </span><span class="s5">+ </span><span class="s6">1</span><span class="s5">];</span>
|
|
<span class="s5">}</span>
|
|
<span class="s4">return </span><span class="s1">pp</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
|
|
<span class="s4">void </span><span class="s1">populateCharDereference</span><span class="s5">(</span><span class="s4">char</span><span class="s5">** </span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">, </span><span class="s4">int </span><span class="s1">columns</span><span class="s5">) {</span>
|
|
<span class="s4">int </span><span class="s1">ascii </span><span class="s5">= </span><span class="s3">'A'</span><span class="s5">;</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s4">int </span><span class="s1">j </span><span class="s5">= </span><span class="s6">0</span><span class="s5">;</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s1">j </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">j </span><span class="s5">< </span><span class="s1">columns </span><span class="s5">-</span><span class="s6">1</span><span class="s5">; ++</span><span class="s1">j</span><span class="s5">) {</span>
|
|
<span class="s5">*(*(</span><span class="s1">pp </span><span class="s5">+ </span><span class="s1">i</span><span class="s5">) + </span><span class="s1">j</span><span class="s5">) = </span><span class="s1">ascii </span><span class="s5">+ </span><span class="s1">i </span><span class="s5">+ </span><span class="s1">j</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j</span><span class="s5">] = </span><span class="s3">'</span><span class="s4">\0</span><span class="s3">'</span><span class="s5">;</span>
|
|
<span class="s1">ascii </span><span class="s5">+= </span><span class="s6">10</span><span class="s5">;</span>
|
|
<span class="s4">if </span><span class="s5">(</span><span class="s1">ascii </span><span class="s5">> </span><span class="s6">100</span><span class="s5">) {</span>
|
|
<span class="s1">ascii </span><span class="s5">= </span><span class="s6">65</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s4">void </span><span class="s1">traverseCharDereference</span><span class="s5">(</span><span class="s4">char</span><span class="s5">** </span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">, </span><span class="s4">int </span><span class="s1">columns</span><span class="s5">) {</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">j </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">j </span><span class="s5">< </span><span class="s1">columns</span><span class="s5">; ++</span><span class="s1">j</span><span class="s5">) {</span>
|
|
<span class="s1">std</span><span class="s5">::</span><span class="s1">cout </span><span class="s5"><< *(*(</span><span class="s1">pp </span><span class="s5">+ </span><span class="s1">i</span><span class="s5">) + </span><span class="s1">j</span><span class="s5">) << </span><span class="s3">" "</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s1">std</span><span class="s5">::</span><span class="s1">cout </span><span class="s5"><< </span><span class="s1">std</span><span class="s5">::</span><span class="s1">endl</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s4">void </span><span class="s1">traverseCharSingleLoopDereference</span><span class="s5">(</span><span class="s4">char</span><span class="s5">** </span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">) {</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s1">std</span><span class="s5">::</span><span class="s1">cout </span><span class="s5"><< *(</span><span class="s1">pp </span><span class="s5">+ </span><span class="s1">i</span><span class="s5">) << </span><span class="s1">std</span><span class="s5">::</span><span class="s1">endl</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s4">void </span><span class="s1">freeMemoryCharDereference</span><span class="s5">(</span><span class="s4">char </span><span class="s5">**</span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">) {</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">rows</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s4">delete</span><span class="s5">[] *(</span><span class="s1">pp </span><span class="s5">+ </span><span class="s1">i</span><span class="s5">);</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
|
|
<span class="s4">void </span><span class="s1">sort</span><span class="s5">(</span><span class="s4">char </span><span class="s5">**</span><span class="s1">pp</span><span class="s5">, </span><span class="s4">int </span><span class="s1">columns</span><span class="s5">, </span><span class="s4">int </span><span class="s1">rows</span><span class="s5">) {</span>
|
|
<span class="s4">char </span><span class="s1">temp</span><span class="s5">;</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">i </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">i </span><span class="s5">< </span><span class="s1">columns</span><span class="s5">; ++</span><span class="s1">i</span><span class="s5">) {</span>
|
|
<span class="s4">for </span><span class="s5">(</span><span class="s4">int </span><span class="s1">j </span><span class="s5">= </span><span class="s6">0</span><span class="s5">; </span><span class="s1">j </span><span class="s5">< (</span><span class="s1">rows </span><span class="s5">- </span><span class="s6">1</span><span class="s5">); ++</span><span class="s1">j</span><span class="s5">) {</span>
|
|
<span class="s4">if </span><span class="s5">(</span><span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j</span><span class="s5">] > </span><span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j </span><span class="s5">+ </span><span class="s6">1</span><span class="s5">]) {</span>
|
|
<span class="s1">temp </span><span class="s5">= </span><span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j</span><span class="s5">];</span>
|
|
<span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j</span><span class="s5">] = </span><span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j </span><span class="s5">+ </span><span class="s6">1</span><span class="s5">];</span>
|
|
<span class="s1">pp</span><span class="s5">[</span><span class="s1">i</span><span class="s5">][</span><span class="s1">j </span><span class="s5">+ </span><span class="s6">1</span><span class="s5">] = </span><span class="s1">temp</span><span class="s5">;</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
<span class="s5">}</span>
|
|
</pre>
|
|
</body>
|
|
</html> |