Thread: very stuck please help!

  1. #16
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    well my code is pretty hefty so far, so illl give you the base,

    Code:
    cout << "which week : ";
    cin << bookings[i].week;
    
    cout << "which day   : ";
    cin << bookings[i].day;
    
    cout << "which period :";
    cin << bookings[i].period;
    
    i++

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I take it that monday is day 1, tuesday 2 etc.

    and period 1 is 1 etc.

  3. #18
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    correct.

  4. #19
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It seems that my code is pretty big...can you post your attempt?

  5. #20
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    well it sort of goes like this but it aint finished.

    Code:
    for(i=0; i<number_of_bookings; i++){
                 
                 switch(bookings[i].week){
                 case 1:
                      switch(bookings[i].day)
                      {
                      case 1:
                           switch(bookings[i].time)
                           {
                           case 1: mp1 += 1; break;
                           case 2: mp2 += 1; break;
                           case 3:
                           case 4:
                           }
                      case 2:
                          switch(bookings[i].time)
                          {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 3:
                          switch(bookings[i].time)
                          {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 4:
                          switch(bookings[i].time)
                          {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 5:
                           switch(bookings[i].time)
                           {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 6:
                           switch(bookings[i].time)
                           {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                           }
                 case 2:
                      switch(bookings[i].day)
                      {
                      case 1:
                           switch(bookings[i].time)
                           {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 2:
                          switch(bookings[i].time)
                          {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 3:
                          switch(bookings[i].time)
                          {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 4:
                          switch(bookings[i].time)
                          {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 5:
                           switch(bookings[i].time)
                           {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 6:
                           switch(bookings[i].time)
                           {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                           }
                 case 3:
                      switch(bookings[i].day){
                      case 1:
                           switch(bookings[i].time)
                           {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 2:
                          switch(bookings[i].time)
                          {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 3:
                          switch(bookings[i].time)
                          {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 4:
                          switch(bookings[i].time)
                          {
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                           }
                      case 5:
                           switch(bookings[i].time){
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                      case 6:
                           switch(bookings[i].time){
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                 case 4:
                       switch(bookings[i].day){
                      case 1:
                           switch(bookings[i].time){
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                      case 2:
                          switch(bookings[i].time){
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                      case 3:
                          switch(bookings[i].time){
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                      case 4:
                          switch(bookings[i].time){
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                      case 5:
                           switch(bookings[i].time){
                           case 1:
                           case 2:
                           case 3:
                           case 4:
                      case 6:
                           switch(bookings[i].time){
                           case 1:
                           case 2:
                           case 3:
                           case 4:
    cout << mp1/(number of mp1s)*100 << endl;
    you sort of get what i mean.

  6. #21
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Code:
    int monday[4];
          int tuesday[4];
          int wednesday[4];
          int thursday[4];
          int friday[4];
          int saturday[4];
          int dayx;
          int periodx;
    
    
          for (int x = 0; x < 50; x++) {
               dayx = bookings[x].day;
               periodx = bookings[x].time;
               if (dayx == 1) {
                  switch (periodx) {
                  case 1:
                  monday[0]++;
                  break;
                  case 2:
                  monday[1]++;
                  break;
                  case 3:
                  monday[2]++;
                  break;
                  case 4:
                  monday[3]++;
                  break;
                  default:
                  return 0;
                  }
               }
               if (dayx == 2) {
                  switch (periodx) {
                  case 1:
                  tuesday[0]++;
                  break;
                  case 2:
                  tuesday[1]++;
                  break;
                  case 3:
                  tuesday[2]++;
                  break;
                  case 4:
                  tuesday[3]++;
                  break;
                  default:
                  return 0;
                  }
               }
               if (dayx == 3) {
                  switch (periodx) {
                  case 1:
                  wednesday[0]++;
                  break;
                  case 2:
                  wednesday[1]++;
                  break;
                  case 3:
                  wednesday[2]++;
                  break;
                  case 4:
                  wednesday[3]++;
                  break;
                  default:
                  return 0;
                  }
               }
                if(dayx == 4) {
                  switch (periodx) {
                  case 1:
                  thursday[0]++;
                  break;
                  case 2:
                  thursday[1]++;
                  break;
                  case 3:
                  thursday[2]++;
                  break;
                  case 4:
                  thursday[3]++;
                  break;
                  default:
                  return 0;
                  }
               }
                if(dayx == 5) {
                  switch (periodx) {
                  case 1:
                  friday[0]++;
                  break;
                  case 2:
                  friday[1]++;
                  break;
                  case 3:
                  friday[2]++;
                  break;
                  case 4:
                  friday[3]++;
                  break;
                  default:
                  return 0;
                  }
               }
                if(dayx == 6) {
                  switch (periodx) {
                  case 1:
                  saturday[0]++;
                  break;
                  case 2:
                  saturday[1]++;
                  break;
                  case 3:
                  saturday[2]++;
                  break;
                  case 4:
                  saturday[3]++;
                  break;
                  default:
                  return 0;
                  }
               }
          }
          cout << "Monday 1: " << (100/9)*monday[0] << "%" << endl;
          cout << "Monday 2: " << (100/9)*monday[1] << "%" << endl;
          cout << "Monday 3: " << (100/9)*monday[2] << "%" << endl;
          cout << "Monday 4: " << (100/9)*monday[3] << "%" << endl;
          cout << "Tuesday 1: " << (100/9)*tuesday[0] << "%" << endl;
          cout << "Tuesday 2: " << (100/9)*tuesday[1] << "%" << endl;
          cout << "Tuesday 3: " << (100/9)*tuesday[2] << "%" << endl;
          cout << "Tuesday 4: " << (100/9)*tuesday[3] << "%" << endl;
          cout << "Wednesday 1: " << (100/9)*wednesday[1] << "%" << endl;
    Nah my way ended up being really long aswell...my calculation at the end is probably really wrong as I couldn't remember how many of each period there were meant to be.

  7. #22
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    thats a good effort bumfluff. gave me a few ideas to think about there with the arrays. however my is just gettin longer and longer. there must be an easier solution!

  8. #23
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Yeah I thought the arrays was pretty clever

    I think that after I had written that I was about to grasp a way of cutting out all of those switch statements...but excessive amounts of Geography have since knocked the thought out of me.

  9. #24
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Here I didn't check the syntax, but you can make an easier 2d array and use a loop for all the if statements. Theres more that could be done, for example check the second code.

    Code:
          int day[7][4];
          int dayx;
          int periodx;
    
          for (int i = 0; i < 50; ++i) {
               dayx = bookings[i].day;
               periodx = bookings[i].time;
               for(int j = 0; j < 6; ++j) {
                    if (dayx == j) {
                         switch (periodx) {
                              case 1:
                                   ++day[j][0];
                              break;
                              case 2:
                                   ++day[j][1];
                              break;
                              case 3:
                                   ++day[j][2];
                              break;
                              case 4:
                                   ++day[j][3];
                              break;
                              default:
                              return 0;
                         }
                    }
               }
          }
    
    
          cout << "Monday 1: " << (100/9)*day[0][0] << "%" << endl;
          cout << "Monday 2: " << (100/9)*day[0][1] << "%" << endl;
          cout << "Monday 3: " << (100/9)*day[0][2] << "%" << endl;
          cout << "Monday 4: " << (100/9)*day[0][3] << "%" << endl;
          cout << "Tuesday 1: " << (100/9)*day[1][0] << "%" << endl;
          cout << "Tuesday 2: " << (100/9)*day[1][1] << "%" << endl;
          cout << "Tuesday 3: " << (100/9)*day[1][2] << "%" << endl;
          cout << "Tuesday 4: " << (100/9)*day[1][3] << "%" << endl;
          cout << "Wednesday 1: " << (100/9)*day[2][1] << "%" << endl;
    Code:
          int day[7][4];
          int dayx;
          int periodx;
    
          for (int i = 0; i < 50; ++i) {
               dayx = bookings[i].day;
               periodx = bookings[i].time;
               for(int j = 0; j < 6; ++j) {
                    if (dayx == j) {
                         if(!periodx) return 0;
    
                         ++day[j][--periodx];
                    }
               }
          }
    
    
          cout << "Monday 1: " << (100/9)*day[0][0] << "%" << endl;
          cout << "Monday 2: " << (100/9)*day[0][1] << "%" << endl;
          cout << "Monday 3: " << (100/9)*day[0][2] << "%" << endl;
          cout << "Monday 4: " << (100/9)*day[0][3] << "%" << endl;
          cout << "Tuesday 1: " << (100/9)*day[1][0] << "%" << endl;
          cout << "Tuesday 2: " << (100/9)*day[1][1] << "%" << endl;
          cout << "Tuesday 3: " << (100/9)*day[1][2] << "%" << endl;
          cout << "Tuesday 4: " << (100/9)*day[1][3] << "%" << endl;
          cout << "Wednesday 1: " << (100/9)*day[2][1] << "%" << endl;
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #25
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Aha yes...I was part of the way there really...maybe my mysterious notion was 2D arrays.

  11. #26
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    thanks for your help guys, that algo came a little too late im afraid (was fast asleep in the mysterious world of C) cause ive got one to work and ive already included it into my documention which is now fully annotated.
    out of curiosity here it is, not quite as good as dae's but hey.

    Code:
    for(i=0; i<number_of_bookings; i++){
                 
                      switch(bookings[i].day)
                      {
                      case 1:
                           switch(bookings[i].time)
                           {
                           case 1: monp1 += 1; break;
                           case 2: monp2 += 1; break;
                           case 3: monp3 += 1; break;
                           case 4: monp4 += 1; break;
                           }
                           break;
                      case 2:
                          switch(bookings[i].time)
                          {
                           case 1: tuep1 += 1; break;
                           case 2: tuep2 += 1; break;
                           case 3: tuep3 += 1; break;
                           case 4: tuep4 += 1; break;
                           }
                           break;
                      case 3:
                          switch(bookings[i].time)
                          {
                           case 1: wedp1 += 1; break;
                           case 2: wedp2 += 1; break;
                           case 3: wedp3 += 1; break;
                           case 4: wedp4 += 1; break;
                           }
                           break;
                      case 4:
                          switch(bookings[i].time)
                          {
                           case 1: thup1 += 1; break;
                           case 2: thup2 += 1; break;
                           case 3: thup3 += 1; break;
                           case 4: thup4 += 1; break;
                           }
                           break;
                      case 5:
                           switch(bookings[i].time)
                           {
                           case 1: frip1 += 1; break;
                           case 2: frip2 += 1; break;
                           case 3: frip3 += 1; break;
                           case 4: frip4 += 1; break;
                           }
                           break;
                      case 6:
                           switch(bookings[i].time)
                           {
                           case 1: satp1 += 1; break;
                           case 2: satp2 += 1; break;
                           case 3: satp3 += 1; break;
                           case 4: satp4 += 1; break;
                           }
                           break;
                            }
                             }
                           
           cout << "Monday Period 1    : " << (100/9) * monp1 << " % " << endl;
           cout << "Period 2           : " << (100/9) * monp2 << " % " << endl;
           cout << "Period 3           : " << (100/9) * monp3 << " % " << endl;
           cout << "Period 4           : " << (100/9) * monp4 << " % " << endl;
           cout << "Tuesday Period 1   : " << (100/9) * tuep1 << " % " << endl;   
           cout << "Period 2           : " << (100/9) * tuep2 << " % " << endl;
           cout << "Period 3           : " << (100/9) * tuep3 << " % " << endl;
           cout << "Period 4           : " << (100/9) * tuep4 << " % " << endl;
           cout << "Wednesday Period 1 : " << (100/8) * wedp1 << " % " << endl;
           cout << "Period 2           : " << (100/8) * wedp2 << " % " << endl; 
           cout << "Period 3           : " << (100/8) * wedp3 << " % " << endl; 
           cout << "Period 4           : " << (100/8) * wedp4 << " % " << endl; 
           cout << "Thursday Period 1  : " << (100/8) * thup1 << " % " << endl;    
           cout << "Period 2           : " << (100/8) * thup2 << " % " << endl;
           cout << "Period 3           : " << (100/8) * thup3 << " % " << endl;
           cout << "Period 4           : " << (100/8) * thup4 << " % " << endl;
           cout << "Friday Period 1    : " << (100/8) * frip1 << " % " << endl;
           cout << "Period 2           : " << (100/8) * frip2 << " % " << endl;         
           cout << "Period 3           : " << (100/8) * frip3 << " % " << endl;
           cout << "Period 4           : " << (100/8) * frip4 << " % " << endl;
           cout << "Saturday Period 1  : " << (100/8) * satp1 << " % " << endl;
           cout << "Period 2           : " << (100/8) * satp2 << " % " << endl;
           cout << "Period 3           : " << (100/8) * satp3 << " % " << endl;
           cout << "Period 4           : " << (100/8) * satp4 << " % " << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM