Defining a recursive function inside another function in C - c

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.

Related

Passing variable from mulitple functions to another function in C

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(....));

Array of structures lose data while passing it in C

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();
}
}

Variably modified at file scope [closed]

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.

My BFS code is only showing the direct paths from source to destination, but not all possible paths

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?

How to remove item from array in C?

I want to remove element from b->array as shown in below code:removeItem function
I have tried to remove 12.12 from the b->array = {11.11,12.12,13.13}but it leads to segmentation fault instead of printing {11.11,13.13}. can anybody help to get rid of it?
typedef struct
{
float val;
}data;
typedef struct
{
data **array;
int size;
}bag;
int main(int argc,char* argv[])
{
bag *str = createBag();
#ifdef DEBUG
printf("Inital values: %p %d\n",str->array,str->size);
#endif
data *iptr = createData(10.10);
data *iptr1 = createData(11.11);
data *iptr2 = createData(12.12);
data *iptr3 = createData(13.13);
data *iptr4 = createData(14.14);
#ifdef DEBUG
printf("%f\n",iptr->val);
#endif
addData(str,iptr);
addData(str,iptr1);
addData(str,iptr2);
addData(str,iptr3);
addData(str,iptr4);
printBag(str);
data *ptr = getData(str,4);
printf("Data item 4 is:%f\n",ptr->val);
int b = getBagSize(str);
printf("Size of bag: %d\n",b);
removeBack(str);
printBag(str);
removeFront(str);
printBag(str);
//cleanBag(str);
int s = searchBag(str,10.10);
printf("%d\n",s);
removeItem(str,12.12);
printBag(str);
return 0;
}
bag* createBag()
{
bag *str = (bag*)malloc(sizeof(bag));
str->array = NULL;
str->size = 0;
return str;
}
data* createData(float v)
{
data *iptr = (data*)malloc(sizeof(data));
iptr->val = v;
return iptr;
}
void addData(bag* b, data* d)
{
b->size++;
data** array1 = (data**)malloc(sizeof(data*)* b->size);
if(b->array!= NULL)
{
int i;
for(i = 0; i<b->size;i++)
{
array1[i] = b->array[i];
}
}
free(b->array);
array1[b->size-1] = d;
b->array = array1;
}
void printBag(bag *b)
{
int i;
for(i=0; i<b->size;i++)
{
printf("%f\n",b->array[i]->val);
}
}
data* getData(bag *b, int pos)
{
if(pos>5)
{
printf("change array position\n");
}
return b->array[pos];
}
int getBagSize(bag *b)
{
return b->size;
}
void removeBack(bag *b)
{
b->size--;
free(b->array[b->size]);
data **array2 = (data**)malloc(sizeof(data*)* b->size);
if(b->array!= NULL)
{
int i;
for(i = 0; i<b->size;i++)
{
array2[i] = b->array[i];
#ifdef DEBUG
printf("%f\n",array2[i]->val);
#endif
}
free(b->array);
b->array = array2;
}
}
void removeFront(bag *b)
{
b->size--;
free(b->array[0]);
data **array2 = (data**)malloc(sizeof(data*)* b->size);
if(b->array!= NULL)
{
int i;
for(i = 0; i<b->size;i++)
{
array2[i] = b->array[i+1];
#ifdef DEBUG
printf("%f\n",array2[i]->val);
#endif
}
free(b->array);
b->array = array2;
}
}
/*void cleanBag(bag *b)
{
int i;
for(i=0;i<b->size;i++)
{
free(b->array[i]);
}
free(b->array);
free(b);
}*/
int searchBag(bag *b,float v)
{
int i;
for(i=0;i<b->size;i++)
{
if(b->array[i]->val==v)
{
return (i+1);
}
}
return -1;
}
void removeItem(bag *b, float v)
{
int flag = 0,i = 0;
flag = searchBag(b,v);
if(flag != -1)
{
data **array2 = (data**)malloc(sizeof(data*)*(b->size-1));
for(i = 0; i < (b->size); i++)
{
if(i == (flag-1))
{
i = i + 1;
continue;
}
array2[i] = b->array[i];
}
free(b->array);
b->size--;
b->array = array2;
}
else
{
printf("Element is not found\n");
}
}
There are two mistakes in your code.
searchBag returns 0, if the float value is not found in the bag.
However, the function removeItem test for if(flag != -1). It should test for if(flag != 0) instead.
The reason for the seg. fault you encountered also lies in the removeItem function.
Note how the memory allocated for array2 is for b->size-1 elements only. Valid array indices for this array are in the range of 0 ... b->size-2.
Now, look at the for loop, and you will note that the index variable i will run from 0 to b->size-1. Exactly in the last iteration i will become b->size-1, so the code tries to write at the location array2[b->size-1] which is beyond the allocated memory of array2.
A possible fix of the removeItem function could look like this:
void removeItem(bag *b, float v)
{
int flag = 0;
int oldArrayIndex = 0;
int newArrayIndex = 0;
flag = searchBag(b,v);
if(flag != 0)
{
data **array2 = (data**) malloc(sizeof(data*)*(b->size-1));
for(oldArrayIndex = 0; oldArrayIndex < (b->size); oldArrayIndex++)
{
if(oldArrayIndex != (flag-1))
{
array2[newArrayIndex] = b->array[oldArrayIndex];
newArrayIndex++;
}
}
free(b->array);
b->size--;
b->array = array2;
}
else
{
printf("Element is not found\n");
}
}

Resources