int sin2(int g)
{                     
                     static int metritis1=1,i=0;
              
                     int l,c,pin[100],pin2[100];
                     char result;                
                     
                     srand ( time(NULL) ); 
                     l = rand() % 10;                     
                     c = rand() % 10;
                     c=65+c;
                     l=l+1;
                     pin[g]=c;
                     pin2[g]=l;
                     
                     printf("%d and %d the g is %d\n",pin[g],pin2[g],g);
                    
                     while (1){
                         if (pin[i]==pin[g] && pin2[i]==pin2[g]){
                                printf("%c kai %d g is %d to i %d\n",pin[g],pin2[g],g,i);            
                                
                                l = rand() % 10;                     
                                c = rand() % 10;
                                c=65+c;
                                l=l+1;
                                pin[g]=c;
                                pin2[g]=l;
                                printf("%c and %d g is %d the i %d\n",pin[g],pin2[g],g,i);
                                i=-1;}
                         i++;                                   }

guys i have a problem...this function doesn't work for me...look i want this function to check in the 2 arrays pin,pin2 for previous numbers already stored in these arrays but it doesn't work...also when i find 2 numbers already stores(same pair of numbers) then i want even if it changes the numbers then to recheck over and over until the pair of numbers is new...any ideas???why doesn't it work?

guys i have a problem...this function doesn't work for me...look i want this function to check in the 2 arrays pin,pin2 for previous numbers already stored in these arrays but it doesn't work...

Not a clue. Since I can't I run the code nor can I see your screen, I don't have any idea what it's doing wrong. It's your job to tell us so we know what to look for.

also when i find 2 numbers already stores(same pair of numbers) then i want even if it changes the numbers then to recheck over and over until the pair of numbers is new...any ideas???

Put it in a loop. If the 2 numbers are unique, exit the loop.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_ROWS 10
#define MAX_COLS 10

void sin1(char map[][MAX_COLS]); 
int sin2(int g);
int sin3(char map[][MAX_COLS]);
int main(void)
{  
   system("chcp 1253 >nul ");
   FILE *fileptr;
  
   char myColchar,apantisi,map[MAX_ROWS][MAX_COLS];
   int metritis1=0,g=0,metritis2=0,apotelesma,myColnum=1,myrow=1,k,a,l,c,row, col;
   char r,fileRow[MAX_COLS + 2]; // account for newline and NULL
  
   printf("ÄÙÓÅ ÔÇ ÄÉÁÔÁÎÇ ÌÅ ÔÏÍ ÁÑÉÈÌÏ ÐÏÕ ÅÐÉÈÕÌÅÉÓ(1/2/3): \n");
   scanf("%d",&k);//edo tha iparxei sinartisi pou tha pairnei to k os parametro kai tha exei void epistrofi...
   while (k!=1 && k!=2 && k!=3){
         printf("ÅÊÁÍÅÓ ËÁÈÏÓ.ÄÙÓÅ ÎÁÍÁ ÔÇ ÄÉÁÔÁÎÇ ÌÅ ÔÏÍ ÁÑÉÈÌÏ ÐÏÕ ÅÐÉÈÕÌÅÉÓ(1/2/3): \n");
         scanf("%d",&k);}
   if (k==1){
        fileptr = fopen("1.txt", "r");}
   else if (k==2){
        fileptr=fopen("2.txt","r");}     
   else if (k==3){
        fileptr=fopen("3.txt","r");}
   if (fileptr == NULL)
      {
           perror("File Open Error");
           return -1;
      }
   else if (fileptr!=NULL){
   printf(" A B C D E F G H I J\n");   
   printf("--------------------- \n");   
   for (row = 0; row < MAX_ROWS; row++)
      {  
         fgets(fileRow, sizeof(fileRow), fileptr);
         
         printf("|");
         for (col = 0; col < MAX_COLS; col++){
              
              map[row][col] = fileRow[col];
              if (map[row][col]=='.'){
                                   map[row][col]=' ';}
              printf("%c",map[row][col]);
              printf("|");}
              printf("%d",row+1);
         
              printf("\n");
              printf("--------------------- \n");     
      }}
      
   printf("\nÓÅ ÐÅÑÉÐÔÙÓÇ ÐÏÕ ÅÊÔÅËÅÉÔÅ ÂÏËÇ ÐÁÔÇÓÔÅ <<N>> ÁËËÉÙÓ <<O>>: \n");
   printf("ÅÊÔÅËÅÓÇ ÂÏËÇÓ (N/O): \n");
   getchar();
   scanf("%c", &apantisi);
   while (apantisi!='N' && apantisi!='n' && apantisi!='O' && apantisi!='o'){
         printf("\nËÁÈÏÓ ÄÉÁÔÁÃÇ.ÓÅ ÐÅÑÉÐÔÙÓÇ ÐÏÕ ÅÊÔÅËÅÉÔÅ ÂÏËÇ ÐÁÔÇÓÔÅ <<N>> ÁËËÉÙÓ <<O>>: \n");
         printf("ÅÊÔÅËÅÓÇ ÂÏËÇÓ (N/O): \n");
         getchar();
         scanf("%c", &apantisi);}
   if (apantisi=='N' || apantisi=='n'){
                      
                     metritis1=sin2(g);
                     sin1(map);
                     g++;
                     
                     while (1){
                           metritis2=sin3(map);
                           sin1(map);
                           
                           if (metritis2==17){
                                             printf("Ç ÏÌÁÄÁ ÌÁÓ Å×ÁÓÅ ÔÇ ÌÁ×Ç ÁËËÁ Ï×É ÔÏÍ ÐÏËÅÌÏ\n");
                                             break;}
                           metritis1=sin2(g);
                           sin1(map);
                           g++;
                          
                           if (metritis1==17){
                                              printf("VICTORY IS OURS\n");
                                              break;}
                           }
                                                    
                           
                                                                 

                     

                    
                    

                     
                     }
   else if (apantisi=='O' || apantisi=='o'){
                  
                     metritis2=sin3(map);
                     sin1(map);
                     while (1){
                           metritis1=sin2(g);
                           sin1(map);
                           if (metritis1==17){
                                              printf("VICTORY IS OURS\n");
                                              break;}
                           metritis2=sin3(map);
                           sin1(map);
                           if (metritis2==17){
                                              printf("Ç ÏÌÁÄÁ ÌÁÓ Å×ÁÓÅ ÔÇÍ ÌÁ×Ç ÁËËÁ Ï×É ÔÏÍ ÐÏËÅÌÏ\n");
                                              break;}
                           }
               
                                                                         
                    
                          }
   
   
         
   fclose(fileptr);
   
   system("pause");
// now you have the map loaded,

      // so do the rest of your program
   return 0;
}
//********************************************************************************************************************************************************************
void sin1(char map[][MAX_COLS])
{    
     int row,col;
     printf(" A B C D E F G H I J\n");   
     printf("--------------------- \n");  
     for (row = 0; row < MAX_ROWS; row++)
      {  
          printf("|");
          for (col = 0; col < MAX_COLS; col++){
              printf("%c",map[row][col]);
              printf("|");}
          printf("%d",row+1);
          printf("\n");
              printf("--------------------- \n");     
      }
}

// ******************************************************************************************************************************************************************
int sin2(int g)
{                     
                     static int metritis1=1,i;
              
                     int l,c,pin[100],pin2[100];
                     char apotelesma;                
                     
                     srand ( time(NULL) ); 
                     l = rand() % 10;                     
                     c = rand() % 10;
                     c=65+c;
                     l=l+1;
                     pin[g]=c;
                     pin2[g]=l;
                     printf("%d kai %d to g einai %d\n",pin[g],pin2[g],g);
                     i=0;
                     for (i=0;i<g;i++){
                         if (pin[i]==pin[g] && pin2[i]==pin2[g]){
                                printf("%c kai %d g einai %d to i %d\n",pin[g],pin2[g],g,i);            
                                
                                l = rand() % 10;                     
                                c = rand() % 10;
                                c=65+c;
                                l=l+1;
                                pin[g]=c;
                                pin2[g]=l;
                                printf("%c kai %d g einai %d to i %d\n",pin[g],pin2[g],g,i);
                                  }   }         
                     
                     
                     printf("ÊÁÔÁÓÔÁÓÇ:ÅÊÔÅËÅÓÇ ÂÏËÇÓ\n");
                     printf("ÓÕÍÔÅÔÁÃÌÅÍÅÓ ÅÎÅÑ×ÏÌÅÍÇÓ ÂÏËÇÓ: %c%d\n",c,l);
                     printf("ÁÐÏÔÅËÅÓÌÁ ÅÊÔÅËÅÓÈÅÉÓÁÓ ÂÏËÇÓ(ÐÁÔÇÓÔÅ <<E>> ÃÉÁ ÅÕÓÔÏ×Ç ÊÁÉ <<A>> ÃÉÁ ÁÓÔÏ×Ç)\n");
                     printf("ÂÏËÇ %c %d: \n",c,l);
                     
                     scanf("%s",&apotelesma);
                     
                     
                     while (apotelesma!='A' && apotelesma!='E' && apotelesma!='e' && apotelesma!='a' ){
                           printf("ËÁÈÏÓ ÅÍÔÏËÇ\nÁÐÏÔÅËÅÓÌÁ ÅÊÔÅËÅÓÈÅÉÓÁÓ ÂÏËÇÓ(ÐÁÔÇÓÔÅ <<E>> ÃÉÁ ÅÕÓÔÏ×Ç ÊÁÉ <<A>> ÃÉÁ ÁÓÔÏ×Ç)\n");
                           printf("ÂÏËÇ %c %d: \n",c,l);
                           scanf("%s",&apotelesma);
                           
                          }
                     if (apotelesma=='E' || apotelesma=='e'){
                                        return metritis1++;
                                        }
                     else if (apotelesma=='A' || apotelesma=='a'){
                          }
}                         

//***********************************************************************************************************************************************************

int sin3(char map[][MAX_COLS])
{
                     char myColchar;
                     int myColnum,myrow,col,row;
                     static int metritis2=1;
                     printf("ÊÁÔÁÓÔÁÓÇ: ËÇØÇ ÂÏËÇÓ\n");
                     printf("ÓÕÍÔÅÔÁÃÌÅÍÅÓ ÅÉÓÅÑ×ÏÌÅÍÇÓ ÂÏËÇÓ: \n");
                     
                     fflush(stdin);
                     scanf("%c",&myColchar);
                     getchar();
                     scanf("%d",&myrow);
                 
                     
                     
                     myColnum = (int)myColchar;
                     while (((myColnum>74 && myColnum<97) || (myColnum >106 && myColnum <=122)) || (myrow>MAX_ROWS || myrow<1)){
                                  printf("ÄÙÓÁÔÅ ËÁÈÏÓ ÓÕÍÔÅÔÁÃÌÅÍÅÓ\n");
                                  printf("ÊÁÔÁÓÔÁÓÇ: ËÇØÇ ÂÏËÇÓ\n");
                                  printf("ÓÕÍÔÅÔÁÃÌÅÍÅÓ ÅÉÓÅÑ×ÏÌÅÍÇÓ ÂÏËÇÓ: \n");
                                 fflush(stdin);
                                 scanf("%c",&myColchar);
                                 getchar();
                                 scanf("%d",&myrow);
                 
                                  
                                  
                                  myColnum = (int)myColchar;}
                       
                     if(myColnum >= 97 && myColnum <=106){
                          myColnum = myColnum - 97;}
                     else if((myColnum >=65) && (myColnum <=74)){
                          myColnum = myColnum - 65;}
                     col = myColnum;
                     row = myrow - 1; // Need to compensate because array starts at 0.      
                     if (map[row][col]==' '){
                          map[row][col]='X';
                          printf("ÁÐÏÔÅËÅÓÌÁ ÅÉÓÅÑ×ÏÌÅÍÇÓ ÂÏËÇÓ: ÁÍÅÐÉÔÕ×ÇÓ\n"); }               
                     else if (map[row][col]=='Ð' || map[row][col]=='Í' || map[row][col]=='Ê' || map[row][col]=='Ö' || map[row][col]=='Õ'){
                          map[row][col]='*';
                          }                          
                     
                     if (map[row][col]!='X'){
                          printf("ÁÐÏÔÅËÅÓÌÁ ÅÉÓÅÑ×ÏÌÅÍÇÓ ÂÏËÇÓ: ÅÐÉÔÕ×ÇÓ\n");
                          return metritis2++;}        
}

here it is....the problem is the program is in greek....u will need also some txt files with 10x10 objects for example .......ΠΠΠ
.............. etc

u said :'Put it in a loop. If the 2 numbers are unique, exit the loop. '

i have already done this with while but it doesn't even work to see if there is already a pair of numbers already stored

When you are using printf statements in the sin2() function, you print one value, using the %c specifier and the other using the %d specifier .... Is there any reason for that ?

Also I am not able to clearly understand what you are trying to do. Is this the question that you want to ask ? Generate 2 random numbers. If this pair already exits, throw these 2 numbers and generate 2 new random numbers ?

If so you want code some thing of this sort

count= No of unique random number pairs)
j=0       // j will represent the number of unique pairs already generated

