Home > Blockchain >  Max value 2d array using pointer arithmetic
Max value 2d array using pointer arithmetic

Time:10-22

I'm trying to write a programm to find a maximum value in column in a initialized 5x5 matrix, and change it to -1. I found out the way to do it, but i want to find a better solution.

Input:

    double array2d[5][5];
        double *ptr;
        ptr = array2d[0];
//        initializing matrix
        for (int i = 0; i < 5;   i) {
            for (int j = 0; j < 5;   j) {
                if (j % 2 != 0) {
                    array2d[i][j] = (i   1) - 2.5;
                } else {
                    array2d[i][j] = 2 * (i   1)   0.5;
                }
            }
        }

This is my solution for the first column :

//        Changing the matrix using pointer arithmetic

        for (int i = 0; i < (sizeof(array2d) / sizeof(array2d[0][0]));   i) {
            if (i % 5 == 0) {
                if (maxTemp <= *(ptr   i)) {
                    maxTemp = *(ptr   i);
                }
            }
        }
        for (int i = 0; i < (sizeof(array2d) / sizeof(array2d[0][0]));   i) {
            if (i % 5 == 0) {
                if (*(ptr   i) == maxTemp) {
                    *(ptr   i) = -1;
                }
            }
        }

I can repeat this code 5 times, and get the result, but i want a better solution. THX.

CodePudding user response:

I have written it in java but I think u can understand. This one is for all 5 columns at the same time. You can try this:

int count = 0;
double max = 0;
for (int i = 0; i < 5;   i) {
   for (int j = 0; j < 5;   j) {
       if (j == 0) {
            max = array2d[j][I];
            count = 0;
       }
       if (array2d[j][i] > max) {
           count = j;
       }
   }
   array2d[count][i] = -1;
}

CodePudding user response:

Below is the complete program that uses pointer arithmetic. This program replaces all the maximum values in each column of the 2D array -1 as you desire.

#include <iostream>

int main()
{
    double array2d[5][5];
    double *ptr;
    ptr = array2d[0];
    //        initializing matrix
    for (int i = 0; i < 5;   i) {
        for (int j = 0; j < 5;   j) {
            if (j % 2 != 0) {
                array2d[i][j] = (i   1) - 2.5;
            } else {
                array2d[i][j] = 2 * (i   1)   0.5;
            }
        }
    }

    //these(from this point on) are the things that i have added.
    //Everything above this comment is the same as your code.  

    double (*rowBegin)[5]  = std::begin(array2d);
    double (*rowEnd)[5] = std::end(array2d);

    while(rowBegin != rowEnd)
    {
        double *colBegin = std::begin(rowBegin[0]);
        double *colEnd = std::end(rowBegin[0]);
        
        double lowestvalue = *colBegin;//for comparing elements
        double *pointerToMaxValue = colBegin;
        
        while(colBegin!= colEnd)
        {
            if(*colBegin > lowestvalue)
            {
                lowestvalue = *colBegin;
                pointerToMaxValue = colBegin ;
            }
            colBegin = colBegin   1;
        }
        double *newcolBegin = std::begin(rowBegin[0]);
        double *newcolEnd = std::end(rowBegin[0]);

        while(newcolBegin!=newcolEnd)
        {
            if(*newcolBegin == lowestvalue)
            {
                *newcolBegin = -1;  
            }
              newcolBegin;
        }
          rowBegin;
    }
    return 0;
}

The program can be checked here.

You can add print out all the element of the array to check whether the above program replaced all the maximum value in each column with -1.

  • Related