I am trying to figure out why my C code is outputting "exited, segmentation fault". The game is supposed to generate 4 random numbers and store them in the "secret" array. Then the user is suppose to guess the secret code. The program also calculates the score of the user's guess. For now, I printed out the random secret code that has been generated, but when the game continues, it will output "exited, segmentation fault". Also, the GetSecretCode function has to be void and doesnt return anything. Help me out please.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int *secret[5];
void GetSecretCode(int size, int my_array[])
{
srand(time(NULL));
for (size = 0; size < 4; size++)
{
my_array[size] = (rand() % 10) +0;
secret[4] = &my_array[size];
printf(" \nPasscode#%d generated:%d \n", size +1 , *secret[4] );
}
}
int getExactMatches(int *secret[], int guess[], int size)
{
int count = 0;
for (int i = 0; i < size; i++)
{
if (*secret[i] == guess[i])
count++;
}
return (count);
}
int getMatches(int *secret[], int guess[], int size)
{
int count = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (*secret[i] == guess[j])
count++;
}
}
return (count);
}
int game(int guess1[4], int* secret[4], int size)
{
size = 4;
int guessLimit = 6;
int guessCount = 0;
int outofGuess = 0;
int i = 0;
int score;
while (guess1[4] != *secret[4] && outofGuess == 0)
{
if (guessCount < guessLimit)
{
printf(" \n\n You have %d guesses...\n", guessLimit - i);
printf(" Guess the four digit secret code stored in the box:\n");
for (i = 0; i < size; i++)
{
printf(" \n %dst number : ", i + 1);
scanf("%d", guess1 + i);
}
guessCount++;
i = guessCount;
int getMatch1 = getMatches(secret, guess1, size) - getExactMatches(secret, guess1, size);
int getMatch2 = getExactMatches(secret, guess1, size);
printf("\n 〈Your score is: %d.%d 〉\n", getMatch2, getMatch1);
if (getMatch2 == 4 && getMatch1 == 0)
{
printf("\nCongratulations!, you guessed the code.\n");
return 0;
}
}
else
{
outofGuess = 1;
}
}
if (outofGuess == 1)
{
printf("\n 〈Sorry, you lost.〉\n");
return 0;
}
return 0;
}
int main()
{
char ch;
int size = 4;
int my_array1[8];
int guess1[5];
GetSecretCode(size, my_array1);
while (game(guess1,secret, size) == 0)
{
printf("\nDo you wish to continue? (y/n) ");
scanf(" %[^\n]%*c", & ch);
if (ch == 'y') {
game(guess1, secret, size);
}
if (ch == 'n')
{
printf("\n\nExiting... ");
exit(0);
} else if ((ch != 'y') || (ch != 'n'))
{
printf("Wrong letter entered, please try again.\n");
return 0;
}
return 0;
}
}
//i have corrected the code now it will work however i didnt change the logic of code as the exact scoring mechanism is provided.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int secret[4];
void GetSecretCode()
{
srand(time(NULL));
for(int size = 0; size < 4; size++)
{
secret[size] = (rand() % 10) +0;
printf(" \nPasscode#%d generated:%d \n", size +1 ,
secret[size]);
}
}
int getExactMatches(int secret[], int guess[], int size)
{
int count = 0;
for (int i = 0; i < size; i++)
{
if (secret[i] == guess[i])
count++;
}
return (count);
}
int getMatches(int secret[], int guess[], int size)
{
int count = 0;
int visited[4]={0};
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (secret[i] == guess[j]&&visited[j]!=1)
{
visited[j]=1;
count++;
}
}
}
return (count);
}
int game(int guess1[4], int secret[4], int size)
{
size = 4;
int guessLimit = 6;
int guessCount = 0;
int outofGuess = 0;
int i = 0;
int score;
while (outofGuess == 0)
{
if (guessCount < guessLimit)
{
printf(" \n\n You have %d guesses...\n", guessLimit - i);
printf(" Guess the four digit secret code stored in the box:\n");
for (i = 0; i < size; i++)
{
printf(" \n %dst number : ", i + 1);
scanf("%d", guess1+i);
}
guessCount++;
i = guessCount;
int getMatch1 = getMatches(secret, guess1, size) - getExactMatches(secret, guess1, size);
int getMatch2 = getExactMatches(secret, guess1, size);
printf("\n 〈Your score is: %d.%d 〉\n", getMatch2, getMatch1);
if (getMatch2 == 4 && getMatch1 == 0)
{
printf("\nCongratulations!, you guessed the code.\n");
outofGuess=1;
return 0;
}
else
{
outofGuess = 0;
}
}
}
if (outofGuess == 1)
{
printf("\n 〈Sorry, you lost.〉\n");
return 0;
}
return 0;
}
int main()
{
char ch;
int size = 4;
int my_array1[8];
int guess1[5];
GetSecretCode(size, my_array1);
while (game(guess1,secret, size) == 0)
{
printf("\nDo you wish to continue? (y/n) ");
scanf(" %[^\n]%*c", & ch);
if (ch == 'y') {
game(guess1, secret, size);
}
if (ch == 'n')
{
printf("\n\nExiting... ");
exit(0);
} else if ((ch != 'y') || (ch != 'n'))
{
printf("Wrong letter entered, please try again.\n");
return 0;
}
return 0;
}
}

