Sorting the arrays of a 2d array between them - arrays

I have a 2d array and i want to sort the 1d arrays in descending order. I dont think i can properly explain so here's an example.
I want to turn this:
3 5 6 7 8
1 2 4 9 10
1 3 5 6 10
Into this:
1 2 4 9 10
1 3 5 6 10
3 5 6 7 8
but i just cant get it right. Any help is appreciated, thanks in advance.
intAlloc function just does a malloc and a check for if the allocation went right.
cpyArray coppies whatever is on the second arguement to the first and ANA is the number of elements the array in the first arguement has.
int **sortArray2(int **arr,int r,int c)
{
int k,i,j,tmp;
int *ptmp;
ptmp = intAlloc(ANA);
//this part is just for sorting the numbers in each 1d array
for(i=0; i<r; i++)
{
for(j=0; j<c-1; j++)
{
for(k=j+1; k<c; k++)
{
if(*(*(arr + i) + j) > *(*(arr + i)+ k))
{
tmp = *(*(arr + i) + j);
*(*(arr + i) + j) = *(*(arr + i)+ k);
*(*(arr + i)+ k) = tmp;
}
}
}
}
//this is the part where im trying to do what i explain
for(i=0; i<r-1; i++)
{
for(j=0; j<c; j++)
{
if(*(*(arr + i) + j) > *(*(arr + i+1)+ j))
{
cpyArray(ptmp, *(arr + i), ANA);
cpyArray(*(arr + i), *(arr + i+1), ANA);
cpyArray(*(arr + i),ptmp ,ANA);
break;
}
}
}
free(ptmp);
return arr;
}

Related

Display Array Items in C

I am having trouble printing out the values stored in the array. It seems to be printing out the memory address instead. Here is my code:
#include <stdio.h>
int main() {
int a[4];
int sum[4];
printf ("Record the scores of the teams according to A:B, A:C, A:D, B:C, B:D and C:D.\n");
for (int i = 0; i < 3; i++) {
for (int j = 1; i + j < 4; j++) {
scanf ("%d", &a[i]);
scanf ("%d", &a[i + j]);
sum[i] = sum[i] + a[i];
sum[i + j] = sum[i + j] + a[i + j];
printf ("%d\n", sum[i + j]);
}
}
for (int i = 0; i < 4; ++i)
printf ("%d\n", sum[i]);
return 0;
}
The output should be:
Record the scores of the teams according to A:B, A:C, A:D, B:C, B:D and C:D.
1 1
1 1
1 1
1 1
1 1
1 1
3
3
3
3
Instead, it shows:
Record the scores of the teams according to A:B, A:C, A:D, B:C, B:D and C:D.
1 1
1 1
1 1
1 1
1 1
1 1
3
3
\-1646415677
21995
How should I solve this?
I want to know how to display what I have entered
Yoi have uninitialized variables. Fix:
int a[4] = { 0 };
int sum[4] = { 0 };
Single zero is enough, because C will have the rest initialized to 0 when you initialize just 1 element of a struct or an array.

Drawing with matrices in C

How can I draw the function x+y=n with n not greater than 10 and the coordinate system using only matrices without any functions or libraries? I am a beginner and I hope you could help me.
#include<stdio.h>
int main() {
int n,i,j;
scanf("%d", &n);
char m [12][63]={" "};
for(i=0;i<12;i++){
for(j=0;j<63;j++){
m[i][j]=' ';
if(i==0 && j==1) m[i][j]='0';
if(i==11 && j==61) m[i][j]='2';
if(j==0 && i!=0 && i!=11) m[i][j]='0'+10-i;
if(j==2 && i!=11) m[i][j]='+';
if(i==11 && j%3==2) m[i][j]='0'+((j-2)/3)%10;
if(i==11 && j%3==1 && j>29 && j<59) m[i][j]='1';
if(i==10 && j%3==2) m[i][j]='+';
if(j==3*(n+i-10) && i>2 && j>2) m[i][j]='*';
}
}
for(i=0;i<12;i++){
for(j=0;j<63;j++){
printf("%c", m[i][j]);
} printf("\n");
}
}
//my code doesn't print the correct result(the coordinate system is correct except that it prints 0+ instead of 10+ on y axis and x+y=n is not correct
0+
9 +
8 +
7 +
6 +
5 +
4 +
3 +
2 +
1 +*
0 + +* + + + + + + + + + + + + + + + + + + +
0 1 2* 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
I have no idea of what you were trying with those conditions. Check this one
#include <stdio.h>
#include <string.h>
#define N 100
int main() {
int n;
char m[N][N];
while( scanf("%d", &n) != 1 && n > (N-1)/2 );
memset(m, ' ', N*N);
for(int i = 0; i < n-1; i++) {
m[i][0] = '*';
m[i][i] = '*';
}
for(int i = 0; i < (n-1)*2; i++) m[n-1][i] = '*';
for(int i = 0; i < n; i++) {
for(int j = 0; j < (n-1)*2; j++) printf("%c", m[i][j]);
puts("");
}
return 0;
}
The program could be improved, it wastes a lot of memory and prints a lot of unnecessary blank spaces. Try to improve it by reducing the unused portion of the matrix. In this way you will also reduce the unnecessary prints.

