Trash output in 8x8x2 array in C - c

I'm trying to represent a 8x8 Cartesian plane whose contents would be a string of length 2. I'd like to keep as type-safety as I can this scheme and doing this:
typedef char cartesian[8][8][2];
cartesian xy;
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
xy[i][j][0] = ' '
xy[i][j][1] = ' '
}
}
// The first element would represent some kind of information, and the other one
// would be just elements like '+' or '*'. In other cases, this would be 'EMPTY',
// it means a double space.
xy[2][4][0] = 'B';
xy[2][4][1] = '+';
// The right printing method (cause it doesn't have any trash) would be:
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
printf("| %c%c ", xy[i][j][0], xy[I][j][1] );
}
printf("|\n");
}
But here's the question: Why the output trash with printf("| %s ", xy[i][j][]); ?
I know this could be a dumb question, but I'm out and tired now.
Thanks in advance.
By the way, doesn't work to assign. I mean: xy[2][4][] = "B+";.

Here is your code rewritten. Note that cartesian is now [8][8][3] and strcpy is used to fill the last array dimension. For clarity, I moved to clearing on xy into a separate function.
#include <stdio.h>
#include <string.h>
typedef char cartesian[8][8][3];
void clearXY(cartesian *xy)
{
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
strcpy((*xy)[i][j], " ");
}
}
}
int main(int argc, char *argv[])
{
cartesian xy;
// The first element would represent some kind of information, and the other one
// would be just elements like '+' or '*'. In other cases, this would be 'EMPTY',
// it means a double space.
clearXY(&xy);
strcpy(xy[2][4], "B+");
printf("-------- Method 1 --------\n");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
printf("| %c%c ", xy[i][j][0], xy[i][j][1]);
}
printf("|\n");
}
printf("-------- Method 2 --------\n");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
printf("| %s ", xy[i][j]);
}
printf("|\n");
}
printf("-------- Done ------------\n");
}

Related

How to print a 2D array with dots inside them in C

how would I input a 2D array in C, filled with dots?
Here is the code I have written so far, but I still don't see an output of a 2D array with dots.
#include <stdio.h>
#include <stdlib.h>
#define PLAYER_NONE 0
#define PLAYER 1
#define PLAYER_CPU 2
int i; // Global Variable for column and row
int j; // Global Variable for column and row
char playerBoard[8][8]; //Global Variable
char cpuBoard[8][8]; // Global Variable
//Initialise the main parts of the board
void initialise_board(void)
{
for (int i = 0; i < 8; i++) { //iterate the rows
for (int j = 0; j < 8; j++) {
cpuBoard[i][j] = '.';
}
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
playerBoard[i][j] = '.';
}
}
}
int main(int argc, char *argv[])
{
initialise_board();
while (!check_win()) {
display_board();
get_move(turn);
turn = (turn == 1) ? 2 : 1;
return 0;
}
//Display battleship board when a turn is played
void display_board()
{
printf("\n");
for (char i = 'A'; i < 'H' + 1; i++) {
printf("%c", i);
}
printf("\n");
for (i = 1; i < 8 + 1; i++) {
printf("%d",i);
for (j = 0; j < 8; j++) {
}
printf("\n");
}
printf("===");
printf("\n");
}
Any help would be much appreciated, Thank you very much
Sometimes, while troubleshooting a seemingly impossible problem, it is a good idea to remove all distractions from the code:
Run a simplified main() function:
int main(int argc, char *argv[])
{
initialise_board();
//while (!check_win()) {
display_board();
// get_move(turn);
// turn = (turn == 1) ? 2 : 1;
return 0;
}
And you will see some output, which you can then debug to adjust as needed.
However, As you point out in your comments, you are not seeing output. If you look closely, you will discover that is because you are never including cpuBoard or playerBoard in a printf statement, such as:
printf("%c", cpuBoard[i][j]);
The following will not finish this for you, but will get you started:
void display_board(void)
{
//printf("\n");//removed for illustration
//for (char i = 'A'; i < 'H' + 1; i++) {
// printf("%c", i);
//}
printf("\n");
for (i = 1; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
printf("%c", cpuBoard[i][j]); //illustrates printout of populated array cpuboard.
}
printf("\n");
}
printf("===");
printf("\n");
}
Your code looks fine. Check for the opening and closing brackets and add following statement in initializeboard() function :
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
printf("%c",playerBoard[i][j]);
}
}

Command line arguments, printing columns and rows

I'm trying to print out characters given input from the command-line arguments. I'm having a bit of trouble wrapping my head around this.
When I run ./test Foo bar I want it to print
./test
Foo
o
o
bar
a
r
While it might not be the best solution, I want it to be done using arrays of arrays of chars, and it looks like a three-dimensional array, so I'm going with a triple-nested for-loop.
What I have so far is this:
for (i = 1; i < argc; i++) {
for (j = 0; j < argv[argc][j]; j++) {
for (k = 0; k < argv[argc][j]; k++) {
printf("%c", k);
}
}
printf("\n");
}
The outermost loop starts at 1, since I don't want to print out the ./test-bit. But I'm lost. I can work with two-dimensional arrays, but I wanted to try it out with an extra dimension.
Can you give me a few pointers?
Your condition(j < argv[argc][j], k < argv[argc][j]) are wrong.
fix like this:
#include <stdio.h>
int main(int argc, char *argv[]){
for (int i = 0; i < argc; i++) {
puts(argv[i]);
if(i){
for(int j = 1; argv[i][j]; ++j){
printf("%c\n", argv[i][j]);
}
printf("\n");
}
}
return 0;
}
You can solve this in 2 loops as
for (i = 1; i < argc; i++) {
for (j = 0; argv[i][j]!='\0'; j++) {
printf("%c", argv[i][j]);
}
printf("\n");
}

I need to pass an array of structures to a function [large code]

I've been working on this for days but can't seem to make it work out.
Sorry in advance for the unholy length of this, so if anyone takes the time to go through it and try to understand this mess, I'd owe you.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct cart {
int id;
char *nume;
} cart;
typedef struct pach {
int id, idCartier, strada, numar, prioritate, codificare;
float greutate;
char* mesaj;
int adresa[18];
} pach;
typedef struct post {
int id, nrPachete;
int vector[50];
} post;
int citeste(int *nrP, cart *cartier, pach *pachet, int *nrC) {
printf("Punctul 1\n");
int i, j;
scanf("%d", nrC);
for (i = 0; i < *nrC; i++) {
cartier[i].id = i;
char aux[500];
scanf("%s", aux);
cartier[i].nume = malloc(strlen(aux) + 1);
cartier[i].nume = aux;
printf("%d %s\n", cartier[i].id, cartier[i].nume);
}
scanf("%d", nrP);
for (i = 0; i < *nrP; i++) {
pachet[i].id = i;
char aux[500];
for (j = 0; j < 18; j++)
scanf("%d", &pachet[i].adresa[j]);
scanf("%d %f", &pachet[i].prioritate, &pachet[i].greutate);
getchar();
fgets(aux, 256, stdin);
pachet[i].mesaj = malloc(strlen(aux) + 1);
pachet[i].mesaj = aux;
printf("%d\n", pachet[i].id);
for (j = 0; j < 18; j++)
printf("%d ", pachet[i].adresa[j]);
printf("\n%d %.6f ", pachet[i].prioritate, pachet[i].greutate);
printf("%s", pachet[i].mesaj);
}
return *nrP;
}
void extrage(int *nrP, pach *pachet) {
printf("\nPunctul 2\n");
int i, j;
for (i = 0; i < *nrP; i++) {
pachet[i].idCartier = 0;
pachet[i].strada = 0;
pachet[i].numar = 0;
for (j = 0; j < 5; j++)
pachet[i].idCartier += pachet[i].adresa[j] * pow(2, (4 - j));
for (j = 5; j < 10; j++)
pachet[i].strada += pachet[i].adresa[j] * pow(2, (9 - j));
for (j = 10; j < 18; j++)
pachet[i].numar += pachet[i].adresa[j] * pow(2, (17 - j));
printf("%d %d ", pachet[i].id, pachet[i].idCartier);
printf("%d %d\n", pachet[i].strada, pachet[i].numar);
}
}
void distribuie(int *nrP, pach *pachet, post *postas, int *nrC, cart *cartier) {
printf("Punctul 3\n");
int i, j;
for (i = 0; i < *nrC; i++) { // FOR-1A
postas[i].nrPachete = 0;
postas[i].id = i;
for (j = 0; j < 50; j++)
postas[i].vector[j] = 0;
}
for (i = 0; i < *nrC; i++) { // FOR-1B
for (j = 0; j < *nrP; j++) {
if (cartier[i].id == pachet[j].idCartier) {
postas[i].vector[postas[i].nrPachete] = pachet[j].id;
postas[i].nrPachete++;
}
}
printf("%d %d ", postas[i].id, postas[i].nrPachete);
for (j = 0; j < postas[i].nrPachete; j++)
printf("%d ", postas[i].vector[j]);
printf("\n");
}
}
void ordoneaza(pach *pachet, int *nrC, post *postas) {
printf("Punctul 4\n");
pach aux;
int i, j, k = 0, schimbat = 1;
for (i = 0; i < *nrC; i++) {
while (schimbat) {
schimbat = 0;
for (j = 0; j < postas[i].nrPachete - k; j++)
if (pachet[postas[i].vector[j]].prioritate < pachet[postas[i].vector[j+1]].prioritate) {
aux = pachet[postas[i].vector[j]];
pachet[postas[i].vector[j]] = pachet[postas[i].vector[j+1]];
pachet[postas[i].vector[j+1]] = aux;
schimbat = 1;
}
k++;
}
k = 0;
schimbat = 1;
for (j = 0; j < postas[i].nrPachete; j++) {
for (k = j; k < postas[i].nrPachete; k++) {
if (pachet[postas[i].vector[j]].prioritate == pachet[postas[i].vector[k]].prioritate)
if (pachet[postas[i].vector[j]].greutate < pachet[postas[i].vector[k]].greutate) {
aux = pachet[postas[i].vector[j]];
pachet[postas[i].vector[j]] = pachet[postas[i].vector[k]];
pachet[postas[i].vector[k]] = aux;
}
}
}
}
for (i = 0; i < *nrC; i++)
for (j = 0; j < postas[i].nrPachete; j++) {
postas[i].vector[j] = pachet[postas[i].vector[j]].id;
}
for (i = 0; i < *nrC; i++) {
printf("%d %d ", postas[i].id, postas[i].nrPachete);
for (j = 0; j < postas[i].nrPachete; j++)
printf("%d ", postas[i].vector[j]);
printf("\n");
}
}
int main() {
int nrP, nrC;
pach pachet[1600];
post postas[32];
cart cartier[32];
citeste(&nrP, &cartier[32], &pachet[1600], &nrC);
extrage(&nrP, &pachet[1600]);
distribuie(&nrP, &pachet[1600], &postas[32], &nrC, &cartier[32]);
ordoneaza(&pachet[1600], &nrC, &postas[32]);
return (0);
}
Short info on what the program does:
The citeste function should read the cartier and pachet structures. All of them. And then print those in a bit different format.
The extrage function should take every pachet, and use the adresa (written in BINARY) to convert its 3 parts and obtain the strada, numar and idCartier. Then also print those.
Distribuie checks if the pachet is distributed to a postas (distributed means pachet.idCartier == postas.id), if not it distributes it.
Ordoneaza takes every postas's vector and sorts it after the prioritate (or greutate if the prioritate-s are equal).
But it doesn't work as intended and also gives weird Segmentation Faults.
For example if I comment out the distribuie function, it gives me segfault right after extrage. If I put it back, it gives segfault right after doing it. And if I uncomment everything, it gives segfault at the end again.
If anyone actually read all of this and would be willing to reply, I'd highly appreciate it. Any bit of advice helps!
I did not read your code, but your title said you had trouble passing array of structures. I am attaching a working snippet hope it will help you get around your problem.
#include<stdio.h>
typedef struct employee{
int empId;
char name[10];
}EMP;
void arrayOfStruct(EMP *a, int size)
{
printf("%d\t%d",a[0].empId,a[3].empId);
}
int main()
{
EMP NC[4];
NC[0].empId = 9;
NC[3].empId = 2;
arrayOfStruct(&NC[0],sizeof(NC)/sizeof(NC[0]));
}
with the help of size you can never go beyond the memory allocated for structures.
In case you, want to pass higher dimensional arrays, you have to hard code all the size of arrays except the outer most.
void arrayOfStruct(EMP a[][4], int size)
{
// to do
}
int main()
{
EMP NC[2][4];
...
arrayOfStruct(NC,sizeof(NC)/sizeof(NC[0]));
}
as you see, I did not specify the higher most size of array, which I am passing via other arguement.
Why do I need to specify size of inner dimensions ?
Lets take an example, for suppose you have int[4][4], and you are trying to pass array to a function via int[3][], how does a compiler know how many inner blocks to create, in other case via int[][3], the compiler can easily understand that it has to make inner block of size 3 for each outer array.

Copying each row of matrix to a temporary array

I want to copy a row (one each team iterating) of a 500x8 matrix to a temp array with the name actual_row. This is what I've tried.
int matrix[500][8]; // this has been already filled by int's
int actual_row[8];
for(int i = 0; i < 500; i++) {
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf("The row is: ");
for(int q = 0; q < 8; q++) {
printf(" %d ",actual_row[q]);
// do other stuff
}
}
printf("\n");
}
This is not printing the line, it's printing 0's and 1's sometime, so there's something I'm doing wrong.
Thanks in advance.
Don't print actual_row before it's filled completely:
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
}
printf("The row is: ");
for(int q = 0; q < 8; q++) {
printf(" %d ",actual_row[q]);
...
}
Your logic is slightly off. You need to copy the row to actual_row, then print the contents. Furthermore, why not just print the contents while you are copying the matrix row to actual_row:
printf("The row is: ");
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf(" %d ",actual_row[j]);
// do other stuff
}
So your code snippet should be this:
int matrix[500][8]; // this has been already filled by int's
int actual_row[8];
for(int i = 0; i < 500; i++) {
printf("The row is: ");
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf(" %d ",actual_row[j]);
// do other stuff
}
// <--at this point, actual_row fully contains your row
printf("\n");
}
Your logic is slightly off (no need for the third nested loop). You need to copy the row to actual_row (which you did), and print the contents within the same loop:
printf("The row is: ");
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf(" %d ",actual_row[j]);
// do other stuff
}

garbage value in C array

I am trying to write a C code that will print a pyramid structure on screen, something like this.
The corresponding code I've written is something like this.
#include <stdio.h>
#include <stdlib.h>
void printArrayFunc(char arr[9][5]) {
int i, j;
printf("=========================================\nprinting the values\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 9; j++) {
//printf("arr[%d][%d] = %d\n", i,j, arr[i][j]);
if (arr[i][j] == 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
int main() {
int i, j;
char arr[9][5] = {
0
};
printf("============================\nfilling the values\n");
for (i = 0; i < 5; i++) {
for (j = 4 - i; j <= 4 + i; j++) {
arr[i][j] = 1;
// printf("arr[%d][%d]= %d\n",i,j,arr[i][j]);
}
//printf("\n");
}
printArrayFunc(arr);
return 0;
}
It is giving an output like
I know I'm doing some silly mistake but at this moment, I'm not able to find what is going wrong. Let me hear your comments on this.
In the function argument:
char arr[9][5]
In the loop:
for (i = 0; i<5; i++) {
for (j = 0; j<9;j++) {
if (arr[i][j] == 1)
You flipped the position of i and j. i should go from 0 to 9, j from 0 to 5.
if (arr[i][j] == 1)
printf("*");
else
printf(" ");
This statement is giving the garbage value in this statement if if condition is true then it print else statement and when else comes true it prints the garbage value.

Resources