<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cajaun Campbell</title>
    <description>The latest articles on DEV Community by Cajaun Campbell (@cajaun).</description>
    <link>https://dev.to/cajaun</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F667903%2F688ca65e-3794-4486-8fb2-824b75be72f1.jpg</url>
      <title>DEV Community: Cajaun Campbell</title>
      <link>https://dev.to/cajaun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cajaun"/>
    <language>en</language>
    <item>
      <title>Find the highest and lowest value in C++ without the use of an array or function.</title>
      <dc:creator>Cajaun Campbell</dc:creator>
      <pubDate>Sun, 12 Dec 2021 02:17:35 +0000</pubDate>
      <link>https://dev.to/cajaun/find-the-highest-and-lowest-value-in-c-without-the-use-of-an-array-or-function-2oi5</link>
      <guid>https://dev.to/cajaun/find-the-highest-and-lowest-value-in-c-without-the-use-of-an-array-or-function-2oi5</guid>
      <description>&lt;h2&gt;
  
  
  Hello Community! 👋
&lt;/h2&gt;

&lt;p&gt;Greetings folks, I came across a question the other day that  asked to write an algorithm that returns the highest and lowest values without an array or function.&lt;/p&gt;

&lt;p&gt;There are many different approaches to solving this question and I decided to share my solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Replacing the need for an array&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;An array is a data structure consisting of a collection of elements, each identified by at least one array index or key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To replace the array that would store the  number of elements to be entered, we would use a N digit.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;

int main() {

    int i;
    int N;
    int highest;
    int lowest;
    int num;

    i = 1;

    cout &amp;lt;&amp;lt; "Enter the amount of numbers in the series: " ;
    cin &amp;gt;&amp;gt; N;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Getting the first number&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;At the start of writing this algorithm, an integer "i" is declared and given the initial value of 1. "i" will act as the counter of the loop and in order to return the highest and lowest number from the series, a number must first be entered before entering the loop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The value of this number is then assigned to the variable highest and the variable lowest. This is done so that inside the loop when other values are entered, those values can be compared against the values stored in highest and lowest.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;

int main() {

    int i;
    int N;
    int highest;
    int lowest;
    int num;

    i = 1;

    cout &amp;lt;&amp;lt; "Enter the amount of numbers in the series: " ;
    cin &amp;gt;&amp;gt; N;

    cout &amp;lt;&amp;lt; "Enter a number: ";
    cin &amp;gt;&amp;gt; num;
    highest =  num;
    lowest = num;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Running the loop&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The loop is ran based off a counter, and that counter each time the loop is incremented checks to see if it is still less than the value stored in N which is the amount of numbers to be entered in the series.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The value stored in "num" is compared to the value stored in highest. If the value is greater than the value stored in highest,  highest therefore gets the value stored in num.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The value stored in "num" is also compared to the value stored in lowest. If the value is less than the value stored in lowest, lowest therefore gets the value stored in num.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A number is then asked to be entered once again to exit the loop, and the counter for the loop is incremented.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;


int main() {

    int i;
    int N;
    int highest;
    int lowest;
    int num;

    i = 1;

    cout &amp;lt;&amp;lt; "Enter the amount of numbers in the series: " ;
    cin &amp;gt;&amp;gt; N;

    cout &amp;lt;&amp;lt; "Enter a number: ";
    cin &amp;gt;&amp;gt; num;
    highest =  num;
    lowest = num;

        while ( i &amp;lt; N ){

        if (num &amp;gt; highest ) {
        highest = num;
        }

        if (num &amp;lt; lowest ) {
        lowest = num;
         }

        cout &amp;lt;&amp;lt; "Enter a number: ";
        cin &amp;gt;&amp;gt; num;
        i++;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Outside the loop&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The value stored in "num" is compared to the value stored in highest. If the value is greater than the value stored in highest,  highest therefore gets the value stored in num.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is repeated because once the loop ends, if this condition was not present the second highest number would be stored in "num" and not the first highest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A message is printed indicating that the program has ended. Another message is printed indicating the highest and lowest numbers from the series.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    cout &amp;lt;&amp;lt; "The program has ended" &amp;lt;&amp;lt; endl;
    cout &amp;lt;&amp;lt; "The highest number from the series of numbers is: " &amp;lt;&amp;lt; highest &amp;lt;&amp;lt; endl;
    cout &amp;lt;&amp;lt; "The lowest number from the series of numbers is: " &amp;lt;&amp;lt; lowest;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Full Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
#include &amp;lt;iostream&amp;gt;
using namespace std;


int main() {

    int i;
    int N;
    int highest;
    int lowest;
    int num;

    i = 1;

    cout &amp;lt;&amp;lt; "Enter the amount of numbers in the series: " ;
    cin &amp;gt;&amp;gt; N;

    cout &amp;lt;&amp;lt; "Enter a number: ";
    cin &amp;gt;&amp;gt; num;
    highest =  num;
    lowest = num;

        while ( i &amp;lt; N ){

        if (num &amp;gt; highest ) {
        highest = num;
        }

        if (num &amp;lt; lowest ) {
        lowest = num;
         }

        cout &amp;lt;&amp;lt; "Enter a number: ";
        cin &amp;gt;&amp;gt; num;
        i++;
    }
        if (num &amp;gt; highest ) {
        highest = num;
        }

    cout &amp;lt;&amp;lt; "The program has ended" &amp;lt;&amp;lt; endl;
    cout &amp;lt;&amp;lt; "The highest number from the series of numbers is: " &amp;lt;&amp;lt; highest &amp;lt;&amp;lt; endl;
    cout &amp;lt;&amp;lt; "The lowest number from the series of numbers is: " &amp;lt;&amp;lt; lowest;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's it, thank you for reading along I hoped there were some takeaways from this post and that it was a good read.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>tutorial</category>
      <category>algorithms</category>
      <category>writing</category>
    </item>
  </channel>
</rss>
