Im trying to create a random function from a given 2d array. The code should be able to shuffle cards based on information in array.
string DeckCards [6][6] = ///determine size of deck //deck1
{
{"1A", "1B", "1C", "1D", "2A", "2B"},
{"2C", "2D", "3A", "3B", "3C", "3D"},
{"4A", "4B", "4C", "4D", "1A", "1B"},
{"1C", "1D", "2A", "2B", "2C", "2D"},
{"3A", "3B", "3C", "3D", "4A", "4B"},
{"4C", "4D", " ", " ", " ", " "}
};
void randomize(string DeckCards[6][6],int n)
{
srand( time(NULL) );
for(int i = n-1; i > 6; i--)
{
int j = rand() %(4+1);
swap(&DeckCards[i],[j]);
}
}
the error is too large to include
Related
typedef struct {
char* value;
char* type;
} CARD;
CARD cards[52];
void initializeCards() {
char types[4][10] = {"Spade", "Club", "Hearts", "Diamonds"};
char values[13][1] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "B", "D", "K"};
for (int i = 0; i < 4; i++) {
for (int e = 0; e < 13; e++) {
cards[i*13 + e].type = types[i];
cards[i*13 + e].value = values[e];
}
}
//This prints all sorts of weid characters.
for (int i=0; i<52; i++) {
printf("%s %s\n", cards[i].type, cards[i].value);
}
}//initializeCards.
I'm trying to make an array containing a deck of cards, but when I try to print the values
it prints all sorts of weird characters.
I tried using %s, %d, %c
I also tried using *cards[i].type or &cards[i].type
all without success.
What am I doing wrong?
Your array values doesn't have enough elements to store the terminating null-character, but you are trying to print that as strings (sequences of characters terminated by a null-character) later.
Allocate enough elements so that the array can hold terminating null-characters.
Wrong line:
char values[13][1] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "B", "D", "K"};
Corrected:
char values[13][3] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "B", "D", "K"};
I'm following this tutorial and library on how to work with JSON in C.
https://linuxprograms.wordpress.com/2010/08/19/json_object_new_object/
I have to work on already developed code, so I cannot change library etc..
here's the code I need to devel; basically I need to create a JSON file with different varName, and fill it with arrays ( each varName has it own array ):
#include <json/json.h>
#include <stdio.h>
int main() {
/*Creating a json object*/
json_object *jobj = json_object_new_object();
/*Creating a json array*/
json_object *jarray = json_object_new_array();
json_object *jarray2 = json_object_new_array();
/*Creating json strings*/
json_object *jstring1 = json_object_new_string("10");
int j = 0;
char num[5];
char num2[5];
for (j = 0; j < 3; j++) {
sprintf(num, "%d", j);
printf("num % s\n", num);
jstring1 = json_object_new_string(num);
json_object_array_add(jarray, jstring1);
sprintf(num2, "%d", j + 50);
printf("num2 % s\n", num2);
jstring1 = json_object_new_string(num2);
json_object_array_add(jarray2, jstring1);
}
/*Form the json object*/
char *varName[] = { "categories", "stories" };
/*what if I have N i.e. 1000 varName element ???
how to put this in a loop
varName[i], could have different values */
json_object_object_add(jobj, varName[0], jarray);
json_object_object_add(jobj, varName[1], jarray2);
...
//json_object_object_add(jobj, varName[N], jarrayN); ?? how to do this in a loop ??
/*Now printing the json object*/
printf("The json object created: %s\n", json_object_to_json_string(jobj));
}
this print:
The json object created: { "categories": [ "0", "1", "2" ], "stories": [ "50", "51", "52" ] }
Which is fine, but how to do this in a loop statement?
I could have many varName elements, and each element has an individual array with different values.
Of course I cannot create N *jarray1 to *jarrayN and to the same with , because I do not know N from the beginning.
json_object_object_add(jobj, varName[0], jarray);
json_object_object_add(jobj, varName[N], jarrayN);
Hope my question is clear
Are you maybe looking for something like this?
#include <json-c/json.h>
#include <stdio.h>
#include <string.h>
static json_object *build_array(const char *var_name) {
printf("Building array for %s...\n", var_name);
json_object *array = json_object_new_array();
for (int j = 0; j < strlen(var_name); j++) {
char num[8];
sprintf(num, "%d", j);
json_object_array_add(array, json_object_new_string(num));
}
return array;
}
int main() {
json_object *root_obj = json_object_new_object();
// since we might not know the length of this array beforehand,
// a NULL string shall denote the last element.
char *var_names[] = {"categories", "stories", "cats", "dogs", NULL};
for (char **var_name_ptr = var_names; *var_name_ptr; var_name_ptr++) {
char *var_name = *var_name_ptr;
json_object *array = build_array(var_name);
json_object_object_add(root_obj, var_name, array);
}
printf("The json object created: %s\n", json_object_to_json_string(root_obj));
json_object_put(root_obj); // Release the root object
}
The output is
Building array for categories...
Building array for stories...
Building array for cats...
Building array for dogs...
The json object created: { "categories": [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ], "stories": [ "0", "1", "2", "3", "4", "5", "6" ], "cats": [ "0", "1", "2", "3" ], "dogs": [ "0", "1", "2", "3" ] }
I have a couple of arrays which have some same ids. What I want to achieve is get the intersections between the arrays in plain javascript, no libraries. If there is a match of Ids and arraypicklist values are not equal between 2 or more arrays I should get an array with the matching Ids.
Below is my example which I tried but this ends up with no ids where I expect at least 1 match. In this case Id:123 as in the first and second array there is a match. So I would expect
intersection = [{"Id":"123","arrayPicklist":"Categorie__c"},{"Id":"123","arrayPicklist":"Regio__c"}];
fiddle:https://jsfiddle.net/ozckc0tw/4/
var buckets = [[{"Id":"123","arrayPicklist":"Categorie__c"}],
[{"Id":"123","arrayPicklist":"Regio__c"}],
[{"Id":"124","arrayPicklist":"Categorie__c"}],
[{"Id":"123","arrayPicklist":"Regio__c"},{"Id":"125","arrayPicklist":"Regio__c"},{"Id":"123","arrayPicklist":
"Regio__c"},{"Id":"126","arrayPicklist":"Regio__c"}]]
function IntersectionByKey(key) {
var i,
j,
k,
ret = [],
item,
args = [].slice.call(arguments, 1);
args.sort(function(a, b) {return a.length - b.length});
i:for(i=0; i<args[0].length; i++) {
item = [Object.assign(args[0][i], {})];
j:for(j=1; j<args.length; j++) {
for(k=0; k<args[j].length; k++) {
if(key in args[0][i] && args[0][i][key] == args[j][k][key]){
item.push(Object.assign(args[j][k], {}));
continue j;
}
}
continue i;
}
ret.push(item);
}
return ret;
}
var key = 'Id';
var intersection = IntersectionByKey.apply(null, [key].concat(buckets));
console.log('intersection '+JSON.stringify(intersection))
Done,Sorry, but I did not looked out your code, I tried my logic with your requirements, You can use it.
my logic
var buckets = [
[{
"Id": "123",
"arrayPicklist": "Categorie__c"
}],
[{
"Id": "123",
"arrayPicklist": "Regio__c"
}],
[{
"Id": "124",
"arrayPicklist": "Categorie__c"
}],
[{
"Id": "123",
"arrayPicklist": "Regio__c"
}, {
"Id": "125",
"arrayPicklist": "Regio__c"
}, {
"Id": "123",
"arrayPicklist": "Regio__c"
}, {
"Id": "126",
"arrayPicklist": "Regio__c"
}]
],
intersection = [{
"Id": "123",
"arrayPicklist": "Categorie__c"
}, {
"Id": "123",
"arrayPicklist": "Regio__c"
}];
var resArray = [];
for (var j = 0; j < intersection.length; j++) {
for (var i = 0; i < buckets.length; i++) {
for (var k = 0; k < buckets[i].length; k++) {
if (buckets[i][k].Id == intersection[j].Id && buckets[i][k].arrayPicklist == intersection[j].arrayPicklist) {
resArray.push(buckets[i][k]);
}
}
}
}
console.log(resArray);
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 20
struct Item
{
int SKU;
char name[MAX_LENGTH+1];
};
int contains(void)
{
int compare;
return 0;
}
void displayItemWith(struct Item item[], int count)
{
int i;
char alphabet;
i = 0;
for(i=0; i<count; i++){
//char inventory[ ] = {item->SKU};
printf("%d", item[i].SKU);
}
int main(void)
{
int compare;
char count;
//prints the title
printf("=== TEST ===\n");
// hard-coded inventory 21 items - room for 0 more
struct Item inventory[21] =
{
// price sku txd qty min name
{ .SKU = 275, "Royal Gala Apples" },
{ .SKU = 386, "Honeydew Melon" },
{ .SKU = 240, "Blueberries" },
{ .SKU = 916, "Seedless Grapes" },
{ .SKU = 385, "Pomegranate" },
{ .SKU = 495, "Banana" },
{ .SKU = 316, "Kiwifruit" },
{ .SKU = 355, "Chicken Alfredo" },
{ .SKU = 846, "Veal Parmigiana" },
{ .SKU = 359, "Beefsteak Pie" },
{ .SKU = 127, "Curry Chicken" },
{ .SKU = 238, "Tide Detergent" },
{ .SKU = 324, "Tide Liq. Pods" },
{ .SKU = 491, "Tide Powder Det." },
{ .SKU = 538, "Lays Chips S&V" },
{ .SKU = 649, "Joe Org Chips" },
{ .SKU = 731, "Allen's Apple Juice" },
{ .SKU = 984, "Coke 12 Pack" },
{ .SKU = 350, "Nestea 12 Pack" },
{ .SKU = 835, "7up 12 Pack" }
};
printf("*** NO MATCHES ***\n");
printf("=== END ===\n");
return 0;
}
Hello, users, I am trying to print out the list of items
in output with such format;
SKU: 275 - Royal Gala Apples
SKU: 386 - Honeydew Melon
but the only thing that would print is the ===TEST===, NO MATCHES, ===END===.
Any reason why?
Because no matter what you do, the "NO MATCHES" will show because you printf it without no conditions. Also, in order to print the list you need to call the method you made which is "displayItemWith(struct Item item[], int count)" and please declare the struct first for a better approach.example:
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
I am getting some confusing behaviour trying to use the c builtin bsearch on an array of strings in C. Here is the code. I know you can use the builtin strcmp for searching arrays of strings, but I included myStrCmp for debugging purposes because I didn't know why it wasn't working.
const char *stateNames[] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "Washington DC", "West Virginia", "Wisconsin", "Wyoming"};
int myStrCmp(const void *s1, const void *s2) {
printf("myStrCmp: s1(%p): %s, s2(%p): %s\n", s1, (char *)s1, s2, (char *)s2);
return strcmp(s1, s2);
}
int determineState(char *state) {
printf("state: %s\n", state);
for(int i = 0; i < 51; i++)
printf("stateNames[%i](%p): %s\n", i, &(stateNames[i]), stateNames[i]);
char *found = (char *) bsearch(state, stateNames, 51, sizeof(char *), myStrCmp );
if(found == NULL)
return -1;
return 0;
}
and here is some of the output when this function is called to look for Alabama.
stateNames[0](0x618440): Alabama
stateNames[1](0x618448): Alaska
stateNames[2](0x618450): Arizona
...
stateNames[24](0x618500): Missouri
stateNames[25](0x618508): Montana
stateNames[26](0x618510): Nebraska
stateNames[27](0x618518): Nevada
stateNames[28](0x618520): New Hampshire
stateNames[29](0x618528): New Jersey
stateNames[30](0x618530): New Mexico
stateNames[31](0x618538): New York
stateNames[32](0x618540): North Carolina
stateNames[33](0x618548): North Dakota
stateNames[34](0x618550): Ohio
stateNames[35](0x618558): Oklahoma
stateNames[36](0x618560): Oregon
stateNames[37](0x618568): Pennsylvania
stateNames[38](0x618570): Rhode Island
stateNames[39](0x618578): South Carolina
stateNames[40](0x618580): South Dakota
stateNames[41](0x618588): Tennessee
stateNames[42](0x618590): Texas
stateNames[43](0x618598): Utah
stateNames[44](0x6185a0): Vermont
stateNames[45](0x6185a8): Virginia
stateNames[46](0x6185b0): Washington
stateNames[47](0x6185b8): Washington DC
stateNames[48](0x6185c0): West Virginia
stateNames[49](0x6185c8): Wisconsin
stateNames[50](0x6185d0): Wyoming
myStrCmp: s1(0x415430): Alabama, s2(0x618508):
UA
myStrCmp: s1(0x415430): Alabama, s2(0x618570): A
myStrCmp: s1(0x415430): Alabama, s2(0x618540): PUA
myStrCmp: s1(0x415430): Alabama, s2(0x618528): 1UA
myStrCmp: s1(0x415430): Alabama, s2(0x618538): GUA
myStrCmp: s1(0x415430): Alabama, s2(0x618530): <UA
As you can see, the locations visited by bsearch in the course of its search should have valid strings (as was just checked before calling bsearch), but the output if you try to print the char * at that location is garbage. Can anyone see my mistake? Incidentally I get the same bad behaviour (but don't get to follow it as closely obviously) when I call bsearch with the final parameter set to:
(int(*)(const void*, const void*))strcmp
Thanks!
Since you are using an array of const char *, bsearch() will pass to the comparison function a pointer to those elements. In other words, it will receive const char * const * in its second argument.
int myStrCmp(const void *s1, const void *s2) {
const char *key = s1;
const char * const *arg = s2;
printf("myStrCmp: s1(%p): %s, s2(%p): %s\n", s1, key, s2, *arg);
return strcmp(key, *arg);
}
Your state name (or key) needs to be a pointer to a pointer. Didn't have to add/remove constanywhere. myStrCmpneeds to dereference by one to compare the strings. The code below does what you want I think. Please let me know if not, thanks.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *stateNames[] = {"Alabama", "Alaska", "Arizona", "Arkansas","California", "Colorado", "Connecticut", "Delaware", "Florida","Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "Washington DC", "West Virginia", "Wisconsin", "Wyoming"};
int myStrCmp(const void *s1, const void *s2) {
printf("myStrCmp: s1(%p): %s, s2(%p): %s\n", s1, *(char **)s1, s2, *(char**)s2);
return strcmp(*(char **) s1, *(char **) s2);
}
int determineState(char *state) {
printf("state: %s\n", state);
for(int i = 0; i < 51; i++)
printf("stateNames[%i](%p): %s\n", i, &(stateNames[i]), stateNames[i]);
char **found = (char **) bsearch(&state, stateNames, 51, sizeof(char *), myStrCmp );
if(found == NULL){
return -1;
} else {
printf("Found it!: %s\n", *found);
}
return 0;
}
int main(int argc, const char * argv[]) {
determineState("Alabama");
}