occurrences in multidimensional array - c

I have a 2d array which contains for example
dict[0][30] = "name1";
dict[1][30] = "name2";
dict[2][30] = "name3";
i'm using the following function to check for occurrences
char letters[] = {"abcdefghijklmnopqrstuvwxyz"};
for(i = 0; i < size; i++)
for (j = 0; j < 26; j++)
if(tolower(dict[i]) == letters[j])
count[j]++;
i have tested the code using 1d array and its working any example for 2d arrays
Thanks

dict[0][30] = "name1";
check if it is defining the variable.

Related

Reversing a string using for loop giving an error

I'm trying to assign the elements of string str[] to rev[] using this code below, but it keeps giving me the error:
"string subscript out of range" error.
How do I correct this?
for (int i = 0; i <= str.length(); i++)
{
for (int j = str.length(); j >= 0; j--)
{
rev[i] = str[j];
}
}
For an array index range from 0 to array size - 1.
You don't need two loops to reverse an array.
In java you can do reversal like this:
int length = str.length - 1;
for (int i = 0; i <= str.length-1; i++) {
rev[length - i] = str[i];
}

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

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)

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;
}

Is the statement *a[i]++ /= K valid?

Array initialization Code:
int m = 100;
int n = 50;
int i = 0, j = 0;
float **a = (float**)malloc(m*sizeof(float*));
for (i = 0; i < m; i++)
{
a[i] = (float*)malloc(n*sizeof(float));
for (j = 0; j < n; j++)
a[i][j] = i + j;
}
a is a 2D array and I want to traverse and update the elements of the 1D array a[0]
Say I want to divide all elements of a[0] by 2:
for (i = 0; i < n; i++)
*a[0]++ /= 2; // instead of a[0][i] /= 2;
This doesn't seem to work..
I guess a is a 2 dimensional array like int a[10][20], then the given statement a[i]++ is "invalid".
The reason is that since a[i] being an array a[i] is a non-modifiable 'lvalue'.
In the above case *a[i] is valid but not the a[i]++
Yes, although it look like a homework. If you want more interesting code, you could write this without spaces, as *a[i]++/=K, and finally you could attach this to the containing loop, as for example while(p=a[i]++)*p/=K; to make things more compressed. ;-)

Copying 1-D array into 2 -D array

I have one two dimensional array and one single dimensional array.
The two dimensional array is of NxM size. And the one dimensional array is of size N x M means
it has N X M elements.
Now I want to copy all the elements of the one dimensional array into the 2-D array.
This is what I tried
for(i = 0; i < M; i += 1) {
for(j = 0; j < N; j += 1) {
arr2d[i][j] = arr2d[(i*j +j)];
}
}
But not working Any suggestions???
You can change:
arr2d[i][j] = arr2d[(i*j +j)];
to:
arr2d[i][j] = arr1d[(i*N + j)];
which would probably work out better.
var arr1d = [1,2,3,4,5,6];
var M = 1, N = 2;
var arr2d = [];
var counter = 0;
for(i = 0; i <= M; i += 1) {
for(j = 0; j <= N; j += 1) {
if(j == 0)
arr2d[i] = [];
arr2d[i][j] = arr1d[counter++];
}
}

Resources