Reputation: 11
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNING
#endif
#include <stdio.h>
#include <stdlib.h>
void bubbleSort(int matrix[9], int n);
int main(void) {
int matrix[9];
int n = 9;
printf("enter 10 values to the matirx \n");
for (int i = 0; i < 10; i++) {
printf("now %dth componet\n ", i+1);
scanf("%d", &matrix[i]);
}
bubbleSort(matrix[9], n);
return 0;
}
void bubbleSort(int matrix [9], int n) {
//bubble sort the given matrix
int temp = 0;
for (int i=n-1; i > 0; i--) {
// compare two values at j and j+1 and move left when j+1 is smaller than j
for (int j = 0; j < i; j++) {
if (matrix[j] > matrix[j + 1]) {
temp = matrix[j];
matrix[j] = matrix[j + 1];
matrix[j + 1] = temp;
}
}
printf("Check the matrix \n");
for (int i = 0; i < 9; i++) {
printf("%d ", matrix[i]);
}
printf("\n");
}
}
Hi, I am getting a read access violation error at
if (matrix[j] > matrix[j + 1]) {
**temp = matrix[j];**
matrix[j] = matrix[j + 1];
matrix[j + 1] = temp;
}
This part of the code. The code builds right, but when I run the program I get an error. Can anyone help me out to figure out the issue? I searched up a little bit and based on that I assume it has something to do with the pointer but I do not know why there would be an issue with a pointer since I never used it in my code.
Upvotes: 0
Views: 55
Reputation: 311088
For starters you declared an array that contains only 9
elements.
int matrix[9];
On the other hand, you are trying to enter 10
elements.
printf("enter 10 values to the matirx \n");
for (int i = 0; i < 10; i++) {
printf("now %dth componet\n ", i+1);
scanf("%d", &matrix[i]);
}
So the program already has undefined behavior.
In this function declaration
void bubbleSort(int matrix[9], int n);
the magic number 9
used in the declaration of the first parameter does not make a great sense. Just write
void bubbleSort(int matrix[], int n);
or
void bubbleSort(int *matrix, int n);
In this call
bubbleSort(matrix[9], n);
instead of passing the array matrix
like
bubbleSort(matrix, n);
you are passing the non-existent element of the array matrix[9]
. The compiler should issue a message that you are trying to convert an integer to a pointer.
Within the function you are using the magic number 9
instead of the parameter n
in this loop
for (int i = 0; i < 9; i++) {
Pay attention to that it looks strange when a one-dimensional array is named matrix.
Using your approach the program can look the following way.
#include <stdio.h>
void bubbleSort( int matrix[], size_t n )
{
if ( n )
{
for ( size_t i = n - 1; i != 0; i-- )
{
// compare two values at j and j+1 and move left when j+1 is smaller than j
for ( size_t j = 0; j < i; j++ )
{
if ( matrix[j + 1] < matrix[j] )
{
int temp = matrix[j];
matrix[j] = matrix[j + 1];
matrix[j + 1] = temp;
}
}
}
}
}
int main(void)
{
enum { N = 9 };
int matrix[N];
printf( "enter %d values to the matirx \n", N );
for ( int i = 0; i < N; i++ )
{
printf( "now %dth componet\n", i+1 );
scanf( "%d", matrix + i );
}
bubbleSort( matrix, N );
for ( int i = 0; i < N; i++ )
{
printf( "%d", matrix[i] );
}
putchar( '\n' );
return 0;
}
The program output might look
enter 9 values to the matirx
now 1th componet
9
now 2th componet
8
now 3th componet
7
now 4th componet
6
now 5th componet
5
now 6th componet
4
now 7th componet
3
now 8th componet
2
now 9th componet
1
1 2 3 4 5 6 7 8 9
Upvotes: 4
Reputation: 882326
bubbleSort(matrix[9], n);
This passes just the final element of the list (coerced into an address) rather than the actual address of the list, which is what you probably intended.
That won't end well :-)
You should just pass in matrix
.
A decent compiler should warn you of this, such as with gcc
:
prog.c: In function ‘main’:
prog.c:21:22: warning: passing argument 1 of ‘bubbleSort’
makes pointer from integer without a cast
[-Wint-conversion]
21 | bubbleSort(matrix[9], n);
| ~~~~~~^~~
| |
| int
prog.c:7:21: note: expected ‘int *’ but argument is of
type ‘int’
7 | void bubbleSort(int matrix[9], int n);
| ~~~~^~~~~~~~~
Upvotes: 2