Thread: Comparing 2 arrays

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Comparing 2 arrays

    I am programming a deal or no deal game for class. I am stuck at the point after a person "opens" a case. I can get the program to display the proper case number and hidden value, I can also get the program to recognize that that particular case is gone, but not that prize.

    For example, if the $500 prize is gone, the computer cannot list it. here are my functions.

    The code to open a case
    Code:
    void open (int cases[], int arraylength){
      int i= 0;
      int sentinal=0;
      int j = 0;
      int k = 0;
      
      while(sentinal==0 ){
      printf("\nChoose a case 1-10\n");
      scanf("%d",&i);
      
      if(1>i || i>10){
        printf("Out of range, try again");
      }
        else{
        if(cases[i]==0){
        printf("Already opened, try again");
        }
          else{
          printf("Case %d had $%d\n", i, cases[i-1]);
          cases[i-1]=0;
          sentinal++;
          } 
        }
      }
    }
    the code to list remaining case numbers
    Code:
    void unopenedcases (int cases[], int arraylength){
      int sentinal = 0;
      int i = 0;
      printf ("\n");
        for (i=0; i<SIZE; i++){
          sentinal = 0;
    	while (sentinal==0){
    	  if (cases[i]!=0){
    	  printf("%d ", i+1);
    	  sentinal = 1;
    	  }
    	     else{
    	     sentinal = 1;
    	     }
           }
        }
      printf ("\n");
    }
    and the problem code, to display remaining prizes
    Code:
    void prizelook (int cases[], int prizes[], int arraylength){
      int i = 0;
      int j = 0;
      
    
      while(i<SIZE ){
        if (prizes [i] == cases [j]){
          printf ("$%d ", prizes[i]);
          i++;
          }
        else{
          j++;
        }
      }
       printf ("\n");
    }
    Any ideas?

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by shel5210 View Post
    Code:
      while(i<SIZE ){
        if (prizes [i] == cases [j]){
          printf ("$%d ", prizes[i]);
          i++;
          }
        else{
          j++;
        }
      }
    }
    What exactly are you doing here? You compare the value of prizes[0] to the value of cases[0], then if they match you print prizes[0] and increment its counter, then compare prizes[1] to cases[0] and then if they don't match, inc j and compare again so then its prizes[1] == cases[1]..

    I don't see how that could be useful in any way? bit of an odd loop..Are you sure you didn't mean something like this:
    Code:
    while (i < SIZE)
        if (case[i]) /* case[i] does not equal zero */
            printf("$%d\n",prizes[i++]);

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Things that are closely associated with certain other things (characteristics or numbers), are generally better off being put into a struct:

    Code:
    struct student {
      char *firstname;
      char *lastname;
      long id_num;
      char major[20];
      char advisor[25];
    };
    
    struct student students[200]; //make an array for 200 student records
    Now you can deal with a student like an object, and have all the data on the student, at your fingertips, by using the (students[i].id_num) kind of struct dot member access.

    For your suitcases, you might want members of: prize, chosen (1 = chosen, 0 = not chosen).

    Then just get into your game loop:

    while(gameon)

    banker figures out the worth of the case, given the current probabilities
    host says: our banker has made an offer for your case. Will you give it up for N, amount of money: ("Contestant's name"), "Deal or NO deal?"
    contestant: presses key to accept or decline offer
    if(accept)
    host: you're going to give up your case for N dollars! First let's see what cases you would have chosen, if you didn't accept the offer". What would have been your next case to open?"

    etc.
    else (offer declined)

    host: "then choose the case to be opened - and good luck"
    contestant: enters case number on the keyboard
    host: you chose #(case number). Are you sure?
    //etc.


    }

    I think if you just get into the game - play the game maybe on paper or watch an episode, you'll see what you need here.

    The thing is, the idea's part is really in your territory. We can help you with the C problems to help implement your idea, but you need to put in the idea's and do the start up, generally.

    You're the lead off man. We're the clean up batters.
    Last edited by Adak; 03-30-2010 at 01:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing Arrays question
    By taugust7 in forum C++ Programming
    Replies: 36
    Last Post: 04-14-2009, 12:29 PM
  2. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-14-2007, 12:20 AM
  3. Comparing 2 elements from 2 different arrays
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 11-15-2006, 02:43 PM
  4. comparing arrays
    By dustyrain84 in forum C Programming
    Replies: 1
    Last Post: 01-21-2004, 05:52 PM
  5. Comparing Character Arrays
    By LostNotFound in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 12:42 PM