Jump to content

Generating numbers

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
addicteddrug

addicteddrug

    Newbie

  • Members
  • Pip
  • 4 posts
Hi everyone

Here is my problem:

I'm looking for a function that takes an integer and output different orders of numbers
up to that integer. The example make it more clear:

input : 2
output:
1,2
2,1


input: 3
output:
1,2,3
1,3,2
3,1,2
3,2,1
2,3,1
2,1,3


Thank you very much for your help!!!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Are you looking for help on the algorithm, the input, something else? Without seeing what code you've already written, it's hard to offer much help.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
addicteddrug

addicteddrug

    Newbie

  • Members
  • Pip
  • 4 posts
yes I'm working on the Bankers algorithm, and I've done most of that , currently
I'm looking for a function that can generate different and all order of numbers. actually
the numbers are the order of the processes that may be some of them can cause deadlock.

I did those parts that can understand a particular order of proccesses may cause deadlock or not, the only problem is : "generating all orders of the proccesses." actually I want to find all orders of proccesses that maintane the system safe

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Again, without seeing the code you currently have, I can't help a whole lot. One hint that may help: you are creating all permutations of the numbers 1-n.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
addicteddrug

addicteddrug

    Newbie

  • Members
  • Pip
  • 4 posts
here is my code:

the problem is in the last FOR of the main function, it generates only 1 order
of processes ( 0,1,2,3,...,n) but i need a function that can test all possible
order of the 1 TO n.




 #include<stdio.h>

#include<conio.h>

#define M 10




int max[M][M];

int all[M][M];

int need[M][M];

int a[M];

int i,j;

int flagr=0;

int flagp=0;

int r=1;



void needfinder(void);

int suit(int);




main(){


while ( r != 0 )

{

   printf("please enter the available resources (-1 for exit): \n");

   scanf("%d",&a[flagr]);

   flagr++;


   if(a[flagr-1] == -1)

    r=0;


}

flagr--;


int j=0;

r=1;

int temp;


while ( r != 0 )

{


    printf("please enter the Max resources need for Proccess (%d) (-1 for exit): \n",flagp);

    for (j=0 ; j<flagr ; j++ )

    {

        printf("\n resource %d:",j);

        scanf("%d",&temp);

        if ( temp != -1 )

         max[flagp][j]=temp;

          else break;

    }

      flagp++;


      if(temp == -1)

       r=0;


}

r=1;

flagp--;


printf("\n \n \n \n \n \n");

for( i=0 ; i<flagp ; i++)

{

printf("\nplease enter the Allocated resources  for Proccess (%d) : \n",i);

 for (j=0 ; j<flagr ; j++ )

 {

  printf("\n resource %d:",j);

  scanf("%d",&all[i][j]);

 }

}

needfinder();


for ( i=0 ; i<flagp ; i++)

{

    temp=suit(i);

    if( temp == 1)

    {

      

         for( int j=0 ; j<flagr ; j++)

            a[j]+=all[i][j];

  

    }

}

getch();


}







void needfinder(void)

{

 for ( i=0 ; i<flagp ; i++)

 for ( j=0 ; j<flagr ; j++) 

 need[i][j]=  max[i][j]- all[i][j];

}       


int suit(int n)

{

   for( int i=0 ; i<flagr ; i++)

    if(need[n][i] > a[i] )  

      return -1;

    return 1;

}       



#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Have a look at this site. It's in JavaScript, but should help:
Permutation Generator
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
addicteddrug

addicteddrug

    Newbie

  • Members
  • Pip
  • 4 posts
thanks guys i found the solution, here is the code

#include <stdio.h>

#include <conio.h>


int t[11],n;


void permute(int k) {

int i,c;

if (1==k){

for (i=n; i>0; --i)

printf("%d", t[i]);

printf("\n");

} else {

permute(k-1);

for (i=1; i<k; ++i){

c=t[i]; t[i]=t[k]; t[k]=c;

permute(k-1);

c=t[i]; t[i]=t[k]; t[k]=c;}}}


 main(int x){

n=0;

x=1234;

while (x>0) {

t[++n]=x%10;

x/=10;}

permute(n);

getch();

}