I am trying to figure out why my C code is outputting "exited, segmentation fault". The...
I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) { int i, j; int rowA, colA, rowB, colB; int A[10][10], B[10][10]; int sum[10][10]; char str1[10]; printf("This is a matrix calculator\n"); //read in size from user MATRIX A printf("Enter in matrix A....\n"); printf("\t#row = "); scanf("%d", &rowA); printf("\t#col = "); ...
Can someone help me fix my C code. I am getting segmentation fault along with missing termination " /* These are the included libraries. */ #include #include #include #include void printTriangle(char *str); int main() { char sentence[81]; int done = 0; while(done != 1) { printf("Please enter the sentence or quit: "); fgets(sentence, 80, stdin); if(strcmp(sentence, "quit") == 0) done = 1; else printTriangle(sentence); } } void printTriangle(char *str) { int index = 0; int line = 1; int i;...
Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...
The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...
I am getting the Segmentation fault error on the Ubuntu machine
but not on macOS.
Any help would be appreciated.
/**** main.c ****/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#define WORD_LEN 6
#define TOP 10
char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r";
struct Word {
char word[30];
int freq;
};
int threadCount;
int fileDescriptor;
int fileSize;
off_t chunk;
struct Word* wordArray;
int arrIndex = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...
I am trying to write C programming code and output will be as
below
but I donno how to get the result analysis part.
help me to add the 2nd part with my code:
here is my code below:
#include <stdio.h>
#define MAX 100
struct studentMarkVariable{
int id;
float marks;
};
void getData(struct studentMarkVariable arrs[]);
void show(struct studentMarkVariable arrs[]);
int main()
{
printf ("####### Marks Analyzer V3.0 ####### \n");
struct studentMarkVariable
arrs[MAX];
getData(arrs);
show(arrs);
return 0;
}...
Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct { char name[MAX_SIZE1]; int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...
Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions: 1. First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....
Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...
C Programming Question
Hi, I have the following code and in my submission I'm supposed
to only include function definitions (i.e. implementations) in the
file. However, I'm not NOT required to write the main,
struct definitions and function prototypes.
Could someone help me fix this code?
Text Version:
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct ip_address {
int octet_1;
int octet_2;
int octet_3;
int octet_4;
};
typedef struct ip_address ip_address_t;
void print_ip_address(ip_address_t ip1){
printf("%d.%d.%d.%d",
ip1.octet_1,ip1.octet_2,ip1.octet_3,ip1.octet_4);
}
int is_valid(ip_address_t ip1){
if(ip1.octet_1 < 0...