Sorting a 2D Array c++

I am bubble sorting a 2D array that looks like this. I am confuse on how to make my largest value as 1 and make the 2nd row's value follow to 1st row's counterpart.
Input:
13 9 1 8 5
1 2 3 4 1
Actual output:
1 5 8 9 13
1 2 3 4 1
This is the expected output that i am trying to make.
Output:
5 8 9 13 1
1 4 2 1 1
Here is my code for sorting the cards (col = 5 and row = 2):
void sortedCards(int card[][col])
{
int i, j, k, temp;
printf("\n\nSorted Cards\n");
for (k = 0; k < 10; k++)
{
for (i = 0; i < row - 1; i++)
{
for (j = 0; j < col - 1; j++)
{
if (card[i][j] > card[i][j + 1])
{
temp = card[i][j];
card[i][j] = card[i][j + 1];
card[i][j + 1] = temp;
}
}
}
}
for (i = 0; i < row; i++)
{
if (i == 1)
{
printf("\n");
}
for (j = 0; j < col; j++)
{
printf("%i ", card[i][j]);
}
}
}
If your sorting is only dependent on the first row, there is no need to iterate through the second row. Just set both rows at the same time while checking the first row.
Also, if you want 1 to be treated as larger than all other numbers, you need to add that to your Boolean logic. Adjusting your for loop like below should do it.
int j, k, temp, temp2;
for (k = 0; k < 10; k++)
{
for (j = 0; j < col-1; j++)
{
//here we only test row 0, and we check if the value is 1
if (card[0][j] == 1 || (card[0][j] > card[0][j+1] && card[0][j+1] != 1))
{
//all other reassignment is the same but you do both rows at the same time
temp = card[0][j];
temp2 = card[1][j];
card[0][j] = card[0][j + 1];
card[1][j] = card[1][j + 1];
card[0][j + 1] = temp;
card[1][j + 1] = temp2;
}
}
}

matrix rotation..for loop changes the value of pointer

