1

I was working on the following 2d-array program to output this result shown in picture:

I can't seem to get the min value for the result and get it displayed in array form. The code is below:

#include<stdio.h>
#define NUMROWS 2
#define NUMCOLS 3

//accessing elements of 2D array using pointers
int main(void){

    const int table[NUMROWS][NUMCOLS]={{1,2,3},{5,6,7}};
    int minvals[NUMROWS];
    int i, j;
    int *ptr = &table;

    //accessing the elements of 2D array using ptr
    printf("table values:  min value\n");

    for(int i=0;i<NUMROWS;i++){
        for(int j=0;j<NUMCOLS;j++)
            printf("%d ",*((ptr+i*NUMCOLS)+j)); 
        printf("\n");
    }
    
    for(int i=0;i<NUMROWS;i++){
        for(int j=0;j<NUMCOLS;j++)
            printf("%d ",*((ptr+i*NUMCOLS)+j)<minvals[i]); 
    }
    return 0;
}
6
  • 2
    "I can't seem to display the result in array form and make it executeable as a command line argument." - that's because you've written no code to actually do so. Commented Jun 12, 2021 at 17:13
  • This is the code in question I need to complete, not my own code. Commented Jun 12, 2021 at 17:15
  • 2
    Yes, it's the code you need to complete - not us. Make an attempt to solve to problem first, then come back when you hit a wall, and have a specific question about your own code. If you've made an attempt - please show it, not just the outline of the question. Please read How do I ask homework questions? Commented Jun 12, 2021 at 17:18
  • Oh yes, ofc, my code was giving me compilation error so I did not include it but I will put it in here, thanks for this Commented Jun 12, 2021 at 17:41
  • My impression is that it is too hard for you to solve the entire problem at once. Therefore, I suggest that you first attempt to write the code for printing only the first line (which does not require a nested loop), and once that works, then you can expand it so that it prints several lines (which requires a nested loop). Commented Jun 12, 2021 at 17:45

2 Answers 2

2

The existence of minvals would imply that you are expected to calculate the minimum value of each 'row' of table before then moving on to printing. As it stands, had your program properly calculated the minimum values of each array, your printing would be rather out of order.

There's no need to do any tricky, manual pointer manipulation. Simple array subscription is much clearer.

Let's start simple and return to basics by looking at the way we find the minimum value in a one dimensional array, as it is the core of this problem.

To find the minimum value in an array we need a few things to start:

  • An array
  • The length of the array
  • An initial value to compare against

The array itself is obviously each subarray of table, and the length in this case is known to be NUMCOLS. Our initial value should either be INT_MAX (or another type-appropriate maximum constant found <limits.h>), such that every element in the array is equal to or less than our initial value, or a value from the array itself.

Often times we opt for the second option here, choosing the first element in the array as our initial value, and comparing it to the second and onward elements.

As such, finding the minimum value in a single 'row' would look like this

const int row[NUMCOLS] = { 9, 2, 5 };
int min = row[0];

for (int i = 1; i < NUMCOLS; i++)
    if (row[i] < min)
        min = row[i];

but since we want to find and record the minimum value of each 'row' in table, we're going to use a nested loop. Instead of the min variable from before, we store each value in the associated index of our minvals array.

for (i = 0; i < NUMROWS; i++) {
    minvals[i] = table[i][0];

    for (j = 1; j < NUMCOLS; j++)
        if (table[i][j] < minvals[i])
            minvals[i] = table[i][j];
}

When it comes time to print, we're going to repeat our nested loop. Our inner loop prints each element of each 'row' of table, and we end each iteration of the outer loop by printing the value found in minvals with the same index of our 'row'.

for (i = 0; i < NUMROWS; i++) {
    for (j = 0; j < NUMCOLS; j++)
        printf("%6d", table[i][j]);

    printf(":%6d\n", minvals[i]);
}

Here's a working example.

#include <stdio.h>
#define NUMROWS 2
#define NUMCOLS 3

int main(void) {
    const int table[NUMROWS][NUMCOLS] = {
        { 9, 2, 5 },
        { 3, -4, -12 }
    };
    int minvals[NUMROWS];
    int i, j;

    for (i = 0; i < NUMROWS; i++) {
        minvals[i] = table[i][0];

        for (j = 1; j < NUMCOLS; j++)
            if (table[i][j] < minvals[i])
                minvals[i] = table[i][j];
    }

    puts("Table value: minimum values");

    for (i = 0; i < NUMROWS; i++) {
        for (j = 0; j < NUMCOLS; j++)
            printf("%6d", table[i][j]);

        printf(":%6d\n", minvals[i]);
    }
}

A good further exercise for you would be to compose the logic of the inner loop for finding minimum values into a more generic function. Its function signature would look like

int min(int *array, size_t length);

allowing it to work on arrays of varying sizes. Then our outer loop could be as simple as:

for (i = 0; i < NUMROWS; i++)
    minvals[i] = min(table[i], NUMCOLS);
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! the explanation helped me alot and I will save it in my notes for solving my other problems in this chapter. Thanks alot!
1

The line

int *ptr = &table;

is wrong, because &table is of type int (*)[2][3] (i.e. a pointer to the entire table), whereas ptr is a pointer to a single element. Also, your pointer is non-const, so it cannot point be made to point into a const array.

If you want ptr to point to a single int value, then you should declare it the following way:

const int *ptr = &table[0][0];

Also, you are reading the contents of the array minvals, although that array contains uninitialized data. This does not make sense and causes undefined behavior.

Instead of doing complex pointer arithmetic with the expression

*((ptr+i*NUMCOLS)+j))

you can simply write the following:

table[i][j]

That way, you do not need the pointer ptr and your code is simpler.

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.