Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find the largest and smallest number in an array in c
    primarykey
    data
    text
    <p>I have to find a way to display the Maximum and Minium number in an array, the size of the array is 100 and will not exceed that and there is not need for input validation. The program will keep asking for input until 0 is encountered and it too will get added to the array.</p> <p>I have everything figured out except how to keep track which is the largest and smallest value. I'd appreciate it if someone can fix my code or show me.Another problem I'm having is getting the loop to terminate and do max/min calculation within the while loop when the input is equal to 0.</p> <pre><code>/* ============================================================================ Name : test.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define n 100 int main(void){ int numbers[n]; int i = 1; int j; int input; int maxvalue; int minvalue; printf("Enter the next array element&gt;"); input = scanf("%d", &amp;numbers[100]); while (input != 0){ numbers[i] = input; i++; printf("Enter the next array element, while loop&gt;"); input = scanf("%d", &amp;numbers[n]); if (input == 0){ printf("Enter the next array element, if loop"); numbers[i] = 0; for (j =2;j &lt;= i; j++){ minvalue = numbers[1]; j++; if (numbers[j] &gt; minvalue){ maxvalue = numbers[j] ; } else{ minvalue = numbers[j] ; } } } } printf("%f\t", maxvalue); printf("%f\n", minvalue); } </code></pre> <p>EDIT: I took all off your suggestions and edited my code. This is my code below. However, it's output isnt what I'm expecting.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define N 100 int main(void){ int numbers[N]; int i = 0; int j; int input; int maxvalue; int minvalue; printf("Enter the next array element&gt;"); scanf("%d", &amp;input); while (input != 0){ numbers[i] = input; i++; if (input == 0){ i++; numbers[i] = 0; minvalue = numbers[0]; maxvalue = numbers[0]; for (j=0;j&lt;=i-1;j++){ if (minvalue &gt;= numbers[j]){ minvalue = numbers[j]; }else if (maxvalue &lt;= numbers[j]){ maxvalue = numbers[j]; } } /* min = value of first array element max = value of first array element begin loop for each array element, index = 0 to (n-1) --- if array element value is less than min, set min to this value --- if array element value is more than max, set max to this value increment index and repeat loop til last index is completed average = sum / number of elements (n). max and min will hold their correct values.*/ } printf("Enter the next array element, while loop&gt;"); scanf("%d", &amp;input); } printf("%d\t", maxvalue); printf("%d", minvalue); } </code></pre> <p>This is the output, I'm getting! Can someone solve this for me. </p> <pre><code>Enter the next array element&gt;1 Enter the next array element, while loop&gt;2 Enter the next array element, while loop&gt;3 Enter the next array element, while loop&gt;0 12190144 l6Press [Enter] to close the terminal </code></pre> <p>FINAL EDIT: I SOLVED THIS ON MY OWN. I put the min/max checking outside the master WHILE loop, this allowed the input of 0 to be entered in the array.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define N 100 int main(void){ int numbers[N]; int i = 0; int j; int input; int maxvalue =1; int minvalue = 1; printf("Enter the next array element&gt;"); scanf("%d", &amp;input); minvalue = input; maxvalue = input; while (input != 0){ numbers[i] = input; ++i; printf("Enter the next array element&gt;"); scanf("%d", &amp;input); if (input == 0){ numbers[i] = 0; ++i; } } for (j =0;j&lt;i;j++){ if (numbers[j] &gt;= maxvalue){ maxvalue = numbers[j]; } if(numbers[j] &lt; minvalue){ minvalue = numbers[j]; } } printf("%d\t", maxvalue); printf("%d\n", minvalue); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload