Beginner in C - Array of strings lost content after initialization in cyklus - c

I am trying to make array of strings each representing one card of a poker deck from 2 strings (ranks, colors). If I try print card immidietly after assignment it's ok but if I try it after all assignments nothing happend.
My "code":
int main(void)
{
char rank[] = "23456789TJQKA";
char color[] = "cdhs";
char deck[52][3];
int k = 0;
for (int i = 0; i < 13; i++) {
for(int j = 0; j < 4; j++) {
deck[k][0] = rank[i];
deck[k][1] = color[j];
deck[k][2] = 0;
k++;
printf("%s\n",deck[k-1]); // this print works
}
}
printf("%s\n",deck[0]); //this does nothing (even if I change index)
//-------------------------- here I am trying make all possible pairs but deck is now empty :(
k = 0;
char allPairs[1327][5];
for (int i = 0; i < 51; i++) {
for (int j = 0; j < 52; j++) { //**edit** - thanks ;)
allPairs[k][0] = deck[i][0];
allPairs[k][1] = deck[i][1];
allPairs[k][2] = deck[j][0];
allPairs[k][3] = deck[j][1];
allPairs[k][4] = 0;
k++;
}
}
}
All seems to work now thanks guys!

What you need to do is replace i++ with j++ in the following statement
for (int j = 0; j < 3; i++)
and also comment out the following line as it is printing 2c again:
printf("%s\n",deck[0]); //this does nothing (even if I change index)

Related

Array values changing without reason

This is my code for Project Euler: Problem 11
int main(int argc, char** argv) {
char stevila [1600] = "08022297381500400075040507785212507791084949994017811857608717409843694804566200814931735579142993714067538830034913366552709523046011426924685601325671370236912231167151676389419236542240402866331380244732609903450244753353783684203517125032988128642367102638406759547066183864706726206802621220956394396308409166499421245558056673992697177878968314883489637221362309750076442045351400613397343133957817532822753167159403800462161409535692163905429635314755588824001754243629855786560048357189070544443744602158515417581980816805944769287392138652177704895540045208839735991607975732162626793327986688366887576220720346336746551232639353690442167338253911249472180846293240627636206936417230238834629969826759857404361620733529783190017431497148868116235705540170547183515469169233486143520189196748";
int stevilaGrid [20][20];
int stevilaRacunanje[4][4];
int stevecPoStevilih = 0;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
stevilaGrid[i][j] = (stevila[stevecPoStevilih] - 48)*10 + stevila[stevecPoStevilih + 1] - 48;
stevecPoStevilih += 2;
}
}
int rezultat [10];
int najvecji = 0;
int trenutni;
int temp = 0;
for (int i = 0; i < 17; i++) {
for (int j = 0; j < 17; j++) {
//problems start here
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
temp = stevilaGrid[i + k][j + l];
stevilaRacunanje[k][l] = temp;
}
}
for (int k = 0; k < 5; k++) {
rezultat[k] = stevilaRacunanje[k][0] * stevilaRacunanje[k][1] * stevilaRacunanje[k][2] * stevilaRacunanje[k][3];
rezultat[k+4] = stevilaRacunanje[0][k] * stevilaRacunanje[1][k] * stevilaRacunanje[2][k] * stevilaRacunanje[3][k];
}
rezultat[8] = stevilaRacunanje[0][0] * stevilaRacunanje[1][1] * stevilaRacunanje[2][2] * stevilaRacunanje[3][3];
rezultat[9] = stevilaRacunanje[0][3] * stevilaRacunanje[1][2] * stevilaRacunanje[2][1] * stevilaRacunanje[3][0];
for (int k = 0; k < 10; k++) {
trenutni = rezultat[k];
if(trenutni > najvecji){
najvecji = trenutni;
}
}
}
}
printf("Najvecji zmnozek: %d", najvecji);
return (EXIT_SUCCESS);
}
First I convert the string of numbers into a 2D int array.
Then I try to divide the grid into smaller 4x4 squares with which I can work more easily. That is where the problems start (as marked in the code).
At the very beginning (*i=0,j=0;k=4,j=0*) something strange starts to happen. The values in *stevilaGrid[][]* start to change randomly and seemingly without a reason.
Can somebody please explain this to me. I have tested this behavior on Windows with Cygwin 64bit and Ubuntu with GCC 64bit.
[i + k][j + l];
When i==16 and k==4 or j==16 and j==4 you'll be hitting element [20]
Your array only goes 0...19

How to make strings stick together while radix sorting?

I have to make a program that sort strings (with exact length 7 chars) by using radix sort. I already made a function that sort each column separately. My problem is how to make the whole string move, not just one char. It's really problematic for me to see how should it work in C.
I made one array "char strings[3][8]" and "char output[3][8]" to get sorted 3 strings with exact 7 chars in each one. For example sorting these strings:
strcpy(strings[0], "kupbars");
strcpy(strings[1], "daparba");
strcpy(strings[2], "jykaxaw");
In output I get:
dakaaaa
juparbs
kypbxrw
Each column is sorted correctly but chars don't stick together. I tried many ways for 3 hours but nothing works.
My code looks like this:
void countingSort(char a[][8], char b[][8]) {
int c[123];
for (int pos = 6; pos >= 0; pos--) {
for (int i = 0; i < 123; i++)
c[i] = 0;
for (int i = 0; i < 3; i++)
c[(int)a[i][pos]]++;
for (int i = 1; i < 123; i++)
c[i] += c[i - 1];
for (int i = 2; i >= 0; i--) {
b[--c[(int)a[i][pos]]][pos] = a[i][pos];
}
}
}
(There are constants limiting string length etc. because it's easy to change it to variable - I just focused on getting this program work properly.)
Try changing the loop to move an entire string:
for (int i = 2; i >= 0; i--) {
int k = --c[(int)a[i][pos]];
for(int j = 0; j < 8; j++) {
b[k][j] = a[i][j];
}
}
You could do a circular list but it's a little overhead. I propose you to use memmove().
#include <string.h>
void array_move_forward(char array[3][8]) {
for (int i = 0; i < 3; i++) {
char tmp = array[i][6];
memmove(array[i] + 1, array[i], 6);
array[i][0] = tmp;
}
}
void array_move_rewind(char array[3][8]) {
for (int i = 0; i < 3; i++) {
char tmp = array[i][0];
memmove(array[i], array[i] + 1, 6);
array[i][6] = tmp;
}
}
A other solution would be to manipulate your string yourself and using a index, that indicate the first letter of your string.
{
char str[7];
int i = 0;
...
int j = i;
for (int k = 0; k < 7; k++) {
char tmp = str[j++ % 7];
}
}
With that you could rotate your string just with i++ or i--.
struct my_string_radix {
char str[7];
int begin;
}

Why is my 3d array reversed?

I have a 3d array of ints stored in a struct which represents a tetris block:
typedef struct
{
int orientations[4][4][4];
dimension dimensions[4];
int i;
int x;
int y;
}block;
orientations is filled with every possible position for the block and dimension is a struct that provides information for collision checking:
typedef struct
{
int left, right, bottom;
}dimension;
each orientation and dimension should be linked by the block's i value. For some reason orientations (but not dimensions) seems to be reversed. Does anybody know why this is?
here is how I assign values to dimensions:
block* shape = malloc(sizeof(block));
shape->dimensions[0].left = 0;
shape->dimensions[0].right = 3;
shape->dimensions[0].bottom = 1;
shape->dimensions[1].left = 2;
shape->dimensions[1].right = 2;
shape->dimensions[1].bottom = 3;
shape->dimensions[2].left = 0;
shape->dimensions[2].right = 3;
shape->dimensions[2].bottom = 2;
shape->dimensions[3].left = 1;
shape->dimensions[3].right = 1;
shape->dimensions[3].bottom = 3;
and orientations:
int first[4][4] = {{0,0,0,0}, {2,2,2,2}, {0,0,0,0}, {0,0,0,0}};
int second[4][4] = {{0,0,2,0},{0,0,2,0},{0,0,2,0},{0,0,2,0}};
int third[4][4] = {{0,0,0,0},{0,0,0,0},{2,2,2,2},{0,0,0,0}};
int fourth[4][4] = {{0,2,0,0},{0,2,0,0},{0,2,0,0},{0,2,0,0}};
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[0][i][j] = first[i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[1][i][j] = second[i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[2][i][j] = third[i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[3][i][j] = fourth[i][j];
}
}
here is how I'm accessing the array:
shape->orientations[current->i][i][j]
when I attempt to access shape->orientations[3] it returns the values I set to shape->orientations[0] earlier. Any help would be greatly appreciated, thanks in advance.
The reason is that your array is indexed [orientation][row][column], but you are interpreting it as [orientation][x][y] when you should be interpreting it as [orientation][y][x]. So you are getting a pattern with the x and y swapped, which appears as if it has been rotated to a different orientation (actually it's equivalent to a rotation and a flip).

How to put characters in a text file into an array in C programming

I have a project to be done , which is the maze game.
to set the field , I have used
const char random_field[15][20]={
"####################",
"#...#.....#.....#.?#",
"#.#.#.....#.#.....##",
"#.#.#####.#.#.######",
"#.#.#.......#.#.####",
"#.#.###.#######.####",
"#.#................#",
"#.####...#####.###.#",
"#.#....####..#.###.#",
"#.#.#........#.#...#",
"#.#.####..####.#.###",
"#.#....#.....#.#...#",
"#.######.#####.###.#",
"#..................#",
"####################"
};
and it works well !
but now I want to change the idea a little bit ..
I want to write this map into a text file and then declare a 2D array and fix the map in the txt file into the array..
I have wrote this but it doesn't work..
const char random_field[15][20] ;
FILE *filename;
filename=fopen("map1.txt","r");
for (i = 0; i < 15; i++)
{
for (j = 0; i < 20; j++)
{
fscanf(filename, "%c", &random_field[i][j]);
}
}
for (i = 0; i < 15; i++)
{
for (j = 0; i < 20; j++)
{
printf("%c",random_field[i][j]);
}
}
Any Ideas ? thank you
You have some minor typos and you aren't handling the new lines properly.
filename=fopen("map1.txt","r");
for (i = 0; i < 20; i++)
{
//// You had i < 20 here.
for (j = 0; j < 20; j++)
{
fscanf(filename, "%c", &random_field[i][j]);
}
// Each row in the text file has a new line character on the end
// so scan for this.
fscanf(filename, "\n");
}
for (i = 0; i < 15; i++)
{
//// You had i < 20 here.
for (j = 0; j < 20; j++)
{
printf("%c",random_field[i][j]);
}
// You need to add a new line in the print out after each row
// since it isn't mistakenly in random_field now.
printf("\n");
}
With your code, you were reading the new line character (or characters) into the first row of each random_field rather than skipping it.

Copy i-value from array triple time

I have an array[768] but now I have only 256 (from 0 to 255) samples in this array. I want to copy each value from 0 to 255 and fill this array better, I mean:
[1][2][3] - > [1][1][1][2][2][2][3][3][3]
How I can do that? Is there a library function that can do this?
I don't recall any known library function capable of doing this.
If you want to do it in-place, I'd do it from right to left (i.e. tail to head), I think this is the only way to do it in-place:
int i, j;
for (i = 255, j = 767; i > 0; i--) {
for (int k = 0; k < 3; k++) {
array[j--] = array[i];
}
}
If you don't need to do it in-place, this would suffice:
for (int i = 0, j = 0; i < 256; i++) {
for (int k = 0; k < 3; k++)
new_array[j++] = array[i];
}
You can do this:
for (int i = 0; i < 768; ++i)
new_array[i] = array[i/3];
The index of the right hand side of the assignment will vary only every three steps.

Resources