# Thread: Need help in writing a code (assignment)

1. ## Need help in writing a code (assignment)

I need to write a code (in C99). The programe receives an input number (integer) 'n' and then a matrix sized n*n (matrix[n][n]), all numbers are integers. We're supposed to define the matrix's size with malloc.
Now the program needs to sort the lines of the matrix (the number in each line), in a single function (!) this way:
even line index (0,2,4,6...): from small to big.
odds line index (1,3,5...): from big to small.
and then print it.
*note: first line is indexed 0, second line is 1, etc.

I was thinking to sort it with bubblesort function with the following if:
if(i%2==1)
do odds sorting.
else
do even sorting.
when i is the index of the row.

my problem is defining the malloc and how do I send the matrix to sorting.

If needed I will attach my current (not so good (completly awful)) code and functions as well as an example of what the prog. supposed to do.

*sorry for grammer.

2. Originally Posted by Akruze
If needed I will attach my current (not so good (completly awful)) code and functions as well as an example of what the prog. supposed to do.
Do that, plz. Don't forget to use code tags.

3. Originally Posted by GReaper
Do that, plz. Don't forget to use code tags.

Code:
```void swap(int *p, int *q){
int temp=*p;
*p=*q;
*q=temp;
}

int bubble(int a[][], int m)
{
int swapped=0;
for (int i=0; i<m; i++)
{
if ((i%2)==0)
{
for (int left=0; left<m-1; left++)
{
int right=left+1;
if (a[i][left]>a[i][right])
{
swap (a+left, a+right);
swapped++;
}
}
}
if ((i%2)==1)
{
for (int left=0; left<m-1; left++)
{
int right=left+1;
if (a[i][left]<a[i][right])
{
swap (a+left, a+right);
swapped++;
}
}
}

}
return swapped;
}

void bubblesort (int a[][], int n)
{
for (int last=n-1; last>0; last--)
{
if (bubble (a, last+1)==0)
break;
}
}

int main()
{
int n=0;
scanf(" %d", &n);
int matrix[n][n];
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
scanf(" %d", &matrix[i][j]);
}
}
bubblesort(matrix, n);
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
printf("%d ", matrix[i][j]);
}
}

return 0;
}```
I think my problem is how I send the matrix to the func. I think I need to seperate it into one dimensinal arrays and sending each individualy to sorting by the row index.

4. The one dimensional arrays you need, are already there - waiting for you. In C, a two D array has 2 (maybe more) one dimensional arrays, inside it. One for each row.

With a[n][n], a[0], is one one dimension array (the first row). a[1] is another, etc.

This looks like a problem from SPOJ.

The one dimensional arrays you need, are already there - waiting for you. In C, a two D array has 2 (maybe more) one dimensional arrays, inside it. One for each row.

With a[n][n], a[0], is one one dimension array (the first row). a[1] is another, etc.

This looks like a problem from SPOJ.
I've modified my code:
Code:
```#include <stdio.h>#include <stdlib.h>

void bubble(int m, int a[m], int i)
{
int c=0;
if(m<=1) return;
bubble(m-1, a, i);
if ((i%2)==0)
{
for (int left=0; left<m-1; left++)
{
int right=left+1;
if (a[left]>a[right])
{
c=a[left];
a[left]=a[right];
a[right]=c;
}
}
}
else
{
for (int left=0; left<m-1; left++)
{
int right=left+1;
if (a[left]<a[right])
{
c=a[left];
a[left]=a[right];
a[right]=c;
}
}
}

}

int main()
{
int n=0;
scanf(" %d", &n);
int matrix[n][n];
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
scanf(" %d", &matrix[i][j]);
}
}
for (int i=0;i<n;i++)
{
bubble(n, matrix+i*n,i);
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}

return 0;
}```
now it says:
Code:
```In function 'main':|53|error: passing argument 2 of 'bubble' from incompatible pointer type|
|4|note: expected 'int *' but argument is of type 'int (*)[(unsigned int)(n)]'|```
why?
I believe that how I send my array does send it as a one dimensional array to my func.
Or am I mistaken?

6. Nope. matrix is an array of arrays (which can decay to a pointer-to-array, as it has done here). A pointer-to-array is not the same as an array. matrix[0] is a one-D array. matrix[i] is a one-D array. matrix is not, and matrix+i*n is not only not an array, it probably doesn't point to valid memory.

