#Program to print the max and min values of an array prints incorrect values

1 messages · Page 1 of 1 (latest)

plain rover
#
#include<stdio.h>
int main(){
    int arr[] = {
    -835, 267, -503, 148, 382, -951, 744, -167, 99, -623,
    827, -402, 616, -792, 19, 374, -561, 990, -122, 203,
    -718, 445, 310, -879, 53, 721, -409, 882, -296, 137,
    584, -811, 739, -201, 932, -58, 417, -694, 235, 671,
    -48, 796, -940, 167, -288, 502, -119, 648, -523, 819,
    -344, 754, -625, 991, -72, 230, -705, 459, -157, 834,
    -317, 672, -890, 413, 278, -942, 687, -36, 529, -788,
    901, -256, 198, -670, 73, 587, -429, 848, -311, 141,
    552, -823, 723, -187, 976, -65, 396, -779, 215, 654,
    -23, 831, -912, 170, -241, 476, -102, 699, -432, 813,
    -383, 792, -561, 988, -92, 289, -728, 502, -143, 789,
    -312, 675, -867, 409, 293, -934, 728, -12, 541, -732,
    910, -235, 184, -612, 38, 571, -349, 830, -276, 120,
    537, -755, 688, -145, 961, -47, 353, -692, 223, 619,
    -31, 807, -956, 145, -207, 459, -94, 689, -418, 795,
    -370, 759, -601, 972, -89, 262, -697, 529, -125, 742,
    -257, 694, -885, 376, 308, -950, 623, -55, 511, -678,
    925, -215, 170, -583, 66, 543, -311, 802, -246, 107,
    498, -721, 654, -201, 947, -82, 324, -719, 189, 602,
    -11, 793, -908, 157, -289, 431, -69, 668, -381, 763,
    -309, 740, -531, 951, -115, 240, -648, 498, -162, 827,
    -285, 665, -882, 368, 251, -925, 712, -45, 486, -721,
    889, -190, 142, -534, 48, 524, -283, 778, -317, 89,
    473, -695, 630, -120, 936, -38, 315, -653, 200, 578
};
    int min,max;
    min=arr[0];
    max=arr[0];
    int temp;
    for(int i=1;i<299;i++)
    {
        if(arr[i]<min){
            min=arr[i];
        }

        if(arr[i]>max){
            max=arr[i];
        }
    }
    printf("%d, %d\n",max,min);
}

This program prints the incorrect values like 1538713651, -1983497187, likely due to overflows, I don't know what the problem is being an novice to programming. Help is appreciated, thank you!

nova halo
#
for(int i=1;i<299;i++)
``` in this line. looks like you only have 240 total elements.
plain rover
#

@nova halo , thanks I didn't notice that! I generated this test array data from AI quickly for 300 mixed numbers, looks like it failed to generate in the total number. Next time I'll make sure to print the total number of elements before executing the main program for easy debugging.