a= 1st random number
b= 2nd random number

while(j<count)
{
     flag=0;
     for(i=0;i<j;i++)
     {
         check if a is equal to arr1[i] and b is equal to arr2[i]. if yes set some flag=1
    } 
       if flag is not set to 1 that means the pair does not already exist store these 2 numbers in j positions of arr1 and arr2. Increment the value of j
}

Edited 6 Years Ago by abhimanipal: n/a

I said:

It's your job to tell us [what's wrong] so we know what to look for.

Not just post 245 lines of code and hope we can figure it out...

Comments
LOL - it made my eyes bleed

look abhimanipal was right i
want code some thing of this sort as he said.....but are u sure that what u sent is correct?

C Syntax (Toggle Plain Text)

   1.
      count= No of unique random number pairs)
   2.
      j=0 // j will represent the number of unique pairs already generated
   3.
       
   4.
      a= 1st random number
   5.
      b= 2nd random number
   6.
       
   7.
      while(j<count)
   8.
      {
   9.
      flag=0;
  10.
      for(i=0;i<j;i++)
  11.
      {
  12.
      check if a is equal to arr1[i] and b is equal to arr2[i]. if yes set some flag=1
  13.
      }
  14.
      if flag is not set to 1 that means the pair does not already exist store these 2 numbers in j positions of arr1 and arr2. Increment the value of j
  15.
      }