In the final value of array only first element becomes zero and that too when it again goes to the for loop(checked using gdb)..i have mentioned the problem using comments at the bottom of code.Help me out.. I have no clue of what is going wrong.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a, b, c;
printf("enter the size of matrix");
scanf("%d%d",&a,&b);
printf("enter the number of rotations");
scanf("%d",&c);
int *arr = malloc (sizeof(int) * a * b);
int x = (a >= b)? a : b;
printf("enter the values of matrix");
// scanning the values
for(int i = 0; i < a; i++)
{
for(int j = 0; j < b; j++)
{
scanf("%d",(arr + i * b + j));
}
printf("\n");
}
// main code starts
for(int y = 0; y < c; y++)
{
// declared a new array
int *arr1 = malloc (sizeof(int) * a * b);
for(int k = 0; k < x / 2; k++)
{
for(int i = k; i < a - k; i++)
{
for(int j = k; j < b - k; j++)
{
if (i == k && j > k)
{
*(arr1 + i * b + j - 1) = *(arr + i * b + j);
}
else if (i == a - k - 1 && j < b - k - 1)
{
*(arr1 + i * b + j + 1) = *(arr + i * b + j);
}
else if (j == k && i < a - k - 1)
{
*(arr1 + i * b + j + b) = *(arr + i * b + j);
}
else if (j == b - k - 1 && i > k)
{
*(arr1 + i * b + j - b) = *(arr + i * b + j);
}
}
}
if (x % 2 != 0 && a == b)
*(arr1 + x / 2 * b + (b / 2)) = *(arr + x / 2 * b + (b / 2));
}
// changing the old array to new array
arr = arr1;
// first value is getting printed correctly here
printf("%d\n",*(arr));
printf("%p\n",&(*arr));
free(arr1);
}
// printing the output
for(int i = 0; i < a; i++)
{
for(int j = 0; j < b; j++)
{
printf("%d ",*(arr + i * b + j));
}
printf("\n");
}
// first value is getting printed incorrectly here, outside the loop
printf("\n%d\n",*(arr));
printf("%p",&(*arr));
}
C doesn't support array assignment. You have:
int *arr = malloc (sizeof(int) * a * b);
…
int *arr1 = malloc (sizeof(int) * a * b);
…
arr = arr1;
…
free(arr1);
The assignment means you've lost your original array (memory leak) and you then invalidate your new array with the free().
Array copying requires more code — usually a function call such as memmove() or memcpy(), possibly wrapped in a function.
For example, add #include <string.h> and use this in place of the arr = arr1; assignment:
memmove(arr, arr1, sizeof(int) * a * b);
free(arr1); // No longer needed
Alternatively:
free(arr);
arr = arr1;
This code runs cleanly under valgrind on Mac OS X 10.11.5 with GCC 6.1.0 with the 'Either' or the 'Or' options for handling the array assignments.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void dump_matrix(const char *tag, int *arr, int a, int b)
{
printf("Matrix: %s\n", tag);
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
printf(" %3d", arr[i * b + j]);
putchar('\n');
}
}
int main(void)
{
int a, b, c;
printf("enter the size of matrix: ");
scanf("%d%d", &a, &b);
printf("enter the number of rotations: ");
scanf("%d", &c);
int *arr = malloc(sizeof(int) * a * b);
int x = (a >= b) ? a : b;
printf("enter the values of matrix: ");
// scanning the values
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
if (scanf("%d", (arr + i * b + j)) != 1)
{
fprintf(stderr, "failed to read value arr[%d][%d]\n", i, j);
return EXIT_FAILURE;
}
}
printf("\n");
}
dump_matrix("Initial input", arr, a, b);
// main code starts
for (int y = 0; y < c; y++)
{
// declared a new array
int *arr1 = malloc(sizeof(int) * a * b);
for (int k = 0; k < x / 2; k++)
{
for (int i = k; i < a - k; i++)
{
for (int j = k; j < b - k; j++)
{
if (i == k && j > k)
{
*(arr1 + i * b + j - 1) = *(arr + i * b + j);
}
else if (i == a - k - 1 && j < b - k - 1)
{
*(arr1 + i * b + j + 1) = *(arr + i * b + j);
}
else if (j == k && i < a - k - 1)
{
*(arr1 + i * b + j + b) = *(arr + i * b + j);
}
else if (j == b - k - 1 && i > k)
{
*(arr1 + i * b + j - b) = *(arr + i * b + j);
}
}
}
if (x % 2 != 0 && a == b)
*(arr1 + x / 2 * b + (b / 2)) = *(arr + x / 2 * b + (b / 2));
}
// Changing the old array to new array
// Either:
// memmove(arr, arr1, sizeof(int) * a * b);
// free(arr1);
// Or:
free(arr);
arr = arr1;
dump_matrix("After rotation", arr, a, b);
}
dump_matrix("Finished", arr, a, b);
free(arr);
return 0;
}
Note the use of the dump_matrix() function. Writing such a function once means it can be used multiple places in the code. The tag argument simplifies the use. The 'commercial grade' variant takes a FILE *fp argument too and writes to the specified file stream.
Note the error checking on the main input loop scanf(). I should also have checked the two other scanf() statements. Errors are reported on standard error, of course.
Example run:
$ ./mat31
enter the size of matrix: 3 4
enter the number of rotations: 2
enter the values of matrix: 1 2 3 4 10 11 12 13 99 98 97 96
Matrix: Initial input
1 2 3 4
10 11 12 13
99 98 97 96
Matrix: After rotation
2 3 4 13
1 12 11 96
10 99 98 97
Matrix: After rotation
3 4 13 96
2 11 12 97
1 10 99 98
Matrix: Finished
3 4 13 96
2 11 12 97
1 10 99 98
$
Whether the output is what you intended is a wholly separate discussion. This is simply not abusing the memory.

how to sort arrays that pointed by pointers array?

I need to sort arrays that pointed by pointers array
with this function (i need only with pointers, without these [ ])
void getSort(int** p2a, int arrSizes[]) //p2a is a array of pointers to diffrent arrays
I tried bubble sort:
if (i < 5)
{
for (j = 1; j < arrSizes[i]; j++) // I started with 1 bcz i dont want to sort the first value
{
for (k = j + 1; k < arrSizes[i]; k++)
{
if (*(*(p2a + i) + j) > *(*(p2a + i) + k))
{
temp = *(*(p2a + i) + j);
*(*(p2a + i) + j) = *(*(p2a + i) + k);
*(*(p2a + i) + k) = temp;
}
}
i++;
}
}
but it not sorting well...
exampels:
Before: 3 9 3 7
After: 3 3 9 7
Before: 3 6 1 7
After: 3 1 6 7
Before: 4 9 7 1 7
After: 4 9 7 1 7
change the if with for solved the problem
for (i = 0; i < NUM; i++)
{
for (j = 1; j < arrSizes[i]; j++)
{
for (k = j + 1; k < arrSizes[i]; k++)
{
if (*(*(p2a + i) + j) > *(*(p2a + i) + k))
{
temp = *(*(p2a + i) + j);
*(*(p2a + i) + j) = *(*(p2a + i) + k);
*(*(p2a + i) + k) = temp;
}
}
}
}

Resources