on the line " int RandomA, RandomB" I keep on getting an error. The error says Expected identifier or "(". The same error also pop up between the struct card and the name of the cards. I'm not sure how to fix it. This is my first time asking a question on this website. I'm using mac osx application command line tool.
main.c file
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct card {
char* name;
char* suit;
int value;
};
void shuffle(struct card* deck){
// Seed the random number generator
srandom(time (NULL ) );
int i = 0;
int randomA, randomB;
struct card tempCard;
do {
// Generate 2 random numbers to determine which cards to swap
randomA = random() % 52;
randomB = random() % 52;
// Swap slots A and B
tempCard = deck[randomA];
deck[randomA] = deck[randomB];
deck[randomB] = tempCard;
// Increment the counter
++i;
}
while (i<1000000);
}
void printDeck(struct card* deck){
// Print out the shuffled deck
int i=0;
while (i<52) {
if (deck[i].value == 1) {
printf("The ace of %s is great!\n", deck[i].suit);
}
else {
switch (deck[i].value){
case 11:
printf("A Jack of all trades (%s)\n",deck[i].suit);
break;
case 12:
printf("A Queen of the castle (%s)\n",deck[i].suit);
break;
case 13:
printf("The King of the world (%s)\n",deck[i].suit);
break;
default:
printf("The card is %s of %s\n", deck[i].name,deck[i].suit);
break;
}
}
i++;
}
}
int main(int argc, const char * argv[]);
{
struct card deck[] =
{
{"ace", "spades", 1}, {"two", "spades", 2}, {"three", "spades", 3},
{"four", "spades", 4}, {"five", "spades", 5}, {"six", "spades", 6},
{"seven", "spades", 7}, {"eight", "spades", 8}, {"nine", "spades", 9},
{"ten", "spades", 10}, {"jack", "spades", 11}, {"queen", "spades", 12},
{"king", "spades", 13},
{"ace", "clubs", 1}, {"two", "clubs", 2}, {"three", "clubs", 3},
{"four", "clubs", 4}, {"five", "clubs", 5}, {"six", "clubs", 6},
{"seven", "clubs", 7}, {"eight", "clubs", 8}, {"nine", "clubs", 9},
{"ten", "clubs", 10}, {"jack", "clubs", 11}, {"queen", "clubs", 12},
{"king", "clubs", 13},
{"ace", "hearts", 1}, {"two", "hearts", 2}, {"three", "hearts", 3},
{"four", "hearts", 4}, {"five", "hearts", 5}, {"six", "hearts", 6},
{"five", "hearts", 7}, {"eight", "hearts", 8}, {"nine", "hearts", 9},
{"ten", "hearts", 10}, {"jack", "hearts", 11}, {"queen", "hearts", 12},
{"king", "hearts", 13},
{"ace", "diamonds", 1}, {"two", "diamonds", 2}, {"three", "diamonds", 3},
{"four", "diamonds", 4}, {"five", "diamonds", 5}, {"six", "diamonds", 6},
{"seven", "diamonds", 7}, {"eight", "diamonds", 8},
{"nine", "diamonds", 9}, {"ten", "diamonds", 10}, {"jack", "diamonds", 11},
{"queen", "diamonds", 12},{"king", "diamonds", 13}};
// Run the function to shuffle the deck
shuffle(deck);
// Print the deck
printDeck(deck);
return 0;
}
Perhaps this:
int main(int argc, const char * argv[]) /* -------> */ ; /* <------- */
Followed by {, which opens up a scope outside of a context...
Related
I am working on a sudoku exercise using the backtracking algorithm using C. It prints the gameboard with the first solution it finds, however it does not stop there and continues the recursion by removing the fields of the gameboard and creates a new solution. The problem is somewhere in the solve function. Here is my approach:
#include "sudoku.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int field[SIZE][SIZE];
int initial[SIZE][SIZE];
/* Initializes the sudoku array.
* The field initial keeps the original start value for
* figuring out if a value is fixed or can be changed. */
void init(int begin[SIZE][SIZE]) {
memcpy(field, begin, SIZE * SIZE * sizeof(int));
memcpy(initial, begin, SIZE * SIZE * sizeof(int));
}
/* Retrieves the solution. NEEDED FOR AUTO-TESTING PURPOSES. */
void getResult(int result[SIZE][SIZE]){
memcpy(result, field, SIZE * SIZE * sizeof(int));
}
/* pretty prints the sudoku array */
void print() {
int row, col;
// print the first line
printf("||");
for (col = 0; col < SIZE - 1; col++) {
if (col % SQRT_SIZE == SQRT_SIZE - 1)
printf("===++");
else
printf("===+");
}
printf("===||\n");
// loop through all rows of the array
for (row = 0; row < SIZE; row++) {
// print the line with field values
for (col = 0; col < SIZE; col++) {
if (col % SQRT_SIZE == 0)
printf("|| ");
else
printf("| ");
if (field[row][col] == 0)
printf(" ");
else
printf("%d ", field[row][col]);
}
// print the separation line;
// depending on the result of the modulo operation
// print a single or double line
printf("||\n||");
if (row % SQRT_SIZE == SQRT_SIZE - 1) {
for (col = 0; col < SIZE - 1; col++) {
if (col % SQRT_SIZE == SQRT_SIZE - 1)
printf("===++");
else
printf("===+");
}
printf("===||\n");
}
else {
for (col = 0; col < SIZE - 1; col++) {
if (col % SQRT_SIZE == SQRT_SIZE - 1)
printf("---++");
else
printf("---+");
}
printf("---||\n");
}
}
}
/* Checks if the value is valid and can be set into the field.
* The function returns false if the value is already present or
* has been one of the initial values. */
int checkValueInField(int value, int row, int col) {
int i, r, c;
int squareRow;
int squareCol;
// checks for initial values
if (initial[row][col] != 0) {
if (initial[row][col] == value)
return 1;
else
return 0;
}
// check horizontally
for (i = 0; i < SIZE; i++) {
if (field[row][i] == value) return 0;
}
// check vertically
for (i = 0; i < SIZE; i++) {
if (field[i][col] == value) return 0;
}
// check square
squareRow = row / SQRT_SIZE;
squareCol = col / SQRT_SIZE;
for (r = squareRow * SQRT_SIZE; r < squareRow * SQRT_SIZE + SQRT_SIZE; r++) {
for (c = squareCol * SQRT_SIZE; c < squareCol * SQRT_SIZE + SQRT_SIZE; c++) {
if (field[r][c] == value) return 0;
}
}
return 1;
}
/* Set a value in the sudoku field if the field is empty.
* The method returns false if the field contains a fixed number. */
int setValueInField(int value, int row, int col) {
if (initial[row][col] == 0) {
field[row][col] = value;
return 1;
}
else if (initial[row][col] == value)
return 1;
return 0;
}
/* Removes a value in the sudoku field if it doesn't contain an initial value.
* The method returns false if the field contains a fixed number and cannot be
* removed. */
int removeValueFromField(int row, int col) {
if (initial[row][col] == 0) {
field[row][col] = 0;
return 1;
}
return 0;
}
/* Returns the value in the field */
int getValueFromField(int row, int col) {
return field[row][col];
}
/* Return true after a valid solution for the sudoku has been found. */
int solve(int row, int col) {
print();
bool isSet = false;
if (row == 0 && col == 0) {
for (int i = 1; i <= SIZE; i++) {
if (checkValueInField(i, row, col) && getValueFromField(row, col) == 0) {
isSet = setValueInField(i, row, col);
if (isSet) {
solve(row, col);
}
if (row == SIZE - 1 && col == SIZE - 1) {
return 1;
}
else {
removeValueFromField(row, col);
}
}
}
}
if (row == SIZE - 1 && col == SIZE - 1) {
//Here is the solution, but then it continues after returning 1
for (int r = 1; r <= SIZE; r++) {
for (int c = 1; c <= SIZE; c++) {
if (getValueFromField(r, c) != 0) {
return 1;
}
}
}
}
if (row == SIZE - 1) {
row = 0;
col = col++;
}
else {
row = row++;
}
for (int i = 1; i <= SIZE; i++) {
if (checkValueInField(i, row, col) == 1) {
isSet = setValueInField(i, row, col);
solve(row, col);
if (row == SIZE - 1 && col == SIZE == -1) {
return 1;
}
else {
removeValueFromField(row, col);
}
}
}
return 1;
}
This is my header file:
#ifndef _SUDOKU_H_
#define _SUDOKU_H_
#define SIZE 9
#define SQRT_SIZE 3
void init(int begin[SIZE][SIZE]);
void getResult(int result[SIZE][SIZE]);
void print();
int checkValueInField(int value, int row, int col);
int setValueInField(int value, int row, int col);
int removeValueFromField(int row, int col);
int getValueFromField(int row, int col);
int solve(int row, int col);
This is the initial gameboard and its solution:
int initial[SIZE][SIZE] = {
{0, 1, 0, 0, 0, 9, 0, 5, 0},
{0, 9, 0, 0, 0, 0, 4, 8, 0},
{0, 6, 0, 1, 0, 4, 0, 0, 0},
{0, 0, 5, 0, 0, 0, 9, 3, 0},
{0, 0, 0, 7, 0, 2, 0, 0, 0},
{0, 2, 1, 0, 0, 0, 8, 0, 0},
{4, 0, 0, 0, 8, 0, 6, 0, 9},
{0, 0, 0, 0, 6, 0, 5, 0, 3},
{2, 0, 0, 0, 3, 0, 0, 0, 0},
};
int expected[SIZE][SIZE] = {
{3, 1, 4, 8, 7, 9, 2, 5, 6},
{5, 9, 7, 3, 2, 6, 4, 8, 1},
{8, 6, 2, 1, 5, 4, 3, 9, 7},
{7, 4, 5, 6, 1, 8, 9, 3, 2},
{9, 3, 8, 7, 4, 2, 1, 6, 5},
{6, 2, 1, 5, 9, 3, 8, 7, 4},
{4, 7, 3, 2, 8, 5, 6, 1, 9},
{1, 8, 9, 4, 6, 7, 5, 2, 3},
{2, 5, 6, 9, 3, 1, 7, 4, 8},
};
This is my testcase:
TEST_CASE("Test2", "Sudoku")
{
int result = 0;
int actual[SIZE][SIZE];
int initial[SIZE][SIZE] = {
{0, 1, 0, 0, 0, 9, 0, 5, 0},
{0, 9, 0, 0, 0, 0, 4, 8, 0},
{0, 6, 0, 1, 0, 4, 0, 0, 0},
{0, 0, 5, 0, 0, 0, 9, 3, 0},
{0, 0, 0, 7, 0, 2, 0, 0, 0},
{0, 2, 1, 0, 0, 0, 8, 0, 0},
{4, 0, 0, 0, 8, 0, 6, 0, 9},
{0, 0, 0, 0, 6, 0, 5, 0, 3},
{2, 0, 0, 0, 3, 0, 0, 0, 0},
};
int expected[SIZE][SIZE] = {
{3, 1, 4, 8, 7, 9, 2, 5, 6},
{5, 9, 7, 3, 2, 6, 4, 8, 1},
{8, 6, 2, 1, 5, 4, 3, 9, 7},
{7, 4, 5, 6, 1, 8, 9, 3, 2},
{9, 3, 8, 7, 4, 2, 1, 6, 5},
{6, 2, 1, 5, 9, 3, 8, 7, 4},
{4, 7, 3, 2, 8, 5, 6, 1, 9},
{1, 8, 9, 4, 6, 7, 5, 2, 3},
{2, 5, 6, 9, 3, 1, 7, 4, 8},
};
init(initial);
result = solve(0,0);
getResult(actual);
INFO("Test Case: valid Sudoku board failed.");
REQUIRE(memcmp(actual, expected, sizeof(initial)) == 0);
INFO("Test Case: return value not correct.");
REQUIRE(result == 1);
}
I want to initialise all three arrays of mainstr , can anyone help me to initialise this anonymous union inside the struture? 0th index should initialise with interger array and 1st and 2nd indexes with char pointer.
typedef struct
{
int testy;
union
{
int a[3];
char* b[3];
}
bool testz;
} testStr;
typedef struct
{
testStr x[3];
} mainStr;
something like this,
mainStr test = {
{20, {{10, 20, 30}}, FALSE},
{10, {{"test1", "test2", NULL}}, TRUE},
{30, {{"test3", "test4", NULL}}, FALSE},
}
Use designators:
mainStr test = {{
{20, {{10, 20, 30}}, false},
{10, {.b={"test1", "test2", NULL}}, true},
{30, {.b={"test3", "test4", NULL}}, false},
}};
Demo
A bit of work, but do-able.
#include <stdbool.h>
int foo() {
typedef struct {
int testy;
union {
int a[3];
char *b[3];
};
bool testz;
} testStr;
typedef struct {
testStr x[3];
} mainStr;
testStr test1 = {20, { {10, 20, 30}}, false};
(void) test1;
mainStr test = { //
{ //
{20, { {10, 20, 30}}, false}, //
{10, { .b={"test1", "test2", NULL}}, true}, //
{30, { .b={"test3", "test4", NULL}}, false} //
}//
};
(void) test;
}
Some issues:
union { ... }; must end with semicolon.
Use standard bool not some home-made version.
mainStr Means there's a need for an extra pair of initializers, one for the struct, one for the array member x.
With those issues fixed, you can use designated initializers to tell the compiler which union member you are initializing:
#include <stdbool.h>
typedef struct {
int testy;
union {
int a[3];
char* b[3];
};
bool testz;
} testStr;
typedef struct {
testStr x[3];
} mainStr;
int main (void) {
mainStr test =
{
{
{20, .a = {10, 20, 30}, false},
{10, .b = {"test1", "test2", 0}, true},
{30, .b = {"test3", "test4", 0}, false},
}
};
}
It might be a good idea to use designated initializers for all struct/union members though, to get self-documenting code.
I got a problem getting a loop done in Mathematica while using the SNEG package (it's for calculus with fermionic operators).
Here's a little bit of my code:
natord = {{cjdckd, 1, 2}, {cjdckd, 1, 3}, {cjdckd, 1, 4}, {cjdck, 1,
1}, {cjdck, 1, 2}, {cjdck, 1, 3}, {cjdck, 1, 4},
{cjdckd, 2, 3}, {cjdckd, 2, 4}, {cjdck, 2, 1}, {cjdck, 2,
2}, {cjdck, 2, 3}, {cjdck, 2, 4}, {cjdckd, 3, 4}, {cjdck, 3,
1}, {cjdck, 3, 2}, {cjdck, 3, 3}, {cjdck, 3, 4}, {cjdck, 4,
1}, {cjdck, 4, 2}, {cjdck, 4, 3}, {cjdck, 4, 4}, {cjck, 1,
2}, {cjck, 1, 3}, {cjck, 1, 4}, {cjck, 2, 3}, {cjck, 2, 4}, {cjck,
3, 4}};
Do[
j = natord[[ind, 2]] // ToString;
k = natord[[ind, 3]] // ToString;
cjd = c[0, j];
cj = c[1, j];
ckd = c[0, k];
ck = c[1, k];
cjck = nc[cj, ck];
cjdck = nc[cjd, ck];
cjdckd = nc[cjd, ckd];
korr = natord[[ind, 1]];
After this the following code generates a list of coefficients and one single value.
The list should be appended to a List M, so I have matrix in the end and the single value is going to be in a list so I get a vector.
When I'm not doing a Do-Loop and just saying
j="1";
k="2";
and
korr=cjck;
it somehow this will do it.
As you can see in the natord list, I got 28 cases which I don't want to put in each explicitly but creating a Loop doing this for me.
Hope you can help me. If you need more code tell me.
Cheers
Your code sample doesn't reveal any problem you may be encountering with Append or AppendTo.
m = {};
Do[AppendTo[m, natord[[i]]], {i, Length[natord]}]
m == natord
True
I'm genuinly confused as to why i am having this error. My code compiles fine but i would prefer to have 0 warnings. After searching stackoverflow for a bit no one had the same structure type that i was using. Below is a snippit of my code (these are made up names)
if it helps my MAXCONTACTS is set to 5
struct Contact conNum[MAXCONTACTS] = { { {"Rick", { '\0' }, "Grimes" },
{11, "Trailer Park", 0, "A7A 2J2", "King City" },
{"4161112222", "4162223333", "4163334444" } },
{
{"Maggie", "R.", "Greene" },
{55, "Hightop House", 0, "A9A 3K3", "Bolton" },
{"9051112222", "9052223333", "9053334444" } },
{
{"Morgan", "A.", "Jones" },
{77, "Cottage Lane", 0, "C7C 9Q9", "Peterborough"},
{"7051112222", "7052223333", "7053334444" } },
{
{"Sasha", {'\0'}, "Williams" },
{55, "Hightop House", 0, "A9A 3K3", "Bolton"},
{"9052223333", "9052223333", "9054445555" } },
};
edit: here is my structure declaration
// Structure type Name declaration
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
struct Address {
int streetNumber;
int apartmentNumber;
char street[41];
char postalCode[8];
char city[41];
};
struct Numbers {
char cell[11];
char home[11];
char business[11];
};
struct Contact {
struct Name name;
struct Address address;
struct ``Numbers numbers;
};
Your struct Address contains two int fields followed by two char arrays:
struct Address {
int streetNumber;
int apartmentNumber;
char street[41];
char postalCode[8];
char city[41];
};
but you don't initialize them in that order:
{11, "Trailer Park", 0, "A7A 2J2", "King City" },
Unless you use named initializers, fields must be specified in order. Put the street number first, then the apartment number, then the street:
{11, 0, "Trailer Park", "A7A 2J2", "King City" },
Do the same with the other three.
{11, "Trailer Park", 0, "A7A 2J2", "King City" },
this initialization is for
struct Address {
int streetNumber;
int apartmentNumber;
char street[41];
char postalCode[8];
char city[41];
};
As you see you are trying to initialize "Trailer Park" to int apartmentNumber;.
That is why you are getting warning.
Thus change the definition of structure to
struct Address {
int streetNumber;
char street[41];
int apartmentNumber;
char postalCode[8];
char city[41];
};
At only one place
or
change the initialization sequence like below in all the places.
{11, 0, "Trailer Park", "A7A 2J2", "King City" },
Would it be possible to have a TogglerBar instead of the 2 Check Box to show or not the different Shapes.
With Green & Red written in each Button of the TogglerBar ?
Manipulate[
Graphics[{If[thePink, {Pink, Disk[{5, 5}, 3]}],
If[theGreen, {Green, Disk[{15, 2}, 1]}]},
PlotRange -> {{0, 20}, {0, 10}}], {{thePink, True,
Style["Pink", Black, Bold, 12]}, {True, False}}, {{theGreen, True,
Style["Green", Black, Bold, 12]}, {True, False}}]
The actual Manipulate object I am trying to adjust can be found there : http://www.laeh500.com/LAEH/COG.html
The purpose being to replace the CheckBox by a nice TogglerBar.
Like this?
Manipulate[
Graphics[{
{White, Circle[{5, 5}, r]}, (* For Mma 7 compatibility*)
If[MemberQ[color, "Red"], {Pink, Disk[{5, 5}, r]}],
If[MemberQ[color, "Green"], {Green, Disk[{4, 2}, r]}]},
PlotRange -> {{0, 20}, {0, 10}}],
{{r, 1, "Radius"}, 1, 5, 1, ControlType -> Setter},
{{color, "Red", "Color"}, {"Red", "Green"}, ControlType -> TogglerBar},
LabelStyle -> Large]
Edit
Answering your comment, I think your notebook could benefit from a template like this one:
Manipulate[
Graphics[
{
{White, Circle[{5, 5}, r]},(* For Mma 7 compatibility*)
If[MemberQ[whatToDisplay, "Circle"], {Red, Circle [{5, 5}, r]}],
If[MemberQ[whatToDisplay, "Square"], {Blue, Rectangle[{5, 5}, {r, r}]}],
If[MemberQ[whatToDisplay, "Other"], {Black, Line [Tuples[{3, 4}, 2]]}],
},
PlotRange -> {{0, 20}, {0, 10}}
],
(* Controls follow *)
{{r, 1, Style["Radius", Black, Bold, 12]}, 1, 5, 1, ControlType -> Slider
, ControlPlacement-> Top
},
Control#{{whatToDisplay, True, Style["What", Black, Bold, 12]},
{"Circle", "Square", "Other"},
ControlType -> TogglerBar,
Appearance -> "Vertical",
ControlPlacement -> Left
}
]
How about this?
Manipulate[
Show[Graphics[myObject],
PlotRange -> {{0, 20}, {0, 10}}], {{myObject, {},""}, {{Pink,
Disk[{5, 5}, 3]} ->
Style["Pink", Black, Bold, 12], {Green, Disk[{15, 2}, 1]} ->
Style["Green", Black, Bold, 12]}}, ControlType -> TogglerBar]
How about
Manipulate[
Graphics[{#} & /# x,
PlotRange -> {{0, 20}, {0, 10}}],
{{x, {}, "Colour"},
{{Pink, Disk[{5, 5}, 3]} \[Rule] "Pink",
{Green, Disk[{15, 2}, 1]} \[Rule] "Green"},
ControlType -> TogglerBar}]
it's ugly and inelegant, though! Dynamic manipulation is not my favourite use of Mathematica, so this is sort of trial and error for me too...
EDIT: Slightly less ugly now...
EDIT2: Added a label