Why is the second for-loop not executing? - c

The issue in the code below is that the second for-loop does not seem to execute.
I added the printf("1"); and printf("h"); statements to test if the loops were executing. The program printed the 1's but not the h's.
Some background info about the program is that I am supposed to allocate a 2D array and store sine waves at different frequencies in each row of the array.
Also the variable PI is defined as a macro with value acos(-1).
short int** Notes = (short int**) calloc(25,sizeof(short int*));
for(int x = 0; x < 25; x++){
Notes[x] = (short int*) calloc(44100 * (1/3),sizeof(short int));
}
for(int x = 0; x < 25; x++){
float freq;
switch(x){
case 0:
freq = 440.00;
break;
case 1:
freq = 466.16;
break;
case 2:
freq = 493.88;
break;
case 3:
freq = 523.25;
break;
case 4:
freq = 554.37;
break;
case 5:
freq = 587.33;
break;
case 6:
freq = 622.25;
break;
case 7:
freq = 659.25;
break;
case 8:
freq = 698.46;
break;
case 9:
freq = 739.99;
break;
case 10:
freq = 783.99;
break;
case 11:
freq = 830.61;
break;
case 12:
freq = 880.00;
break;
case 13:
freq = 932.33;
break;
case 14:
freq = 987.77;
break;
case 15:
freq = 1046.50;
break;
case 16:
freq = 1109.73;
break;
case 17:
freq = 1174.66;
break;
case 18:
freq = 1244.51;
break;
case 19:
freq = 1318.51;
break;
case 20:
freq = 1396.91;
break;
case 21:
freq = 1479.98;
break;
case 22:
freq = 1567.98;
break;
case 23:
freq = 1661.22;
break;
case 24:
freq = 0.00;
break;
}
printf("1");
for(int y = 0; y < 44100 * (1/3); y++)
{
Notes[x][y] = 32700 * sin(2 * PI * freq * (y/44100));
printf("h");
}
}

for(int y = 0; y < 44100 * (1/3); y++)
The substatement never executes because y < 44100 * (1/3) is always false. This is because integer division rounds toward zero, so (1/3) and therefore 44100 * (1/3) are zero.
Assuming you want the value which is one-third of 44100, just write 44100/3.

Dividing two integers will result in an integer:
for(int y = 0; y < 44100 * (1/3); y++)
is evaluated as:
for(int y = 0; y < 44100 * (0); y++)
So you never enter the loop

for(int y = 0; y < 44100 * (1/3); y++)
Here the testExpression y < 44100 * (1/3) always evaluate to false.
As 1/3 as both will result in zero as it is integer division and dividing two integers will result integer.
You can use
for(int y = 0; y < 44100 * (1.0/3); y++)

Related

Program that moves the knight on the chessboard

