Basically I need to pass the ret variable from the functions checkPOS, checkPOS1, checkPOS2 to the result() function, if the sum of the 3 ret variables equals to zero, user's access is granted, in case it is 1+, the access is denied. How can I pass the 3 variables to the result function?
void checkPOS2(struct Node* n, int pos, int alg, int input) {
int ret3;
for (int i = 0; i < pos - 1; i++) {
n = n->next;
}
if (input == n->data[alg]) {
printf("Numero certo (");
printf("%d", n->data[0]);
printf("%d", n->data[1]);
printf("%d)", n->data[2]);
ret3 = 0;
return ret3;
}
else {
printf("Numero errado (");
printf("%d", n->data[0]);
printf("%d", n->data[1]);
printf("%d)", n->data[2]);
ret3 = 1;
return ret3;
}
}
void result() {
if ((ret3) == 0) { printf("Acesso Permitido!"); }
else { printf("Acesso Negado!"); }
printf("%d", ret3);
}
It needs to take parameter as well.
void result(int r)
{
if ((r) == 0) { printf("Acesso Permitido!"); }
else { printf("Acesso Negado!"); }
printf("%d", r);
}
and before you call it:
int res = checkPOSx(....);
result(res);
or
result(checkPOSx(....));
Related
can you help me with the size of the digits, for example, when I enter 01234, then everything works as it should, but it shouldn’t, the limit of digits should be within four.When I enter some four-digit number, everything works as it should work. But when some five-digit, six-digit or even more, then everything works as if it should be, but it should not work like that. And when I enter numbers that are less than four-digit, for example 123 , then it gives an error and it's good. But when I enter numbers that are more than four digits, it does not give an error and works as if it should be so.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
typedef struct s_mastermind {
int my_attempt;
char* my_code;
} my_mastermind;
my_mastermind* settings_function(my_mastermind* mastermind, int argc, char** argv);
int checking_for_correctness_num(char* _string);
int wrong_input(int progress,char* num_code);
my_mastermind* my_function();
int check_function(char* string);
char* input_function();
int mis_placed_pieces(char* bit, char* num_code);
int well_placed_pieces(char* bit, char* num_code);
int code_checker(char* bit, char* num_code);
char* size_of_function(char* strye);
char* my_strcpy(char* num1, char* num2) {
for(int i = 0; num2[i] != 0; i++) {
num1[i] = num2[i];
}
return num1;
}
int my_strlen(char* num1) {
return (*num1) ? my_strlen(++num1) + 1 : 0;
}
my_mastermind* my_function() {
my_mastermind* num = malloc(sizeof(my_mastermind));
num->my_code = malloc(5);
num->my_code[4] = '\0';
my_strcpy(num->my_code, "");
num->my_attempt = 10;
return num;
}
my_mastermind* settings_function(my_mastermind* mastermind, int argc, char** argv) {
char* bit;
for(int i = 0; i < argc;) {
if (my_strlen(argv[i]) == 2 && argv[i][0] == '-') {
if(argv[i][1] == 'c') {
char* num_code = argv[i + 1];
if(wrong_input(argc,num_code) != 0) {
break;
}
my_strcpy(mastermind->my_code, num_code);
}else if(argv[i][1] == 't') {
if(checking_for_correctness_num(argv[i + 1]) == 0) {
mastermind->my_attempt = check_function(argv[i + 1]);
}
} else {
printf("WRONG FLAG RESTART THE GAME!!!\n");
}
}
i += 1;
}
return mastermind;
}
int wrong_input(int progress,char* num_code) {
// if(my_strlen(num_code) != 4) {
// printf("Code bigger than 4\n");
// }
if(checking_for_correctness_num(num_code) == 1) {
printf("Wrong input!\n> ");
fflush(stdout);
char* code = input_function();
char* variable = size_of_function(code);
free(code);
int results = 1;
if(wrong_input(progress,variable) == 0) {
results = code_checker(num_code, variable);
}
free(variable);
return results;
}
return 0;
}
int checking_for_correctness_num(char* _string) {
for(int i = 0; _string[i] != '\0'; i++) {
if(!(_string[i] >= '0' && _string[i] <= '9')) {
return 1;
}
}
return 0;
}
int check_function(char* string) {
int check_num = 0;
for(int i = 0; string[i] != '\0'; i++) {
check_num = check_num * 10 + (string[i] - '0');
}
return check_num;
}
char* input_function() {
char* getting = malloc(101);
getting[100] = '\0';
read(0, getting, 100);
fflush(stdout);
return getting;
}
int game_progress(int progress, char* bit) {
printf("Round: %d\n> ", progress);
fflush(stdout);
char* code = input_function();
char* variable = size_of_function(code);
free(code);
int results = 1;
if(wrong_input(progress,variable) == 0) {
results = code_checker(bit, variable);
}
free(variable);
return results;
}
void game_action(my_mastermind* mastermind) {
int current_try = 0;
for (;current_try < mastermind->my_attempt;) {
int results = game_progress(current_try, mastermind->my_code);
current_try += 1;
if(results == 0) {
printf("Congratz! You did it!\n");
break;
}
}
}
int code_checker(char* bit, char* num_code) {
int good_w = well_placed_pieces(bit, num_code);
int not_good_m = mis_placed_pieces(bit, num_code);
if(good_w == 3 || good_w == 2 || good_w == 1 || not_good_m == 3 || not_good_m == 2 || not_good_m == 1){
printf("Well placed pieces: %d\nMisplaced pieces: %d\n---\n", good_w,not_good_m);
}
if(good_w == 4) {
return 0;
} else {
return 1;
}
}
int well_placed_pieces(char* bit, char* num_code) {
int number = 0;
for(int i = 0; i < 4; i++) {
if (bit[i] == num_code[i]) {
number += 1;
}
}
return number;
}
int mis_placed_pieces(char* bit, char* num_code) {
int number = 0;
int i = 0;
int j = 0;
while(i < 4) {
i++;
if (bit[i] == num_code[i]) {
number += 1;
}
}
return number;
}
char* size_of_function(char* strye) {
char* new_string = malloc(5);
new_string[4] = '\0';
for(int i = 0; i < 4;i++){
new_string[i] = strye[i];
}
return new_string;
}
int main(int argc, char** argv) {
printf("Will you find the secret code?\n---\n");
my_mastermind* mastermind = my_function();
settings_function(mastermind, argc, argv);
game_action(mastermind);
free(mastermind);
return 0;
}
The problem is that you size_of_function assumes the input string is exactly 4 character long, not counting the '\0'. You should either check if the input string and return a error via a NULL pointer, or fully copy the string and check later.
Returning a NULL pointer require the least modification. You can do it by checking the input string size first :
char* size_of_function(char* strye) {
if(my_strlen(strye) != 4)
return NULL;
char* new_string = malloc(5);
new_string[4] = '\0';
for(int i = 0; i < 4;i++){
new_string[i] = strye[i];
}
if (strye[4] == '\r' || strye[4] == '\n' || strye[4] == '\0')
return new_string;
free(new_string);
return NULL;
}
Then, in wrong_input(), check if num_code is NULL :
int wrong_input(int progress,char* num_code) {
if(num_code == NULL || checking_for_correctness_num(num_code) == 1) {
printf("Wrong input!\n> ");
fflush(stdout);
char* code = input_function();
char* variable = size_of_function(code);
free(code);
int results = 1;
if(wrong_input(progress,variable) == 0) {
results = code_checker(num_code, variable);
}
free(variable);
return results;
}
return 0;
}
It is critical to check if num_code is NULL before calling checking_for_correctness_num(). In C the || operator evaluates the left operand first and skip the second operand evaluation if the first one is true. This way we can ensure that we never pass a NULL pointer to checking_for_correctness_num().
wrong_input() is called recursively and allocates memory without freeing it before calling itself. This can eat up memory fast and is generality considered to be bad practice.
Also, you've implemented my_strlen() as a recursive function, which isn't necessary. Using a loop is better :
int my_strlen(char* num1) {
int index = 0;
while(num1[index++]); //Note that 'index' is post-incremented
return index - 1; //Subtract one to account for the last post increment
}
I'm trying to write a recursive function (printPath) inside another function (dijkstraSSSP), but it gives me a compiler error.
When I run gcc dijkstra.c WGraph.c PQueue.c, it compiles just fine. However, when I run dcc -Wall -Werror -std=c11 -o dijkstra dijkstra.c WGraph.c PQueue.c. I got this error:
dijkstra.c:48:38: error: function definition is not allowed here
void printPath (int currentNode) {
Which I think it is because I define a function inside another function. If this is the case, how should I modify the code?
Thanks a lot!
void dijkstraSSSP(Graph g, Vertex source) {
int dist[MAX_NODES];
int pred[MAX_NODES];
bool vSet[MAX_NODES]; // vSet[v] = true <=> v has not been processed
int s, t;
PQueueInit();
int nV = numOfVertices(g);
for (s = 0; s < nV; s++) {
joinPQueue(s);
dist[s] = VERY_HIGH_VALUE;
pred[s] = -1;
vSet[s] = true;
}
dist[source] = 0;
int relaxWeight;
for (s = 0; s < numOfVertices(g); s++) {
s = leavePQueue(dist);
//printf("iterating with s = %d\n", s);
//printf("updating vSet[%d] = false\n", s);
vSet[s] = false;
for (t = 0; t < numOfVertices(g); t++) {
//printf("iterating with t = %d\n", t);
//printf("checking if s = %d is adj to t = %d and vSet[%d] = true\n", s, t, t);
if (adjacent(g, s, t) != 0 && vSet[t] == true) {
//printf("YES\n");
relaxWeight = adjacent(g, s, t);
if (dist[t] > dist[s] + relaxWeight) {
//printf("updating dist[%d] = dist[%d] + adjWeight[%d]\n", t, s, relaxWeight);
dist[t] = dist[s] + adjacent(g, s, t);
//printf("updating pred[%d] = %d\n", t, s);
pred[t] = s;
}
}
}
}
void printPath (int currentNode) {
if (pred[currentNode] == -1) {
printf("%d", currentNode);
return;
} else {
printPath (pred[currentNode]);
printf("-%d", currentNode);
}
}
for (int i = 0; i < numOfVertices(g); i++) {
if (dist[i] == VERY_HIGH_VALUE) {
printf("%d: no path\n", i);
} else{
printf("%d: distance = %d, shortest path: ", i, dist[i]);
printPath(i);
printf("\n");
}
}
}
Defining a function inside another function is not defined in the C Standard. gcc allows it as an extension, but most compilers don't.
You should move the definition of printPath outside the body of the dijkstraSSSP function, before it in the source code, and pass pred as an extra argument:
void printPath(const int *pred, int currentNode) {
if (pred[currentNode] == -1) {
printf("%d", currentNode);
} else {
printPath(pred, pred[currentNode]);
printf("-%d", currentNode);
}
}
void dijkstraSSSP(Graph g, Vertex source) {
int dist[MAX_NODES];
int pred[MAX_NODES];
bool vSet[MAX_NODES]; // vSet[v] = true <=> v has not been processed
int s, t;
PQueueInit();
int nV = numOfVertices(g);
for (s = 0; s < nV; s++) {
joinPQueue(s);
dist[s] = VERY_HIGH_VALUE;
pred[s] = -1;
vSet[s] = true;
}
dist[source] = 0;
int relaxWeight;
for (s = 0; s < numOfVertices(g); s++) {
s = leavePQueue(dist);
//printf("iterating with s = %d\n", s);
//printf("updating vSet[%d] = false\n", s);
vSet[s] = false;
for (t = 0; t < numOfVertices(g); t++) {
//printf("iterating with t = %d\n", t);
//printf("checking if s = %d is adj to t = %d and vSet[%d] = true\n", s, t, t);
if (adjacent(g, s, t) != 0 && vSet[t] == true) {
//printf("YES\n");
relaxWeight = adjacent(g, s, t);
if (dist[t] > dist[s] + relaxWeight) {
//printf("updating dist[%d] = dist[%d] + adjWeight[%d]\n", t, s, relaxWeight);
dist[t] = dist[s] + adjacent(g, s, t);
//printf("updating pred[%d] = %d\n", t, s);
pred[t] = s;
}
}
}
}
for (int i = 0; i < numOfVertices(g); i++) {
if (dist[i] == VERY_HIGH_VALUE) {
printf("%d: no path\n", i);
} else{
printf("%d: distance = %d, shortest path: ", i, dist[i]);
printPath(pred, i);
printf("\n");
}
}
}
Move the definition of printPath outside of the body of dijkstraSSSP and, to give it access to pred, change it to accept two parameters instead of one. The additional parameter should have type int * and should point to the first element of pred.
In the call, pass pred for that new parameter. The array pred will be automatically converted to a pointer to its first element.
I don't understand why printq() function prints 0, but when I access it back in main() it prints. I don't understand what I am doing wrong, I tried using pointers, but I get some different error in the priority queue. I want to print elements in array pq[10].
EDIT: I realized that the elements are stored but when I use pq[R].data it prints
but when I use pq[i].data in printq() and put it inside for loop, it prints zero.
#include <stdio.h>
int F = -1, R = -1;
int item, max = 10;
struct prioq {
int data;
int prio;
};
struct prioq pq[10] = { 0 };
void printq()
{
int i = 0;
printf("%d,", pq[i].data);
printf("QUEUE :");
for (i = 0; i < max; i++) {
printf("%d,", pq[i].data);
}
printf("\n");
printf("PRIO :");
for (i = 0; i < max; i++) {
printf("%d,", pq[i].prio);
}
}
void enqueue(int item, int p, struct prioq pq[])
{
if (F == -1 && R == -1 || F > R) {
F == 0;
R == 0;
pq[R].data = item;
pq[R].prio = p;
printf("%d", pq[R].data);
printf("%d", pq[R].prio);
printq();
} else if (R == max-1 || R > max) {
printf("overflow\n");
} else if (R < max) {
R++;
pq[R].data = item;
pq[R].prio = p;
printq();
}
}
void dequeue(struct prioq pq[])
{
int large = 0;
if (F == -1) {
printf("underflow\n");
} else {
int i;
for (i = 0; i < max; i++) {
if (pq[i].prio > large) {
large = i;
}
}
}
item = pq[large].data;
pq[large].prio = 0;
pq[large].data = 0;
printf("item deleted: %d\n", item);
printq();
}
void main()
{
int item = 0;
int c = 0, p = 0;
do {
printf("choose your option\n");
printf("1.Insert, 2.Delete, 3.Exit\n" );
scanf("%d", &c);
switch (c) {
case 1:
printf("Enter the priority and element to insert\n");
scanf("%d %d", &item, &p);
enqueue(item,p,pq);
printf("%d", pq[R].data);
printf("%d", pq[R].prio);
break;
case 2:
dequeue(pq);
break;
default:
c = 3;
break;
}
} while (c != 3);
printf("exited\n");
}
In your enqueue function, change the == in the F and R assignments to =.
void enqueue(int item, int p, struct prioq pq[])
{
if (F == -1 && R == -1 || F > R) {
F = 0; // Here
R = 0; // And here
pq[R].data = item;
pq[R].prio = p;
printf("%d", pq[R].data);
printf("%d", pq[R].prio);
printq();
} else if (R == max-1 || R > max) {
printf("overflow\n");
} else if (R < max) {
R++;
pq[R].data = item;
pq[R].prio = p;
printq();
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
How can I fix this error? It is caused by initializing my arrays pgAccess, pgArray, and pgRequest to variable sizes that are all global variables used throughout the program. I know there may be some undeclared variables in some of the methods but I would like to focus on the problem with these arrays. I'm working with virtual memory algorithms here.
int flag = 0, var, currFrame = 0, numFault, currRequest, request;
int numPages, numFrames, numRequest, fc =0, go =0, c =0;
int pgAccess[numRequest], pgArray[numPages], pgRequest[numRequest];
void runningMessage(int, int, int);
void fifo();
void lru();
void opt();
void runningMessage(int pg, int frame, int msg){
if(msg == 1)
{
printf("Page %d already in Frame %d\n",pg, frame);
}
if(msg == 2)
{
printf("Page %d loaded into Frame %d\n",pg, frame);
}
if(msg == 3)
{
printf("Page %d unloaded from Fram %d\n",pg, frame);
}
}
int main(int argc, char *argv[])
{
FILE *fp;
fp = fopen(argv[1], "r");
int x =0;
fscanf(fp, "%d %d %d", &numPages, &numFrames, &numRequest);
while(!feof(fp))
{
fscanf(fp, "%d", &pgAccess[x]);
x++;
}
fclose(fp);
printf("Algorithm: %s\n\n", argv[2]);
if(argv[2] == "FIFO")
{
fifo();
}
if(argv[2] == "LRU")
{
lru();
}
if(argv[3] == "OPT")
{
opt();
}
}
void fifo(){
int i, x;
for(i = 0; i < numFrames; i++)
{
pgArray[i] = -1;
}
while(x < numRequest)
{
flag =0;
var = pgAccess[x];
for(i = 0; i < numFrames; i++)
{
if(var == pgArray[i])
{
flag = 1;
x++;
runningMessage(pgArray[i], i, 1);
break;
}
}
if(flag == 0)
{
if(currFrame < numFrames)
{
pgArray[currFrame] = pgAccess[x];
runningMessage(pgAccess[x], currFrame, 2);
currFrame++;
x++;
numFault++;
}
else if( y < numFrames)
{
runningMessage(pgArray[y], y, 3);
pgArray[y] = pgAccess[x];
runningMessage(pgAccess[x], y, 2);
x++;
y++;
numFault++;
else
{
y =0;
}
}
}
}
printf(" %d Page Faults\n\n", numFault);
}
void lru(){
int i, x, y, a, b, j;
y = numFrames;
for(i = 0; i < y; i++)
{
pgRequest[i] = 0;
pgArray[i] = -1;
}
for(i = 0; i < numRequest; i++)
{
flag = 0;
a = pgAccess[i];
for(j = 0; j < y; j++)
{
if(a == pgArray[j])
{
flag = 1;
pgRequest[j] = i;
runningMessage(pgArray[j], j, 1);
break;
}
}
if((flag == 0) && (b < y))
{
fc++;
pgArray[b] = a;
pgRequest[b] = i;
runningMessage(pgArray[b], b, 2);
b++;
}
else if((flag == 0) && (b == y))
{
fc++;
minRequest = pgRequest[0];
for(currRequest = 0; currRequest < y; currRequest++)
{
if(pgRequest[currRequest] < minRequest)
{
minRequest = pgRequest[currRequest];
request = currRequest;
}
}
pgArray[request] = a;
pgRequest[request] = i;
runningMessage(pgAccess[request], request, 3);
runningMessage(pgArray[request], request, 2);
request =0;
}
}
printf(" %d Page Faults\n\n", fc);
}
void opt(){
int i, x, y, a;
for(i = 0; i < numFrames; i++)
{
pgRequest[i] = 0;
pgArray[i] = -1;
}
for(i = 0; i < numRequest; i++)
{
flag = 0;
a = pgAccess[i];
for(y = 0; y < numFrames, y++)
{
if(a == pgArray[y])
{
flag = 1;
runningMessage(pgArray[y], y, 1);
break;
}
}
if((flag == 0) && (x < numFrames))
{
fc++;
pgArray[x] = a;
runningMessage(pgArray[x], x, 2);
x++;
}
else if((flag == 0) && (x == numFrames))
{
fc++;
for(go = 0; go < numFrames; go++)
{
pgRequest[go] = 0;
}
for(currRequest = 0; currRequest < numFrames; currRequest++)
{
c = 0;
for(currFrame = i + 1; currFrame < numRequest; currFrame++);
{
if(pgArray[currRequest] == pgAccess[currFrame])
{
if(pgRequest[currRequest] == 0)
{
pgRequest[currRequest] = currFrame;
c = 1;
}
}
}
if(c != 1)
{
pgRequest[currRequest] = numRequest + 1;
}
}
request = 0;
maxRequest = pgRequest[0];
for(go = 0; go < numFrames; go++)
{
if(pgRequest[go] > maxRequest)
{
maxRequest = pgRequest[go];
runningMessage(pgArray[go], go, 3);
request = go;
}
}
pgArray[request] = a;
runningMessage(pgArray[request], request, 2);
}
}
printf(" %d Page Faults\n\n", fc);
}
Here, you have not initialized the variable numRequest and numPages, and you are passing it inside the initialization of an array. This might be causing the error in your code. Because numRequest and numPages might have some random garbage values as they are not initialized.
So either you declare the array after setting those numRequest and numPages or take the pointer instead of array as mentioned in this answer.
You still have to use malloc(), just make sure that you used it the correct way.
Declare your arrays as pointers,
int *pgAccess, *pgArray, *pgRequest;
when allocating,
pgAccess=malloc(sizeof(int)*numRequest);
Notice that this is not the only issue in your program.
I want to print all possible paths from a given source and destination. But in my BFS code, it only shows the two paths, not the multiple path. For a directed graph where n = 4, edge = 6, given,
1-2
1-3
1-5
5-3
5-4
3-4
3-2
It should've printed 3 paths:
1-5-4
1-3-4
1-5-3-4
But it only shows this two paths
1-3-4
1-5-4
This is my sample code for finding the src to destination path
#include <stdio.h>
int queue1[100], state[100], parent[100];
int front = 0, rear = -1, maxSize = 100;
int count = 0;
int initial = 1, waiting = 2, visited = 3;
int n, e;
int adj[100][100];
bool isEmpty()
{
return count == 0;
}
bool isFull()
{
return count == maxSize;
}
void enqueue(int val)
{
if (!isFull())
{
if (rear == maxSize - 1)
{
rear = -1;
}
rear++;
queue1[rear] = val;
count++;
}
}
int dequeue()
{
int val = queue1[front];
front++;
if (front == maxSize)
{
front = 0;
}
count--;
return val;
}
void BFS_Traversal(int src, int des)
{
int done = 0;
enqueue(src);
state[src] = waiting;
parent[src] = -1;
printf("path ");
while (!isEmpty() && done == 0)
{
src = dequeue();
// printf("%d ",src);
state[src] = visited;
for (int i = 1; i <= n; i++)
{
if (adj[src][i] == 1 && state[i] == initial)
{
enqueue(i);
state[i] = waiting;
parent[i] = src;
if (i == des)
{
state[i] = initial;
int k = des;
do
{
printf("%d ", k);
k = parent[k];
} while (k != -1);
printf("\n");
}
}
}
}
}
int main()
{
int src, start, end, des;
scanf("%d%d", &n, &e);
for (int i = 1; i <= e; i++)
{
scanf("%d%d", &start, &end);
adj[start][end] = 1;
}
for (int i = 1; i <= n; i++)
{
state[i] = initial;
}
for (int k = 1; k <= n; k++)
{
parent[k] = -1;
}
scanf("%d%d", &src, &des);
BFS_Traversal(src, des);
}
As, you can see 1-5-3-4 path is not showing because they are already visited. How should I modify this code to print all possible paths?