shanon4 - 1 year ago 58
C Question

# could someone tell me what is wrong with this?

Question: wow, you are given two things:

The calling order in which all the processes are called.
The ideal order in which all the processes should have been executed.
Now, let us demonstrate this by an example. Let's say that there are 3 processes, the calling order of the processes is: 3 - 2 - 1. The ideal order is: 1 - 3 - 2, i.e., process number 3 will only be executed after process number 1 has been completed; process number 2 will only be executed after process number 3 has been executed.

1. Iteration #1:
Since the ideal order has process #1 to be executed firstly, the calling ordered is changed, i.e., the first element has to be pushed to the last place. Changing the position of the element takes 1 unit of time. The new calling order is: 2 - 1 - 3. Time taken in step #1: 1.

2. Iteration #2: Since the ideal order has process #1 to be executed firstly, the calling ordered has to be changed again, i.e., the first element has to be pushed to the last place. The new calling order is: 1 - 3 - 2. Time taken in step #2: 1.

3. Iteration #3: Since the first element of the calling order is same as the ideal order, that process will be executed. And it will be thus popped out. Time taken in step #3: 1.

4. Iteration #4: Since the new first element of the calling order is same as the ideal order, that process will be executed. Time taken in step #4: 1.

5. Iteration #5: Since the last element of the calling order is same as the ideal order, that process will be executed. Time taken in step #5: 1.

Total time taken: 5 units.
S: Executing a process takes 1 unit of time. Changing the position takes 1 unit of time.

Input format:
The first line a number N, denoting the number of processes. The second line contains the calling order of the processes. The third line contains the ideal order of the processes.

Output format:
Print the total time taken for the entire queue of processes to be executed.

Constraints: 1<=N<=100

SAMPLE INPUT

``````3
3 2 1
1 3 2
``````

SAMPLE OUTPUT

``````5
``````

The first method that came to me was to declare a variable and increase it by one after every swap plus 2, but then if we take an array of size 3, there are only two possibilities. Hence I printed those out, and yes guys this is a problem from an online judge, there was no syntax error, but in the site it tells that there was some problem in declaration, I have no clue what to do I am new to programming and can someone suggest a new method which is applicable for arrays of bigger sizes.

``````#include <stdio.h>

swap(a,b);
long long int a,b;

int main(void) {
long long int n,i;
long long int a[3],b[3];
scanf("%d",&n);

for(i=0;i<=3;i++)
{
scanf("%d",a[i]);
}

for(i=0;i<=2;i++)
{
scanf("%d",&b[i]);
}

if(a[i]=b[i])
{
printf("2");
}

while(a[i]!=b[i])
{
swap(b[0],b[2]);
swap(b[0],b[1]);
{
if(a[i]=b[i])
printf("5");
}
}

return 0;
}

swap(a,b){
long long int temp;
a=temp;
b=a;
temp=b;
return swap(a,b);
}
``````

Your `swap()` function has multiple issues:

1: It calls itself with `return swap(a,b);` in the last line of the function. This will result in infinite recursion and a stack overflow.

2: You need to declare the return type of the function. If it takes two `long long` parameters and returns nothing, then it should be declared as follows:

``````void swap(long long a, long long b);
``````

3: As written, the function will have no effect anyway. You need to pass pointers to these variables if you want to change their values:

``````long long swap(long long *a, long long *b);
``````

4: To pass pointers to this function, prefix the parameters with `&` as follows:

``````swap(&b[0],&b[2]);   /* NOT: swap(b[0],b[2]); */
``````

5: The order of operations in the `swap()` function is also wrong. You start by irretrievably losing the value of `a` instead of storing it so that it can be assigned to `b`. Try this instead:

``````void swap(long long *a, long long *b) {
long long temp;
temp = *a;
*a = *b;
*b = temp;
}
``````

NOTE: Please learn how to format your code properly. Also, always compile your development code with warnings enabled (e.g., add `-Wall -pedantic` to the command line). Doing so would have drawn your attention to most if not all of these problems without having to ask here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download