This program changes the position of a chessboard from 0 to 1 when the knight moves from one position to another. If I try the case 0 the program works. Then after the case 0 if I try the case 1 the position changed to 1 is the one that is placed up-left from the case 0 one. Instead it should be the one placed up-up-right from the case 0 one. Why is the output like this?
#include <stdio.h>
int main(){
int board[8][8]={0};
int currentRow=4, currentColumn=4;
int cont=0, moveNumber=0, i, j;
while(moveNumber>=0 && moveNumber<=7){
printf("Enter a move: ");
scanf("%d", &moveNumber);
cont++;
switch(moveNumber){
case 0:
board[currentRow-1][currentColumn+2]=1;
break;
case 1:
board[currentRow-2][currentColumn+1]=1;
break;
case 2:
board[currentRow-2][currentColumn-1]=1;
break;
case 3:
board[currentRow-1][currentColumn-2]=1;
break;
case 4:
board[currentRow+1][currentColumn-2]=1;
break;
case 5:
board[currentRow+2][currentColumn-1]=1;
break;
case 6:
board[currentRow+2][currentColumn+1]=1;
break;
case 7:
board[currentRow+1][currentColumn+2]=1;
break;
}
for(i=0; i<8; i++){
for(j=0; j<8; j++){
printf("%d ", board[i][j]);
}
printf("\n");
}
printf("Total moves: %d\n",cont);
}
return 0;
}
The main issue is that you never update currentRow or currentColumn, so all moves happen from their initial values.
Some other notes: You should avoid "magic numbers" - 8 in this case. If you decide to change the size of the array, you have to search through all the code and find the eights and replace them. Use a define or const int.
You should always check the return value from scanf. What if the user types a, for example?
Array bounds checking is needed. What happens when a move takes the knight off the board - and past the bounds of the array?
#include <stdio.h>
// Avoid magic numbers
#define ROWS 8
#define COLS 8
int main(){
int board[ROWS][COLS] = {0};
int currentRow = 4, currentColumn = 4;
int cont = 0, moveNumber = 0, i, j;
// Loop forever
while (1) {
printf("Enter a move: ");
// Make sure user enters a number
while (1 != scanf("%d", &moveNumber)) {
// clear stdin
int c;
while((c = getchar()) != '\n' && c != EOF);
// See https://stackoverflow.com/a/6277391/669576
// for why fgets/sscanf is a better option than scanf
// Prompt user for new input
printf("Enter a valid integer:");
}
// Moved this here
if (moveNumber < 0 || moveNumber > 7) break;
cont++;
// Going to use some temp vars to calculate indices
int tempRow, tempCol;
// Calc new indices
switch (moveNumber) {
case 0:
tempRow = currentRow - 1;
tempCol = currentColumn + 2;
break;
case 1:
tempRow = currentRow - 2;
tempCol = currentColumn + 1;
break;
case 2:
tempRow = currentRow - 2;
tempCol = currentColumn - 1;
break;
case 3:
tempRow = currentRow - 1;
tempCol = currentColumn - 2;
break;
case 4:
tempRow = currentRow + 1;
tempCol = currentColumn - 2;
break;
case 5:
tempRow = currentRow + 2;
tempCol = currentColumn - 1;
break;
case 6:
tempRow = currentRow + 2;
tempCol = currentColumn + 1;
break;
case 7:
tempRow = currentRow + 1;
tempCol = currentColumn + 2;
break;
}
// Make sure we have valid indices
if (tempRow < 0 || tempCol < 0 || tempRow >= ROWS || tempCol >= COLS) {
printf("Illegal move\n");
}
else {
// Update the board
currentRow = tempRow;
currentColumn = tempCol;
board[currentRow][currentColumn] = 1;
// And print
for(i = 0; i < ROWS; i++){
for(j = 0; j < COLS; j++){
printf("%d ", board[i][j]);
}
printf("\n");
}
printf("Total moves: %d\n", cont);
}
}
return 0;
}
As #Jonhnny Mopp pointed out, you never update currentRow or currentColumn.
So after case 0 you set board[3][6] and after case 1 you set board[2][5].
So the return of case 1 is up-left of case 0.
I recommend you write a function TryToMoveKnight(int* x, int* y, int moveNumber) that attempts to move a knight in the desired direction.
You already have eight lines of very similar code, and the more work you need to do to move the piece, the worse it's going to get.
Things the function needs to do:
determine the target coordinates from the moveNumber and current
position
verify that the target coordinates are on the board
possibly check for a piece at that position
assign the new coordinates to your variables x and y whose addresses you passed in
This last step was missing from your original code, but checking that the target position is on the board is also going to be essential.
So where you currently have switch(moveNumber)... you would instead call TryToMoveKnight(&currentRow,&currentColumn,moveNumber);