the increment of j is over there??what happens if the above is in a function and j is given as a parameter from main function when we call the function?

int sin2(int g)
{                     
                     static int metritis1=1,i;
              
                     int l,c,pin[100],pin2[100];
                     char apotelesma;                
                     
                     srand ( time(NULL) ); 
                     l = rand() % 10;                     
                     c = rand() % 10;
                     c=65+c;
                     l=l+1;
                     pin[g]=c;
                     pin2[g]=l;
                     printf("%d kai %d to g einai %d\n",pin[g],pin2[g],g);
                     i=0;
                     for (i=0;i<g;i++){
                         if (pin[i]==pin[g] && pin2[i]==pin2[g]){
                                printf("%c kai %d g einai %d to i %d\n",pin[g],pin2[g],g,i);            
                                
                                l = rand() % 10;                     
                                c = rand() % 10;
                                c=65+c;
                                l=l+1;
                                pin[g]=c;
                                pin2[g]=l;
                                printf("%c kai %d g einai %d to i %d\n",pin[g],pin2[g],g,i);
                                i=0;
                                  }   }

the problem with the code above is that when a pair is found that has already been printed then it does changes but then trying to set i=0 again so that it will go inside the loop of for with i=0 it just doesn't work....i tried to transform it in while loop but didn't have any luck...any ideas?

Count is the number of unique random number pairs that you want to generate, j is the number of pairs that you have already generated, so that you will know when you have generated count pairs. In such a scenario why would you want to pass j as the parameter to the function, pass count instead

The code that you have posted in post 9 is logically in correct. Implement the algorithm that I have given

good lord, man, you've been asking variation on this same question for 3 weeks now. how much longer are we going to be here? what part aren't you getting?

abhimanipal :''The code that you have posted in post 9 is logically in correct. Implement the algorithm that I have given ''


why is it incorrect can u tell me??

int sin2(int g)
{                     
                     static int metritis1=1,i;
              
                     int l,c,pin[100],pin2[100];
                     char apotelesma;                
                     
                     srand ( time(NULL) ); 
                     l = rand() % 10;                     
                     c = rand() % 10;
                     c=65+c;
                     l=l+1;
                     pin[g]=c;
                     pin2[g]=l;
                     printf("%d kai %d to g einai %d\n",pin[g],pin2[g],g);
                     i=0;
                     while (i<g){
                         if (pin[i]==pin[g] && pin2[i]==pin2[g]){
                                printf("%c kai %d g einai %d to i %d\n",pin[g],pin2[g],g,i);            
                                
                                l = rand() % 10;                     
                                c = rand() % 10;
                                c=65+c;
                                l=l+1;
                                pin[g]=c;
                                pin2[g]=l;
                                printf("%c kai %d g einai %d to i %d\n",pin[g],pin2[g],g,i);
                                i=-1;
                                                                }
                         i++;   }

if i change this to a while loop??why doesn't it work again?is it logically incorrect and where exactly?

This article has been dead for over six months. Start a new discussion instead.