Reputation: 11
My task is to rearrange the rows of an matrix given two simple rules using C. 1. If the number in the last column is positive the row should be moved to the top. 2. If the number in the last column is zero the row should be moved to the bottom.
For example if I have {{1,2,0},{4,5,-6},{7,8,9},{1,1,1}} I would get {{1,1,1},{7,8,9},{4,5,-6},{1,2,0}}.
To do this I have implemented a matrix as a singlelinked list where the nodes represents the rows of the matrix. Then I have created a function called "rearrange" which should do the task for me. I have been able to solve the problem for all cases except when the number in the last column of the first row is 0. Then all i get is 0. That is for example {{0}{2}{3}} or {{1, 2, 0},{4, 5, 6}{7,8,9}} both give 0. I would be very have if someone could take a look at my code and maybe give me som input. Here is my code:
typedef struct node node;
typedef struct list list;
struct list{
node* first;
node* last;
};
struct node{
int* data;
node* next;
};
list* newList(int* data);
static node* newNode(int* data);
list* newList(int* data);
void insertFirst(list* head, int* data);
void insertLast(list* head, int* data);
void rearrange(list* head,int rows, int cols);
void printList(list* head,int cols);
int main(){
int a[1] = {0};
int b[1] = {-2};
int c[1] = {-3};
//int d[1] = {-4}; // if we want more rows
//int e[1] = {-5};
//Create empty list
list* head = newList(a);
//Add rows
insertLast(head,b);
insertLast(head,c);
//insertLast(head,d);
//insertLast(head,e);
rearrange(head,3,1);
//Print all elements
printList(head,1);
return 0;
}
void printList(list* head,int cols){
int i;
node* currentEl = head->first;
while(currentEl != NULL) {
for(i=0;i<cols;i++){
printf("%d, ",currentEl->data[i]);
}
printf("\n");
currentEl = currentEl->next;
}
}
void rearrange(list* head,int rows,int cols){
head->last->next = head->first;
node* currentEl = head->last;
node* nextEl = head->first;
node* temp;
int count = 1;
while(count != rows+1){
int a = nextEl->data[cols-1];
temp = nextEl->next;
if(a>0 && count != 1){
nextEl->next = head->first;
head->first = nextEl;
currentEl->next = temp;
}else if(a==0 && head->first->data[cols-1] != 0){
head->last->next = nextEl;
head->last = nextEl;
nextEl->next = NULL;
currentEl->next = temp;
}else if(a==0 && head->first->data[cols-1] == 0){ // this needs to be changed
head->last->next = nextEl;
head->last = nextEl;
nextEl->next = NULL;
currentEl->next = temp;
}else {
currentEl = nextEl;
}
//nextEl = nextEl->next;
nextEl = currentEl->next;
if(count == 1){
head->last->next = NULL;
}
count++;
}
}
void insertFirst(list* head, int* data){
node* temp;
temp = newNode(data);
temp->next = head->first;
head->first = temp;
}
void insertLast(list* head, int* data){
node* temp;
temp = newNode(data);
head->last->next = temp;
head->last = temp;
}
static node* newNode(int* data){
node* new;
new = (node*)malloc(sizeof(node));
new->next = NULL;
new->data = data;
return new;
}
list* newList(int* data){
list* head;
node* node = newNode(data);
head = (list*)malloc(sizeof(list));
head->first = node;
head->last = node;
return head;
}
Upvotes: 1
Views: 125
Reputation: 9950
Not sure if this is what you need, but the code below will turn:
{{1,2,0},{4,5,-6},{7,8,9},{1,1,1}}
into {{1,1,1},{7,8,9},{4,5,-6},{1,2,0}}
However you'd need to convert this C# code to C, and use a 1D array instead of a 2D array. Ie instead of {{1,2,0},{4,5,-6},{7,8,9},{1,1,1}}
, you would just have {1,2,0,4,5,-6,7,8,9,1,1,1}
.
The elementsPerGroup
in your case is 3.
private static byte[] ReverseArray(int elementsPerGroup, byte[] forwardsArray)
{
int length = forwardsArray.Length;
byte[] reversedArray = new byte[length];
int groupIdentifier = 0;
for (int i = 0; i < length; i++)
{
if (i != 0 && i % elementsPerGroup == 0)
{
groupIdentifier += 2 * elementsPerGroup;
}
int index = length - elementsPerGroup - groupIdentifier + i;
reversedArray[i] = forwardsArray[index];
}
return reversedArray;
}
Upvotes: 0