Issues with state machine in c [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm facing some issues with my state machine (function automate()). It seems that the enum always keep the same values instead of changing it depending on differents conditions in the function.
My output is always "DECREMENT_PARA_1" Can anyone tell me why the variable etat_courant doesn't keep the value in memory for each iteration please ? Thanks in advance ! Here is my code :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//PARAMETRES
enum Etat {INIT, DECREMENT_PARA_1, INCREMENT_PARA_1, ETAT_INVARIANT_1, DECREMENT_PARA_2, ETAT_INVARIANT_2, INCREMENT_PARA_2, RADAR_SATURE};
//Prototypes
void automate(int tddv_estime, int tab_para_automate[][2], enum Etat *etat_courant, int* para_1_courant, int* para_2_courant);
void fct_test(int scenario[]);
int main()
{
int scenario[15] = {21, 21, 20, 12, 12, 20, 22, 22, 22, 22, 22, 22, 22, 22, 500};
fct_test(scenario);
return 0;
}
/*-----------------------------------------------------------------------
Fonction fct_test
-------------------------------------------------------------------------*/
void fct_test(int scenario[])
{
int increment = 1;
int tddv_estime;
int para_1_courant = 10;
int para_2_courant = 4;
int para_1_min = 5;
int para_1_max = 10;
int para_2_min = 1;
int para_2_max = 4;
int tab_para_automate[2][2] = {{0}};
int tab_para_application[1][3] = {{0}};
tab_para_automate[0][0] = para_1_min;
tab_para_automate[0][1] = para_1_max;
tab_para_automate[1][0] = para_2_min;
tab_para_automate[1][1] = para_2_max;
tab_para_application[0][0] = para_1_courant;
tab_para_application[0][1] = para_2_courant;
tab_para_application[0][2] = increment;
int i;
enum Etat etat_courant = INIT;
for (i=0 ; i<15 ; i++)
{
tddv_estime = scenario[i];
printf("%d\n",scenario[i]);
automate(tddv_estime, tab_para_automate, &etat_courant, &para_1_courant, &para_2_courant);
}
//}
}
/*-----------------------------------------------------------------------
Fonction automate
-------------------------------------------------------------------------*/
void automate(int tddv_estime, int tab_para_automate[][2], enum Etat *etat_courant, int* para_1_courant, int* para_2_courant)
{
int evenement;
int tddv_worst = 20;
if (tddv_estime < tddv_worst)
evenement = 1; //Etat initial
if (tddv_estime > tddv_worst)
evenement = 2; //Decrement para1
if (tddv_estime < tddv_worst && *para_1_courant<= tab_para_automate[0][1])
evenement = 3; //Increment para1
if (tddv_estime == tddv_worst)
evenement = 4; //Etat Invariant 1
if (tddv_estime > tddv_worst && *para_1_courant <= tab_para_automate[0][0])
evenement = 5; //Decrement para_2
if (tddv_estime < tddv_worst && *para_2_courant <= tab_para_automate[1][1])
evenement = 6; //Increment para2
if (tddv_estime == tddv_worst)
evenement = 7; //Etat Invariant 2
if (tddv_estime > tddv_worst && *para_1_courant<= tab_para_automate[0][0] && *para_2_courant<= tab_para_automate[1][0])
evenement = 8; //Etat radar sature
switch (*etat_courant) {
case INIT:
switch(evenement)
{
case 1:
*etat_courant = INIT;
printf("ETAT_INITIAL\n");
break;
case 2:
*etat_courant = DECREMENT_PARA_1;
printf("DECREMENT_PARA_1\n");
break;
}
break;
case DECREMENT_PARA_1:
switch(evenement)
{
case 5:
*etat_courant = DECREMENT_PARA_2;
printf("DECREMENT_PARA_2\n");
break;
case 4:
*etat_courant = ETAT_INVARIANT_1;
printf("ETAT_INVARIANT_1\n");
break;
case 3:
*etat_courant = INCREMENT_PARA_1;
printf("INCREMENT_PARA_1\n");
break;
case 2:
*etat_courant = DECREMENT_PARA_1;
printf("DECREMENT_PARA_1\n");
break;
}
break;
case INCREMENT_PARA_1:
switch(evenement)
{
case 4:
*etat_courant = ETAT_INVARIANT_1;
printf("ETAT_INVARIANT_1\n");
break;
case 3:
*etat_courant = INCREMENT_PARA_1;
printf("INCREMENT_PARA_1\n");
break;
case 2:
*etat_courant = DECREMENT_PARA_1;
printf("DECREMENT_PARA_1\n");
break;
}
break;
case ETAT_INVARIANT_1:
switch(evenement)
{
case 2:
*etat_courant = DECREMENT_PARA_1;
printf("DECREMENT_PARA_1\n");
break;
case 4:
*etat_courant = ETAT_INVARIANT_1;
printf("ETAT_INVARIANT_1\n");
break;
case 3:
*etat_courant = INCREMENT_PARA_1;
printf("INCREMENT_PARA_1\n");
break;
}
break;
case DECREMENT_PARA_2:
switch(evenement)
{
case 5:
*etat_courant = DECREMENT_PARA_2;
printf("DECREMENT_PARA_2\n");
break;
case 7:
*etat_courant = ETAT_INVARIANT_2;
printf("ETAT_INVARIANT_2\n");
break;
case 6:
*etat_courant = INCREMENT_PARA_2;
printf("INCREMENT_PARA_2\n");
break;
case 8:
*etat_courant = RADAR_SATURE;
printf("RADAR_SATURE\n");
break;
}
break;
case ETAT_INVARIANT_2:
switch(evenement)
{
case 5:
*etat_courant = DECREMENT_PARA_2;
printf("DECREMENT_PARA_2\n");
break;
case 7:
*etat_courant = ETAT_INVARIANT_2;
printf("ETAT_INVARIANT_2\n");
break;
case 6:
*etat_courant = INCREMENT_PARA_2;
printf("INCREMENT_PARA_2\n");
break;
}
break;
case INCREMENT_PARA_2:
switch(evenement)
{
case 5:
*etat_courant = DECREMENT_PARA_2;
printf("DECREMENT_PARA_2\n");
break;
case 7:
*etat_courant = ETAT_INVARIANT_2;
printf("ETAT_INVARIANT_2\n");
break;
case 6:
*etat_courant = INCREMENT_PARA_2;
printf("INCREMENT_PARA_2\n");
break;
case 3:
*etat_courant = INCREMENT_PARA_1;
printf("INCREMENT_PARA_1\n");
break;
}
break;
default :
exit(1);
break;
}
}
Can anyone tell me why the variable etat_courant doesn't keep the value in memory for each iteration please ?
It actually is keeping the value in memory: it can not do otherwise, etat_courant will be kept in memory until fct_test(...) returns. The reason the output is always DECREMENT_PARA_1 is purely due to the logic in your automate() function.
For example, you have a redundant comparison in your automate() function - which is overriding a previous assignment of evenement.
if (tddv_estime == tddv_worst)
evenement = 4; //Etat Invariant 1
// ...
if (tddv_estime == tddv_worst)
evenement = 7; //Etat Invariant 2
If - and this is just hypothetical (whether or not this is the right thing to do depends on what you are trying to achieve) - you remove the second comparison, the output will vary at least once.
As an aside ... It is safer to pass the size of the array to your fct_test(...) function - as in
int main(void) {
int scenario[] = {21, 21, 20, 12, 12, 20, 22, 22,
22, 22, 22, 22, 22, 22, 500};
fct_test(scenario, sizeof(scenario)/sizeof(scenario[0]));
return 0;
}
void fct_test(int *scenario, size_t array_len) {
// Etc. (ommitted for brevity )
for (i = 0; i < array_len; i++) {
tddv_estime = scenario[i];
// etc.
}
}
(Note that I added a return 0 in your main() also - which you were missing.)

Counting vowels in text without arrays (C)

i have to write a program that counts all vowels in a text & gives out the percentage of every vowel for the whole text.
For whatever reason we are not allowed to use arrays, but instead should do it with getchar().
#include <stdio.h>
#include <ctype.h>
int main() {
int current;
int cntAll = 0;
int cntA = 0, cntE = 0, cntI = 0, cntO = 0, cntU = 0;
int pA = 0, pE = 0, pI = 0, pO = 0, pU = 0;
printf("Enter Text: ");
while ((current = getchar()) != EOF){
if (isspace(current)) continue; // check for whitespace, if whitespace continue
else {
switch (current) { // check for vowel & increase vowelcount
case 'a':
cntA += 1;
case 'A':
cntA += 1;
case 'e':
cntE += 1;
case 'E':
cntE += 1;
case 'i':
cntI += 1;
case 'I':
cntI += 1;
case 'o':
cntO += 1;
case 'O':
cntO += 1;
case 'u':
cntU += 1;
case 'U':
cntU += 1;
}
}
cntAll++;
}
pA = (cntA / cntAll) * 100;
pE = (cntE / cntAll) * 100;
pI = (cntI / cntAll) * 100;
pO = (cntO / cntAll) * 100;
pU = (cntU / cntAll) * 100;
printf("\nLetters: %d\nPercentage A: %d\nPercentage E: %d\nPercentage I: %d\nPercentage O: %d\nPercentage U: %d\n",cntAll,pA,pE,pI,pO,pU);
system("PAUSE");
return 0;
}
Increasing the cntAll value works without problems, but it doesn't count the individual vowels for whatever reason.
Would appreciate any help!
Edited:
#include <stdio.h>
#include <ctype.h>
int main() {
int current;
int cntAll = 0;
int cntA = 0, cntE = 0, cntI = 0, cntO = 0, cntU = 0;
double pA = 0, pE = 0, pI = 0, pO = 0, pU = 0;
printf("Enter Text: ");
while ((current = getchar()) != EOF){
if (isspace(current)) continue;
else {
switch (current) {
case 'a':case 'A':
cntA += 1;
break;
case 'e':case 'E':
cntE += 1;
break;
case 'i':case 'I':
cntI += 1;
break;
case 'o':case 'O':
cntO += 1;
break;
case 'u':case 'U':
cntU += 1;
break;
}
}
cntAll++;
}
pA = 100.0 * cntA / cntAll;
pE = 100.0 * cntE / cntAll;
pI = 100.0 * cntI / cntAll;
pO = 100.0 * cntO / cntAll;
pU = 100.0 * cntU / cntAll;
printf("\nLetters: %d\nPercentage A: %.2lf\nPercentage E: %.2lf\nPercentage I: %.2lf\nPercentage O: %.2lf\nPercentage U: %.2lf\n",cntAll,pA,pE,pI,pO,pU);
system("PAUSE");
return 0;
}
cheers
You need to insert break statements between the cases.
Otherwise the program will execute all the statements below the one first entered. Actually this is a good feature. It allows you to consider multiple labels at the same time. Putting this together you get:
switch (current){
case 'a': case 'A':
cntA += 1;
break; // Don't follow through to the other cases.
case 'b': case 'B': /*etc*/
After this, note that (cntA / cntAll) * 100; will evaluate the expression in parentheses in integer arithmetic, which will truncate it to 0 in most cases. The fix is to write it as
100 * cntA / cntAll;
This will still truncate to an integer. If that's not tolerable then consider using the floating point expression 100.0 * cntA / cntAll and change your printf formatters accordingly. Using floating point is arguably better anyway as it obviates the potential for overflow when evaluating 100 * cntA.
case labels falls through to the next one below it without a break.
So if you read an 'a' then all the cases in your switch will be executed.
You need something like
switch (current) { // check for vowel & increase vowelcount
case 'a':
cntA += 1;
break; // <-- Note break here
...
First thing i notice is that you are missing break on every switch case. This will lead to wrong behaviour.
Second thing:
pA = (cntA / cntAll) * 100;
will calculate cntS/cntAll first which is <0. This value will be interpreted as integer so you always have 0*100 which is 0. You can rewrite it as
pA = (cntA * 100 ) / cntAll;
In that case you don't have to cast to float to get the right result. Note that for large cntA you may overflow.

Simulating virtual machine, have trouble with incrementing pc versus jumps

I'm writing a virtual machine in C and I have all the various functions working, however I'm having trouble putting them together. Specifically I'm running into the problem that I need a way to increment the program counter without it interfering with the instructions that change what pc is pointing at like JMP, JPC, CAL, and RET. When I try to put in measures to cancel out pc++ like a PCvalueAfterJmp - 1 or if statements to not increment on those cases, it suddenly goes into an infinite loop which appears to be repeatedly going through the instructions.
This program reads in an input file and prints to the screen what instructions are being processed and the current state of the stack
int main(int argc, char* argv[]){
int running = 1;
int numInstructions = 0;
int lineRun;
int arcntr = 0;
//Memory
int stack[MAX_STACK_HEIGHT];
instruction code[MAX_CODE_LENGTH];
int arlist[MAX_STACK_HEIGHT];
//Registers
int sp=0;
int bp=1;
int pc=0;
instruction ir;
//Initializing ir
ir.op = 0;
ir.l = 0;
ir.m = 0;
//Initializing stack
stack[1] = 0;
stack[2] = 0;
stack[3] = 0;
//Reading the input file
numInstructions = readFile(argc, argv, code);
if(numInstructions < 0) //Exit with error if readFile returns invalid
return 1;
//show input code
printFile(code, numInstructions);
//setup and labeling
printState(-1, ir, pc, bp, sp, stack, arlist);
//Execution loop
while(running)
{
lineRun = pc;
//Fetch cycle
ir = code[pc];
//Execution cycle returns a nonzero to keep program running until end
if(!execOp(&sp, &bp, &pc, ir, code, stack, arlist, &arcntr))
running = 0;
//if statement didn't work
printState(lineRun, ir, pc, bp, sp, stack, arlist);
//if (!(ir.op == 5 || ir.op == 7 || ir.op == 8 || (ir.op == 2 && ir.m == 0)))
pc++;
}
return 0;
}
Here is my execution cycle
int execOp(int* sp, int* bp, int* pc, instruction ir, instruction code[],
int stack[], int arlist[], int* arcntr){
switch((opcode)ir.op){
case LIT:
stack[++(*sp)] = ir.m;
break;
case OPR: //Operators
switch((operator)ir.m){
case RET:
if(*bp == 1) //Kill the simulation if we're at the base level
return 0;
arlist[--(*arcntr)] = 0;
*sp = *bp - 1;
*pc = stack[*sp+3];
*bp = stack[*sp+2];
break;
case NEG:
stack[*sp] = -stack[*sp];
break;
case ADD:
(*sp)--;
stack[*sp] = stack[*sp] + stack[*sp+1];
break;
case SUB:
(*sp)--;
stack[*sp] = stack[*sp] - stack[*sp+1];
break;
case MUL:
(*sp)--;
stack[*sp] = stack[*sp] * stack[*sp+1];
break;
case DIV:
(*sp)--;
stack[*sp] = stack[*sp] / stack[*sp+1];
break;
case ODD:
stack[*sp] = stack[*sp] % 2;
break;
case MOD:
(*sp)--;
stack[*sp] = stack[*sp] % stack[(*sp)+1];
break;
case EQL:
(*sp)--;
stack[*sp] = stack[*sp] == stack[*sp+1];
break;
case NEQ:
(*sp)--;
stack[*sp] = stack[*sp] != stack[*sp+1];
break;
case LSS:
(*sp)--;
stack[*sp] = stack[*sp] < stack[*sp+1];
break;
case LEQ:
(*sp)--;
stack[*sp] = stack[*sp] <= stack[*sp+1];
break;
case GTR:
(*sp)--;
stack[*sp] = stack[*sp] > stack[*sp+1];
break;
case GEQ:
(*sp)--;
stack[*sp] = stack[*sp] >= stack[*sp+1];
break;
}
break;
case LOD:
stack[++*sp] = stack[base(ir.l, *bp, stack) + ir.m];
break;
case STO:
stack[base(ir.l, *bp, stack) + ir.m] = stack[(*sp)--];
break;
case CAL:
arlist[(*arcntr)++] = *sp + 1;
stack[*sp + 1] = base(ir.l, *bp, stack);
stack[*sp + 2] = *bp;
stack[*sp + 3] = *pc - 1;
*bp = *sp + 1;
*pc = ir.m;
break;
case INC:
*sp = *sp + ir.m;
break;
case JMP:
*pc = ir.m;
break;
case JPC:
if(!stack[(*sp)--])
*pc = ir.m;
break;
case SOI:
printf("%d\n", stack[(*sp)--]);
break;
case SIO:
scanf("%d", &stack[++(*sp)]);
break;
}
return 1; //A non-zero return value keeps the machine running
}
This part of your instruction decode select statement seems wrong
case CAL:
arlist[(*arcntr)++] = *sp + 1;
stack[*sp + 1] = base(ir.l, *bp, stack);
stack[*sp + 2] = *bp;
stack[*sp + 3] = *pc - 1;
*bp = *sp + 1;
*pc = ir.m;
break;
Normally you'd want to return to the NEXT instruction on a return.
stack[*sp + 3] = *pc - 1;
The *pc-1 part will probably will bring you back to your call instruction on return
I'd expect you'd want to push the address of the next instruction instead.
You'll probably want to update the stack pointer by 3 after pushing all that too, as well as checking your BP logic too

Switch/String in c

I am trying to get this function to return a string.
char * vergleich(int random) {
char *erg;
switch(random) {
case 1: erg = "Siccor" ; break;
case 2: erg = "Paper" ; break;
case 3: erg = "Rock" ; break;
case 4: erg = "Lizard" ; break;
case 5: erg = "Spock" ; break;
default: break;
return erg;
}
}
int main() {
srand(time(NULL));
int p1 = rand() % 4;
int p2 = rand() % 4;
printf("player1 shows %s\n", vergleich(p1));
printf("\n\tif Player 2 plays %s or %s Player1 wins\n", vergleich(p1+1), vergleich(p1+3));
if(p2 == p1 + 1 || p2 == p1 +3) {
printf("player1 wins");
}else {printf("player2 wins");}
return 0;
}
}
I think the initialisation of the function is wrong, but I dont really know how to deal with strings, please help.
If I run the program it just crashes if case > 2. and the strings are not displayed right.
The crash happens since the function vergleich() can be called with argument 0 (when p1 is 0) and 6 when p1 is 3. Zero and six are not handled in the switch, so pointer erg points to junk.
So, I suggest to enumerate your items starting from 0 to 4. To avoid overflow during additions you should also use modulo operation %, for example (p1 + 3) % 5, (p1 + 1) % 5.
To generate random from 0 to 4 you can use rand() % 5;.

Resources