7. Originally Posted by tabstop
Nope. matrix is an array of arrays (which can decay to a pointer-to-array, as it has done here). A pointer-to-array is not the same as an array. matrix[0] is a one-D array. matrix[i] is a one-D array. matrix is not, and matrix+i*n is not only not an array, it probably doesn't point to valid memory.
So can I send different rows to my func as it is now with this?:
Code:
```for (int i=0;i<n;i++)
{
bubble(n, matrix[i], i)
}```
considering matrix is a 2D array?

8. Yes you can send rows to the bubble() function like that. Note that you pass matrix[i] which is an entire row. Therefore matrix[i] must be a pointer to an allocated area that holds a row's worth of integers.

You haven't defined the matrix with malloc yet - that's the first thing.

9. You don't need to use malloc, at all. This is a static array.

Your sort code is the first thing - it's a Substitution sort, not a Bubble sort. While these two sort algorithms are very close to each other, here's the difference: the Bubble sort ALWAYS compares adjacent values, as it sorts. The Substitution sort STARTS comparing just one adjacent value, and then compares only non-adjacent values, as it proceeds to sort.

Your call to Bubble sort should be:
Code:
```    for (int i=0;i<n;i++)
{
bubble(n,i,matrix[i]);   //i goes first, before matrix[i]
}```
The function's first line should be:
Code:
`void bubble(int n, int i, int a[i])`
If int i, is not listed before a[i], you should receive an error or warning, from your compiler.

This is a correct Substitution sort:
Code:
```        for (left=0; left<n-1; left++)   //Substitution sort
{

for(right=left+1;right<n;right++) //starts with 1 comparison to
{
c=a[left];                    //then begins comparing non
a[right]=c;
}
}```
and this is a correct Bubble sort:
Code:
```     do {
swapped = 0;
for(left =0; left < n-1; left++) {  //Bubble sort
if(a[left] < a[left + 1 ]) {      //always compares adjacent values
c=a[left];
a[left]=a[left+1];
a[left+1]=c;

swapped = 1;
}
} //end for
--n;                               //a reasonable optimization
}while(swapped);               //for a small quantity sorter, only.```
Both of these sorting algorithms are good for small quantities, only. Don't even THINK about using them for a large quantity sort - they fall right on their faces.

10. Originally Posted by nonoob
Yes you can send rows to the bubble() function like that. Note that you pass matrix[i] which is an entire row. Therefore matrix[i] must be a pointer to an allocated area that holds a row's worth of integers.

You haven't defined the matrix with malloc yet - that's the first thing.
You don't need to use malloc, at all. This is a static array.

Your sort code is the first thing - it's a Substitution sort, not a Bubble sort. While these two sort algorithms are very close to each other, here's the difference: the Bubble sort ALWAYS compares adjacent values, as it sorts. The Substitution sort STARTS comparing just one adjacent value, and then compares only non-adjacent values, as it proceeds to sort.

Your call to Bubble sort should be:
Code:
```    for (int i=0;i<n;i++)
{
bubble(n,i,matrix[i]);   //i goes first, before matrix[i]
}```
The function's first line should be:
Code:
`void bubble(int n, int i, int a[i])`
If int i, is not listed before a[i], you should receive an error or warning, from your compiler.

This is a correct Substitution sort:
Code:
```        for (left=0; left<n-1; left++)   //Substitution sort
{

for(right=left+1;right<n;right++) //starts with 1 comparison to
{
c=a[left];                    //then begins comparing non
a[right]=c;
}
}```
and this is a correct Bubble sort:
Code:
```     do {
swapped = 0;
for(left =0; left < n-1; left++) {  //Bubble sort
if(a[left] < a[left + 1 ]) {      //always compares adjacent values
c=a[left];
a[left]=a[left+1];
a[left+1]=c;

swapped = 1;
}
} //end for
--n;                               //a reasonable optimization
}while(swapped);               //for a small quantity sorter, only.```
Both of these sorting algorithms are good for small quantities, only. Don't even THINK about using them for a large quantity sort - they fall right on their faces.
Thank you both so much!!!! It seems I didn't really understood few things with func. and sorting, you cleared some of my misunderstandings, thank you!!!
BTW, so I won't have to bother this forum with stupid questions so much, can you recommand me on a site (or anything of the sort) that can help me improve in C? A site that has theoretical material (this part is not really critical as this->) and assigments (pref. with solutions)?