Click here to Skip to main content
15,503,491 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
during the first run it works well but during the second and so on it didn't work right anymore..
here's my code:

#include <stdio.h>

 void main()
{

    int arr[4][5];
    int row, col;

    printf("Input array values \n");
    for(col=0;col<5;col++)
    {
       for(row=0;row<4;row++)
       {
           printf("arr[col %d][ row %d]: ",col,row);
           scanf("%d", &arr[col][row]);
       }
    }


     printf("\n\narray values \n");
    for(row=0;row<4;row++)
    {
       for(col=0;col<5;col++)
       {
           printf("%d ", arr[row][col]);

       }
       printf("\n");
    }

}


for example if i inputted:
arr[col 1][row 1]: 7
arr[col 1][row 2]: 755
arr[col 1][row 3]: 3
arr[col 2][row 0]: 4
arr[col 2][row 1]: 2
arr[col 2][row 2]: 3
arr[col 2][row 3]: 4
arr[col 3][row 0]: 5
arr[col 3][row 1]: 6
arr[col 3][row 2]: 7
arr[col 3][row 3]: 6
arr[col 4][row 0]: 5
arr[col 4][row 1]: 4
arr[col 4][row 2]: 3
arr[col 3][row 3]: 4
arr[col 4][row 0]: 5
arr[col 4][row 1]: 6
arr[col 4][row 2]: 7
arr[col 7][row 3]: 8 ===this part is not right. it supposed to be arr[col 4][row 3]: 8


the output will actually have to look like this...
Input array values:
arr[col 0][row 0]: 22
arr[col 0][row 1]: 33
arr[col 0][row 2]: 44
arr[col 1][row 0]: 55
arr[col 1][row 1]: 66
arr[col 1][row 2]: 77
arr[col 2][row 0]: 88
arr[col 2][row 1]: 99
arr[col 2][row 2]: 23


This is my purpose for the second nested loop.
array values
22 33 44
55 66 77
88 99 23

What I have tried:

I've tried searching in google what might be wrong with this but I got none since this code is not copied from the net.
Posted
Updated 27-Oct-21 6:31am
v3
Comments
Dave Kreskowiak 27-Oct-21 9:12am    
You're going to have to define what you mean by "this part is not right". What is this code supposed to do and what isn't it doing?

I don't know if this is the problem or not, but look at your two nested loops. The first one indexes by col (0-5), then row (0-4). The second one indexes by row (0-4), then col (0-5). This is confusing and incorrect. If you're not going to use a debugger to figure out why your code doesn't work, you need to read it very carefully when executing it in your head.
 
Share this answer
 
v2
Comments
Member 15356357 27-Oct-21 9:30am    
actually, the second nested loop is just for printing all array values that are from the first nested loop; the values that came from the user displayed in a column-major order. it has to look like this

Input array values
arr[0][0]: 22
arr[0][1]: 33
arr[0][2]: 44
arr[1][0]: 55
arr[1][1]: 66
arr[1][2]: 77
arr[2][0]: 88
arr[2][1]: 99
arr[2][2]: 23


array values
22 33 44
55 66 77
88 99 23

Process returned 10 (0xA) execution time : 9.402 s
Press any key to continue.
Greg Utas 27-Oct-21 9:36am    
Yes, but look at the code carefully. The first nested loop indexes arr[0-4][0-3] and the second one indexes arr[0-3][0-4]. One of them is wrong and will access memory that is outside the array.
Quote:
What's wrong with my code?

Your concept of array !
In a 2d array, sizes are Rows and cols in this order !
C++
int arr[4][5]; // arr is 4 rows and 5 cols
    int row, col;

    printf("Input array values \n");
    for(col=0;col<5;col++) // loop for col is set accordingly to array cols
    {
       for(row=0;row<4;row++) // loop for row is set accordingly to array rowscols
       {
           printf("arr[col %d][ row %d]: ",col,row);
           scanf("%d", &arr[col][row]); // Here you have swapped row and col to access the array
       }
    }
 
Share this answer
 
Comments
Member 15356357 27-Oct-21 22:58pm    
Thank you very much po
In my opinion, you have two things wrong. First is the use of literal values. Those should be constants or definitions. The second is the logic error mentioned previously. Here is how you can fix things.
C++
#define COUNT_ROWS 4
#define COUNT_COLS 5

void main()
{
    int arr[COUNT_ROWS][COUNT_COLS];
    int row, col;

    printf("Input array values \n");
    for(row=0;row<COUNT_ROWS;row++)
    {
       for(col=0;col<COUNT_COLS;col++)
       {
           printf("array[row %d][col %d]: ",row,col);
           scanf("%d", &arr[row][col] );
       }
    }
    // more code here
}
This fixes the logic error explained previously. The bottom line is the input and output loops should be the same and I think it is best to loop by row on the outside loop and by column on the inside loop. This is how the code I showed you is doing it and how you did it on your output loop.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900