I am trying to make this program, but it is not working, its failing in attributing values for the second line of the aux matrix, and i cant see why, can anyone help me? Thanks!
Oh, and I have already putted some debugging lines, and, apparently, everything is fine, it's just not attributing the values.
#include <stdio.h>
#include <math.h>
void zerar(int n,int m[][n]) {
int i, j;
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
m[i][j] = 0;
}
}
}
void printm(int n, int matriz[][n]) {
int i,j;
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
printf("\t%d", matriz[i][j]);
}
printf("\n");
}
}
int det(int n, int m[][n]) {
int i, j, k, x, y, soma=0, aux[n][n];
zerar(n, aux);
if (n < 1) {}
else if (n == 1) {
return m[0][0];
}
else if (n == 2) {
soma = (m[0][0] * m[1][1]) - (m[0][1] * m[1][0]);
return soma;
}
else {
for (i=0; i<n; i++) {
for (j=1, x=0; j<n; j++) {
for (k=0, y=0; k<n; k++) {
if (k == i) {
continue;
}
else {
printf("\n\n");
printf("\nx=%d, y=%d, j=%d, k=%d, i=%d\n", x, y, j, k, i);
aux[x][y] = m[j][k];
printm(n-1, aux);
y++;
}
}
x++;
}
soma += m[0][i]*pow(-1, i+2)*det(n-1, aux);
}
return soma;
}
}
int main()
{
int m[3][3] = {{4, 3, 2}, {1, 4, 5}, {2, 1, 2}};
det(3, m);
printf("%d", det(3, m));
printf("\n\n");
printm(3, m);
printf("\n\n");
}
When the input matrix is of size n x n, the size of the auxiliary matrix, aux, needs to be n-1 x n-1.
Change
int i, j, k, x, y, soma=0, aux[n][n];
to
int i, j, k, x, y, soma=0, aux[n-1][n-1];
You said in a comment:
Can someone explain me please why it have to be that way?
If you use aux[n][n] and n is 3, the memory layout of the object is:
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
and you fill up the data as though it is a 2 x 2 matrix.
0 1 2
+---+---+---+---+---+---+---+---+---+
| x | x | | x | x | | | | |
+---+---+---+---+---+---+---+---+---+
In the next recursive call, you treat that memory as though it is a 2 x 2 array.
0 1
+---+---+---+---+---+---+---+---+---+
| x | x | | x | x | | | | |
+---+---+---+---+---+---+---+---+---+
^ ^
| Ignored
Using uninitialized array element
In theory, the program is subject to undefined behavior if you use aux[n][n] instead of aux[n-1][n-1].
Related
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX_ELEMENTS 1000
#define OFFSET 100
#define MAX_LINES 10
int a[MAX_ELEMENTS] ;
int comp, swap ;
void quicksort(int [],int ,int);
void init_step(void);
void print_step(int);
void print_header(char *);
int sorted(int);
void swap1(int*,int*);
int main(void){
int i;
int n;
srandom(time(0));
print_header("random");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET) {
init_step();
for (i = 0; i < n; i++) {
a[i] = random() % n ;
}
quicksort(a,0,n-1);
print_step(n);
}
print_header("ascending order");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET) {
init_step();
for (i = 0; i < n; i++) {
a[i] = i ;
}
quicksort(a,0,n-1);
print_step(n);
}
print_header("descending order");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET) {
init_step();
for (i = 0; i < n; i++) {
a[i] = n - i ;
}
quicksort(a,0,n-1);
print_step(n);
}
return 0;
}
void init_step(void){
swap = 0; comp = 0;
}
void print_header(char *s) {
printf("%s\n n, comparison, swap, check", s);
printf("\n");
}
void print_step(int n){
printf("%4d, %8d, %8d", n, comp, swap);
if (sorted(n)) {
printf(", sorted\n");
} else {
printf(", unsorted\n");
}
}
int sorted(int n) {
int i;
for (i=0; i < n-1; i++)
if (a[i] > a[i+1]) return 0;
return 1;
}
void swap1(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
swap++;
}
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
comp++;
while(number[j]>number[pivot])
j--;
comp++;
if(i<j){
swap1(&number[i], &number[j]);
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
so i have this code to count comparison and swap of quicksort at c max element means the maximum data and offset means Incremental value of the number of data.
the one i want to ask is for the random case is count perfectly but for descending and ascending case the value of the comparison and swap is all the same. and swap values is always 0
to be more specific i will post the result below
random
| n |comparison| swap| check|
|:---: |:--------:|:---:|:----:|
|100 | 316 | 89 |sorted|
|200 | 756 | 244 |sorted|
|300 | 1156 | 375 |sorted|
|400 | 1630 | 552 |sorted|
|500 | 2164 | 745 |sorted|
|600 | 2682 | 932 |sorted|
|700 | 3202 |1125 |sorted|
|800 | 3776 |1351 |sorted|
|900 | 4286 |1539 |sorted|
|1000 | 4732 |1678 |sorted|
ascending order
| n |comparison| swap| check |
|:---:|:--------:|:---:|:-----:|
|100 | 198 | 0 | sorted|
|200 | 398 | 0 | sorted|
|300 | 598 | 0 | sorted|
|400 | 798 | 0 | sorted|
|500 | 998 | 0 | sorted|
|600 | 1198 | 0 | sorted|
|700 | 1398 | 0 | sorted|
|800 | 1598 | 0 | sorted|
|900 | 1798 | 0 | sorted|
|1000 | 1998 | 0 | sorted|
descending order
| n |comparison| swap| check |
|:---:|:--------:|:---:|:-----:|
|100 | 198 | 0 | sorted|
|200 | 398 | 0 | sorted|
|300 | 598 | 0 | sorted|
|400 | 798 | 0 | sorted|
|500 | 998 | 0 | sorted|
|600 | 1198 | 0 | sorted|
|700 | 1398 | 0 | sorted|
|800 | 1598 | 0 | sorted|
|900 | 1798 | 0 | sorted|
|1000 | 1998 | 0 | sorted|
is there any one know why my ascending and descending case show same value of comparison and swap count?
and i think for the ascending comparison count is not supposed to be this high isn't it?
You have:
temp = number[pivot];
number[pivot] = number[j];
number[j] = temp;
That looks like a swap, but you don't count it as one. You also have:
while (number[i] <= number[pivot] && i < last)
i++;
comp++;
The comp++; is outside the loop, so you are miscounting the comparisons too. That occurs twice.
With this version of your code, I get more plausible results:
/* SO 7273-8321 */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX_ELEMENTS 1000
#define OFFSET 100
#define MAX_LINES 10
int a[MAX_ELEMENTS];
int comp, swap;
void quicksort(int[], int, int);
void init_step(void);
void print_step(int);
void print_header(char *);
int sorted(int);
void swap1(int *, int *);
int main(void)
{
int i;
int n;
srandom(time(0));
print_header("random");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET)
{
init_step();
for (i = 0; i < n; i++)
{
a[i] = random() % n;
}
quicksort(a, 0, n - 1);
print_step(n);
}
print_header("ascending order");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET)
{
init_step();
for (i = 0; i < n; i++)
{
a[i] = i;
}
quicksort(a, 0, n - 1);
print_step(n);
}
print_header("descending order");
for (n = OFFSET; n <= MAX_ELEMENTS; n += OFFSET)
{
init_step();
for (i = 0; i < n; i++)
{
a[i] = n - i;
}
quicksort(a, 0, n - 1);
print_step(n);
}
return 0;
}
void init_step(void)
{
swap = 0;
comp = 0;
}
void print_header(char *s)
{
printf("%s\n n, comparison, swap, check", s);
printf("\n");
}
void print_step(int n)
{
printf("%4d, %8d, %8d", n, comp, swap);
if (sorted(n))
{
printf(", sorted\n");
}
else
{
printf(", unsorted\n");
}
}
int sorted(int n)
{
int i;
for (i = 0; i < n - 1; i++)
if (a[i] > a[i + 1])
return 0;
return 1;
}
void swap1(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
swap++;
}
void quicksort(int number[25], int first, int last)
{
int i, j, pivot, temp;
if (first < last)
{
pivot = first;
i = first;
j = last;
while (i < j)
{
while (number[i] <= number[pivot] && i < last)
{
i++;
comp++;
}
while (number[j] > number[pivot])
{
j--;
comp++;
}
if (i < j)
{
swap1(&number[i], &number[j]);
}
}
temp = number[pivot];
number[pivot] = number[j];
number[j] = temp;
swap++;
quicksort(number, first, j - 1);
quicksort(number, j + 1, last);
}
}
Example output:
random
n, comparison, swap, check
100, 644, 172, sorted
200, 1703, 367, sorted
300, 2845, 592, sorted
400, 3493, 840, sorted
500, 5020, 1094, sorted
600, 6206, 1327, sorted
700, 7279, 1617, sorted
800, 8784, 1827, sorted
900, 9342, 2143, sorted
1000, 10185, 2470, sorted
ascending order
n, comparison, swap, check
100, 5049, 99, sorted
200, 20099, 199, sorted
300, 45149, 299, sorted
400, 80199, 399, sorted
500, 125249, 499, sorted
600, 180299, 599, sorted
700, 245349, 699, sorted
800, 320399, 799, sorted
900, 405449, 899, sorted
1000, 500499, 999, sorted
descending order
n, comparison, swap, check
100, 4999, 99, sorted
200, 19999, 199, sorted
300, 44999, 299, sorted
400, 79999, 399, sorted
500, 124999, 499, sorted
600, 179999, 599, sorted
700, 244999, 699, sorted
800, 319999, 799, sorted
900, 404999, 899, sorted
1000, 499999, 999, sorted
I have created an array and some functions that print different patterns each. My problem is that they are printed inside the array in a weird way. The below is the main part of the code and just one function for the example.
int castle(int patternWidth, int doorStart, int doorEnd, int N, int i, int j, int row, int col, char** array)
{
if (N >= 3 && N <= 20)
{
for (i = 1; i <= (N + 1); i++)
{
array[row][col] = '*'; col++;
array[row][col] = ' '; col++;
}
row++;
patternWidth = (((N + 1) * 2) - 1);
doorStart = (patternWidth - 3) / 2;
doorEnd = doorStart + 3;
for (i = 0; i < N; i++)
{
for (j = 1; j <= patternWidth; j++)
{
if(N - i <= 2 && j > doorStart && j <= doorEnd)
{
array[row][col] = ' '; col++;
}
else
{
array[row][col] = '*'; col++;
}
}
row++;
}
}
return 0;
}
int main()
{
int N = 0, M = 0, i = 0, j = 0, a = 0, b = 0, s = 0, width = 0, height = 0, patternWidth = 0, doorStart = 0, doorEnd = 0, option = 0, num = 3, col = 0, row = 0;
char** array;
printf("Give height board size:");
scanf("%d", &height);
printf("Give width board size:");
scanf("%d", &width);
array = (char**)malloc(height * sizeof(char*));
for (i = 0; i < width; i++)
{
array[i] = (char*)malloc(height * sizeof(char));
}
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
array[i][j] = ' ';
}
}
while (option != 6)
{
printf("\nOption: 1-5, 6 to exit\n"
"1) Stairs and flag\n"
"2) Castle\n"
"3) Trap door\n"
"4) Platform\n"
"5) Obstacles\n"
"Option:");
scanf("%d", &option);
if (option == 1)
{
printf("Valid values 6 - 20\n");
printf("Size:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
stairs_flag(N, i, j, b, a, num, col, row, array);
}
else if (option == 2)
{
printf("Valid values 3 - 15\n");
printf("Size:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
castle(patternWidth, doorStart, doorEnd, N, i, j, row, col, array);
}
else if (option == 3)
{
printf("Valid values 3 - 18\n");
printf("Size of N:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
trap_door(N, patternWidth, i, j, col, row, array);
}
else if (option == 4)
{
printf("Valid values of N 3 - 20\n");
printf("Size of N:");
scanf("%d", &N);
printf("Valid values of M 6, 8, 10, 12, 14, 16, 18, 20\n");
printf("Size of M");
scanf("%d", &M);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
platform(N, M, i, j, col, row, array);
}
else if (option == 5)
{
printf("Valid values 2 - 10\n");
printf("Size of N:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
obstacles(N, i, s, j, patternWidth, row, col, array);
}
print_array(array, height, width);
}
free(array);
return 0;
}
The expected result and the correct one is this :
|------------------------------------------------------------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| * * * * * |
| ********* |
| ********* |
| *** *** |
| *** *** |
| |
| |
| |
| |
| |
|------------------------------------------------------------|
But the actual result is this:
|------------------------------------------------------------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| * * * * * |
| ********* |
| ********* |
| *** *** |
| *** *** |
| |
| |
| |
| |
| |
|------------------------------------------------------------|
For example, in the two arrays above, the board is 60 width and 20 height and for the pattern I gave the position 10 for both height and width. Every line of the pattern should be under the previous one and print a pattern but in reality it prints every line under the previous one but further more than the previous. How can I correct this? Is there any mistake in the function code. For every function I have done the same thing. I've put array[row][col] = '*'; col++; for the asterisks and array[row][col] = ' '; col++; for the spaces and row++; to print each line in a new line.
Thank you for your time
The problem seems to be that your column index col keeps increasing throughout the castle() function. If you want the asterisks to be aligned under each other, you need to set the column index back to its original value at the start of the for-loops.
For example, you could make it
int castle(int patternWidth, int doorStart, int doorEnd, int N, int i, int j, int row, int col, char** array)
{
int column_index = col;
if (N >= 3 && N <= 20)
{
for (i = 1; i <= (N + 1); i++)
{
array[row][col] = '*'; col++;
array[row][col] = ' '; col++;
}
row++;
patternWidth = (((N + 1) * 2) - 1);
doorStart = (patternWidth - 3) / 2;
doorEnd = doorStart + 3;
for (i = 0; i < N; i++)
{
for (j = 1; j <= patternWidth; j++)
{
col = column_index;
if(N - i <= 2 && j > doorStart && j <= doorEnd)
{
array[row][col] = ' '; col++;
}
else
{
array[row][col] = '*'; col++;
}
}
row++;
}
}
return 0;
}
where I added a command int column_index = col; which stores the original value at the start of the function, and a line col = column_index; at the start of the inner for-loop, putting the stored value back in the variable.
So I am relatively new to C and have been practicing questions on it. So I came across the question, 'Where can I go?': http://www.chegg.com/homework-help/questions-and-answers/c-coding-java-c--c-coding-bouncy-string-tasked-write-program-take-parameters-command-line--q19411844
I manage to write a part of the code, but I am not sure how to get this
| | | |
| | | |
| | | |
| |X| |
I somewhat have the logic of when the counter for width = x - 1, counter for width = x + 1, counter for height = y - 1 (basically the first few steps that can be moved to from the starting position, X) the numbers placed there would be limit - 1.
Same for when it goes on
for i less than or equal to limit
the spaces between the limit - 1, will now be filled with limit - 2. So it will be like x-1 && y-1, y-2, y-1 && x+1, x+2 and so on until the last where 0 is placed.
But how do you write this and fix it in the for loop?
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main()
{
int width, height, x, y, max;
printf("Enter width: ");
scanf("%d\n", &width);
printf("Enter height: ");
scanf("%d\n", &height);
printf("Enter x: ");
scanf("%d\n", &x);
printf("Enter y: ");
scanf("%d\n", &y);
printf("Enter walking limit: ");
scanf("%d\n", &max);
int b = 0, a = 0; // initalize local variable
for (a = 0; a < height; a++) {
// fill the width
for (b = 0; b < width + 1; b++ ) {
printf("|");
if (b==x && a==y)
{
printf("X");
}else if(((b==x-1) && (a==y)) || ((b==x+1) && (a==y)) || ((a==y-1) && (b==x)))
{
printf("%d", max-1);
}else if(//i am not sure if this will be needed)
{
for(int i = 2; i <= max; i++)
{
//add max - i to spaces after C until max is over.
}
}
}
printf("\n");
}
return 0;
}
You should split your code in several functions.
I have done a quick example below.
First we need a function to display the array.
You need to create your array, see the malloc.
Then you set the max steps at the correct position.
Then you need a function to do the propagation, this is a very naive way to do.
Scan the array and set to the value minus one all the cell around.
Do it max times.
For example:
Enter width: 9
Enter height: 9
Enter x: 3
Enter y: 7
Enter walking limit: 3
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | |0| | | | | |
| | |0|1|0| | | | |
| |0|1|2|1|0| | | |
|0|1|2|3|2|1|0| | |
| |0|1|2|1|0| | | |
The code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
static void array_print(int *array, int width, int height)
{
for (int i = 0; i < height; i++) {
printf("|");
for (int j = 0; j < width; j++) {
int val = array[i * width + j];
if (val < 0) {
printf(" |");
} else {
printf("%d|", val);
}
}
printf("\n");
}
}
/**
*
* Update the array if x, y is inside the limit and
* if val is greater than the already set value
*
*/
static void
array_upate(int *array, int width, int height, int x, int y, int val) {
if (x >= 0 && x < width && y >= 0 && y < height) {
int prev = array[y * width + x];
if (val > prev) {
array[y * width + x] = val;
}
}
}
static void array_propagate(int *array, int width, int height)
{
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int val = array[i * width + j];
if (val > 0) {
array_upate(array, width, height, j, i - 1, val - 1);
array_upate(array, width, height, j, i + 1, val - 1);
array_upate(array, width, height, j - 1, i, val - 1);
array_upate(array, width, height, j + 1, i, val - 1);
}
}
}
}
int main()
{
int width, height, x, y, max;
int *array;
printf("Enter width: ");
scanf("%d", &width);
printf("Enter height: ");
scanf("%d", &height);
printf("Enter x: ");
scanf("%d", &x);
printf("Enter y: ");
scanf("%d", &y);
printf("Enter walking limit: ");
scanf("%d", &max);
/* allocate the array */
array = malloc(sizeof(int) * width * height);
/* Set all to -1 */
memset(array, -1, sizeof(int) * width * height);
/* set the start to max */
array_upate(array, width, height, x, y, max);
/* do the propagation */
while (max-- > 0) {
array_propagate(array, width, height);
}
array_print(array, width, height);
free(array);
}
I'm creating a connect-4 game... I have a lot of it done; however, the way I was creating my board was static & it needed to be dynamic, so I've made a side program to fix this before implementing it in my main program. For some reason, the if & else-if conditionals in this chunk of code create a segmentation fault, and I can't figure out why...
// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
if(aPtr[row][col] == '0') {
printf("| X ");
}
else if(aPtr[row][col] == '1') {
printf("| O ");
}
else {
printf("| ");
}
}
puts("||");
}
when I comment these conditionals out the board prints just fine & looks like this
------ Connect *Four ------
Connect X Command Line Game
&&===================&&
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
&&===================&&
1 2 3 4 5
the entirety of this side-program is below, any insight as to why this segmentation fault is occurring will be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
void initialize(int num_rows, int num_cols, char **aPtr) {
int i, r, c;
// create the space for the board
aPtr = malloc(num_rows * sizeof(char*));
for (i = 0; i < num_rows; i++){
aPtr[i] = malloc(num_cols * sizeof (char));
}
// go through the board and set all values equal to -1
for (r = 0; r < num_rows; r++) {
for (c = 0; c < num_cols; c++) {
aPtr[r][c] = '9';
printf("%c", aPtr[r][c]);
}
printf("\n");
}
}
void printBoard(int num_rows, int num_columns, char **aPtr) {
int row, col;
printf("\n");
puts("------ Connect *Four ------");
puts("Connect X Command Line Game");
// for fancy top of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
// if(aPtr[row][col] == '0') {
// printf("| X ");
// }
// else if(aPtr[row][col] == '1') {
// printf("| O ");
// }
// else {
printf("| ");
// }
}
puts("||");
}
// for fancy bottom of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
printf(" ");
if (col < 100){
for(col = 0; col < num_columns; col++) {
if (col < 10) {
printf(" %d ", col + 1);
}
else {
printf("%d ", col + 1);
}
}
puts("\n");
}
}
// *******************************************************************************************************
// *******************************************************************************************************
int main (int argc, char *argv[]) {
char **aPtr;
int height = 10;
int width = 5;
int i;
initialize(height, width, aPtr);
printBoard(height, width, aPtr);
}
Here is the modification of your code, maybe it will help. Note that I'm passing &aPtr and *aPtr = (char*) malloc(...)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
void initialize(int num_rows, int num_cols, char **aPtr) {
int i, r, c;
// create the space for the board
*aPtr = (char*) malloc(num_rows * sizeof(char*));
if(*aPtr == NULL)
{
free(*aPtr);
printf("Memory allocation failed");
}
for (i = 0; i < num_rows; i++){
aPtr[i] = (char *) malloc(num_cols * sizeof (char));
}
// go through the board and set all values equal to -1
for (r = 0; r < num_rows; r++) {
for (c = 0; c < num_cols; c++) {
aPtr[r][c] = '9';
printf("%c", aPtr[r][c]);
}
printf("\n");
}
}
void printBoard(int num_rows, int num_columns, char **aPtr) {
int row, col;
printf("\n");
puts("------ Connect *Four ------");
puts("Connect X Command Line Game");
// for fancy top of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
if(aPtr[row][col] == '0') {
printf("| X ");
}
else if(aPtr[row][col] == '1') {
printf("| O ");
}
else {
printf("| ");
}
}
puts("||");
}
// for fancy bottom of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
printf(" ");
if (col < 100){
for(col = 0; col < num_columns; col++) {
if (col < 10) {
printf(" %d ", col + 1);
}
else {
printf("%d ", col + 1);
}
}
puts("\n");
}
}
// *******************************************************************************************************
// *******************************************************************************************************
int main (int argc, char *argv[]) {
char *aPtr;
int height = 10;
int width = 5;
int i;
initialize(height, width, &aPtr);
printBoard(height, width, &aPtr);
}
Note that you are doing: aPtr[r][c] = '9'; so your board is empty, but if you change it to say 0you would get something like:
------ Connect *Four ------
Connect X Command Line Game
&&===================&&
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
&&===================&&
1 2 3 4 5
I'm assuming that's what you expected?
I want re-order my matrix in ascending order but it should be in row basis and I'm a little bit stuck and in trouble with it. Here what it looks like.
ex.
3 5 4
9 2 8
6 4 8
should be;
3 4 5
2 8 9
4 6 8
But with my code orders everything in ascending order!
2 3 4
4 5 6
8 8 9
Here is my full code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define n 3
int main()
{
int arr[n][n],min,i,j,tmp,y,k,w,z=0,q=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("Enter number: ");
scanf("%d",&arr[i][j]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
z = i;
q = j;
min = arr[i][j];
w = j;
for (k = i; k < n; k++)
{
for (; w < n; w++)
if (arr[k][w] < min)
{
min = arr[k][w];
z = k;
q = w;
}
w = 0;
}
tmp=arr[i][j];
arr[i][j]=min;
arr[z][q]=tmp;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d ",arr[i][j]);
printf("\n");
}
getch();
}
Here sorting technique is like :
Assume all the element before | is sorted
|5 4 1 2
select 1st lowest value and insert it in 1st position
1 |4 5 2
select 2nd lowest value and insert it in 2nd position
1 2| 5 4
select 3rd lowest value and insert it in 3rd position
1 2 4| 5
select 4th lowest value and insert it in 4th position
1 2 4 5| ...no elements left :) Sorted order
So the implementation will be :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define n 3
int main()
{
int arr[n][n],min,i,j,tmp,y,k,w,z=0,q=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter number: ");
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
z = j; // assuming that current value is j'th minimum
min = arr[i][j];
// Checking the j'th min value of i'th row
for (k = j +1 ; k < n; k++)
{
if (arr[i][k] < min)
{
min = arr[i][k];
z = k; // saving j'th min value
}
}
// swapping current value with the j'th min value
tmp=arr[i][j];
arr[i][j]=min;
arr[i][z]=tmp;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d ",arr[i][j]);
printf("\n");
}
getch();
}
not sure at all if i have understood everything but :
void sort_int_tab(int *tab, unsigned int size)
{
unsigned int i;
unsigned int j;
int tmp;
i = 0;
while (i < size)
{
j = 0;
while (j < size - 1)
{
if (tab[j] > tab[j + 1])
{
tmp = tab[j];
tab[j] = tab[j + 1];
tab[j + 1] = tmp;
}
j++;
}
i++;
}
}
int i = 0;
while (i < n)
{
sort_int_tab(arr[i], n);
i++;
}