0

Ok so I created a program that successfully creates a 2D array and stores random numbers in the array and determines the highest, lowest, and average of the random numbers created using arrays and for loop statements. However I have a feeling that something is incorrect with my code but cannot figure it out. I believe that it is something with the for loop statements I used.

Here is the code:

#include <stdio.h> 
#include <stdlib.h>
#include <time.h> 

#define ROW 3
#define COLUMN 5

int main( void )
{

    int randnum_array [ROW] [COLUMN];
    int highrn = 0 , lowrn = 9999999 , total = 0 , nextvalue = 0;
    int r = 0 , c = 0 , lr = 0 , lc = 0 , hr = 0 , hc = 0;
    float average = 0.0;
    srandom ( (unsigned) time (NULL) ); 

    printf( "\n Welcome User, this program creates an array random numbers "
    "and stores them into each \n of the 11 elements of the rand_num "
    "array. The program then displays the \n highest, lowest, "
    "and average of the numbers created.\n\n" ) ;

    printf("   Contents of randnum_array:\n\n");

    for (r = 0; r < ROW ; r++)
    {
        for (c = 0; c < COLUMN ; c++)
        {
            nextvalue = random ( ) % 10001;
            printf("     Value in randnum_array row %d column %d is: %d\n", r+1 , c+1 , nextvalue);
            randnum_array [r] [c] = nextvalue;

            if (nextvalue > highrn) 
            {
                highrn = nextvalue;
                hr = r;
                hc = c;
            }

            if (nextvalue < lowrn) 
            {
                lowrn = nextvalue;
                lr = r;
                lc = c;
            }

            total = total + nextvalue;
        }
    }

    average = (float)total / (ROW * COLUMN);

    printf("\n   The lowest value is %d in row %d column %d.", lowrn , lr+1 ,
        lc+1 ) ;

    printf("\n   The highest value is %d in row %d column %d.", highrn , hr+1 , 
        hc+1) ;

    printf("\n   The average of all of the numbers in the randnum_array"
        " is: %-7.3f", average ) ;

    printf("\n\n Thank you for using this program\n\n" ) ;

    return ( 0 ) ;

}

This is the output:

 Welcome User.

 Contents of randnum_array:

 Value in randnum_array row 1 column 1 is: 2375
 Value in randnum_array row 1 column 2 is: 102
 Value in randnum_array row 1 column 3 is: 1754
 Value in randnum_array row 1 column 4 is: 6464
 Value in randnum_array row 1 column 5 is: 3237
 Value in randnum_array row 2 column 1 is: 3495
 Value in randnum_array row 2 column 2 is: 2221
 Value in randnum_array row 2 column 3 is: 5663
 Value in randnum_array row 2 column 4 is: 885
 Value in randnum_array row 2 column 5 is: 8442
 Value in randnum_array row 3 column 1 is: 4465
 Value in randnum_array row 3 column 2 is: 4561
 Value in randnum_array row 3 column 3 is: 9182
 Value in randnum_array row 3 column 4 is: 1781
 Value in randnum_array row 3 column 5 is: 7478

 The lowest value is 102 in row 1 column 2.
 The highest value is 9182 in row 3 column 3.
 The average of all of the numbers in the randnum_array is: 4140.333

 Thank you for using this program

Can anyone give me some advice as to what might be wrong with the code, or if it is fine as is. Everything functions correctly, but since I have never worked with 2D arrays before i'm not really too sure what I am doing.

4
  • 1
    lowrn = 9999999 is out of range of int. Commented Oct 29, 2013 at 9:30
  • So should i change it to a different number? I heard that your supposed to initialize low variables really high. Commented Oct 29, 2013 at 9:32
  • 1
    You can use INT_MAX and INT_MIN from limits.h. Commented Oct 29, 2013 at 9:40
  • My professor hasn't taught us how to use those yet, and we are not allowed to use anything he hasn't taught yet, which is kind of aggravating. Could I just set it to 9999? Commented Oct 29, 2013 at 9:45

1 Answer 1

1

Initialize values with 0, and in first iteration set values to first one with

int highrn = 0 , lowrn = 0;
for (r = 0; r < ROW ; r++)
    {
        for (c = 0; c < COLUMN ; c++)
        {
            nextvalue = random ( ) % 10001;
            printf("     Value in randnum_array row %d column %d is: %d\n", r+1 , c+1 , nextvalue);
            randnum_array [r] [c] = nextvalue;

            if( r==0 && c==0 ) {
                 hihgrn = lowrn = nextvalue;
                 hr = hc = lr = lc = 0;
            }
            else {
              // otherwise - perform your checks
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.