Thread: Array help

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    3

    Post Array help

    Right now im currently trying to write a piece of code where if array[0] is lowest i want to run a certain code but if array[3] is lowest number i want to run a different set of code. Currently i have the following although it doesnt really work as intended and i know there would be a much easier way to do it.

    Code:
    if((IRvalue[0] <= IRvalue[1]) && (IRvalue[0] <= IRvalue[2]) && (IRvalue[0] <= IRvalue[3]) && (IRvalue[0] <= IRvalue[4]) && (IRvalue[0] <= IRvalue[5]) && (IRvalue[0] <= IRvalue[6]) && (IRvalue[0] <= IRvalue[7])){
           PrintMessage("CMD_ACT_ROT_0_315");
           lcd.print("0");
         }
         if((IRvalue[1] <= IRvalue[0]) && (IRvalue[1] <= IRvalue[2]) && (IRvalue[1] <= IRvalue[3]) && (IRvalue[1] <= IRvalue[4]) && (IRvalue[1] <= IRvalue[5]) && (IRvalue[1] <= IRvalue[6]) && (IRvalue[1] <= IRvalue[7])){
           PrintMessage("CMD_ACT_ROT_0_270");
           lcd.print("1");
         }
         if((IRvalue[2] <= IRvalue[0]) && (IRvalue[2] <= IRvalue[1]) && (IRvalue[2] <= IRvalue[3]) && (IRvalue[2] <= IRvalue[4]) && (IRvalue[2] <= IRvalue[5]) && (IRvalue[2] <= IRvalue[6]) && (IRvalue[2] <= IRvalue[7])){
           PrintMessage("CMD_ACT_ROT_0_225");
           lcd.print("2");
         }
         if((IRvalue[3] <= IRvalue[0]) && (IRvalue[3] <= IRvalue[1]) && (IRvalue[3] <= IRvalue[2]) && (IRvalue[3] <= IRvalue[4]) && (IRvalue[3] <= IRvalue[5]) && (IRvalue[3] <= IRvalue[6]) && (IRvalue[3] <= IRvalue[7])){
           PrintMessage("CMD_ACT_ROT_0_180");
           lcd.print("3");
         }
         if((IRvalue[4] <= IRvalue[0]) && (IRvalue[4] <= IRvalue[1]) && (IRvalue[4] <= IRvalue[2]) && (IRvalue[4] <= IRvalue[3]) && (IRvalue[4] <= IRvalue[5]) && (IRvalue[4] <= IRvalue[6]) && (IRvalue[4] <= IRvalue[7])){
           PrintMessage("CMD_ACT_ROT_0_135");
           lcd.print("4");
         }
         if((IRvalue[5] <= IRvalue[0]) && (IRvalue[5] <= IRvalue[1]) && (IRvalue[5] <= IRvalue[2]) && (IRvalue[5] <= IRvalue[3]) && (IRvalue[5] <= IRvalue[4]) && (IRvalue[5] <= IRvalue[6]) && (IRvalue[5] <= IRvalue[7])){
           PrintMessage("CMD_ACT_ROT_0_90");
           lcd.print("5");
         }
         if((IRvalue[6] <= IRvalue[0]) && (IRvalue[6] <= IRvalue[1]) && (IRvalue[6] <= IRvalue[2]) && (IRvalue[6] <= IRvalue[3]) && (IRvalue[6] <= IRvalue[4]) && (IRvalue[6] <= IRvalue[5]) && (IRvalue[6] <= IRvalue[7])){
           PrintMessage("CMD_ACT_ROT_0_45");
           lcd.print("6");
         }
         if((IRvalue[7] <= IRvalue[0]) && (IRvalue[7] <= IRvalue[1]) && (IRvalue[7] <= IRvalue[2]) && (IRvalue[7] <= IRvalue[3]) && (IRvalue[7] <= IRvalue[4]) && (IRvalue[7] <= IRvalue[5]) && (IRvalue[7] <= IRvalue[6])){
           PrintMessage("CMD_ACT_ROT_0_0");
           lcd.print("7");

  2. #2
    Registered User
    Join Date
    Oct 2020
    Posts
    3
    any help would be much appreciated as im quite new to coding

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,082
    Quote Originally Posted by jazzamate
    Right now im currently trying to write a piece of code where if array[0] is lowest i want to run a certain code but if array[3] is lowest number i want to run a different set of code.
    You say that, yet it looks like you have 8 different pieces of code that you want to run, not just 2

    What I would suggest is to #include <algorithm> then do something like this:
    Code:
    auto lowest = *std::min_element(IRvalue, IRvalue + sizeof(IRvalue) / sizeof(IRvalue[0]));
    if (lowest == IRValue[0]) {
        PrintMessage("CMD_ACT_ROT_0_315");
        lcd.print("0");
    } else if (lowest == IRValue[1]) {
        PrintMessage("CMD_ACT_ROT_0_270");
        lcd.print("1");
    } // etc
    Instead of the if-else chain, it would be more flexible to put all those strings to be printed into an array (of pairs/tuples, perhaps) then use a loop to select the appropriate strings to be printed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2020
    Posts
    3
    although that looks like it would work im trying to do this without the use of non c standard librarys

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,082
    Quote Originally Posted by jazzamate
    although that looks like it would work im trying to do this without the use of non c standard librarys
    <algorithm> and std::min_element are part of the C++ standard library. It is true that they are not part of the C standard library, but you're programming in C++, not C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,294
    On the assumption that @jazzamate just posted in the wrong forum, a C answer.

    A bit of advice.
    If you have arrays, you should be looking at loops (not copy/pasting specific subscripts).

    Compare.
    Code:
    #include <stdio.h>
    
    void PrintMessage(const char *msg)
    {
      printf("%s\n", msg);
    }
    
    struct {
      void (*print) (const char *);
    } lcd = {
      PrintMessage
    };
    
    void foo(int IRvalue[8])
    {
      if ((IRvalue[0] <= IRvalue[1]) && (IRvalue[0] <= IRvalue[2]) && (IRvalue[0] <= IRvalue[3])
          && (IRvalue[0] <= IRvalue[4]) && (IRvalue[0] <= IRvalue[5]) && (IRvalue[0] <= IRvalue[6])
          && (IRvalue[0] <= IRvalue[7])) {
        PrintMessage("CMD_ACT_ROT_0_315");
        lcd.print("0");
      }
      if ((IRvalue[1] <= IRvalue[0]) && (IRvalue[1] <= IRvalue[2]) && (IRvalue[1] <= IRvalue[3])
          && (IRvalue[1] <= IRvalue[4]) && (IRvalue[1] <= IRvalue[5]) && (IRvalue[1] <= IRvalue[6])
          && (IRvalue[1] <= IRvalue[7])) {
        PrintMessage("CMD_ACT_ROT_0_270");
        lcd.print("1");
      }
      if ((IRvalue[2] <= IRvalue[0]) && (IRvalue[2] <= IRvalue[1]) && (IRvalue[2] <= IRvalue[3])
          && (IRvalue[2] <= IRvalue[4]) && (IRvalue[2] <= IRvalue[5]) && (IRvalue[2] <= IRvalue[6])
          && (IRvalue[2] <= IRvalue[7])) {
        PrintMessage("CMD_ACT_ROT_0_225");
        lcd.print("2");
      }
      if ((IRvalue[3] <= IRvalue[0]) && (IRvalue[3] <= IRvalue[1]) && (IRvalue[3] <= IRvalue[2])
          && (IRvalue[3] <= IRvalue[4]) && (IRvalue[3] <= IRvalue[5]) && (IRvalue[3] <= IRvalue[6])
          && (IRvalue[3] <= IRvalue[7])) {
        PrintMessage("CMD_ACT_ROT_0_180");
        lcd.print("3");
      }
      if ((IRvalue[4] <= IRvalue[0]) && (IRvalue[4] <= IRvalue[1]) && (IRvalue[4] <= IRvalue[2])
          && (IRvalue[4] <= IRvalue[3]) && (IRvalue[4] <= IRvalue[5]) && (IRvalue[4] <= IRvalue[6])
          && (IRvalue[4] <= IRvalue[7])) {
        PrintMessage("CMD_ACT_ROT_0_135");
        lcd.print("4");
      }
      if ((IRvalue[5] <= IRvalue[0]) && (IRvalue[5] <= IRvalue[1]) && (IRvalue[5] <= IRvalue[2])
          && (IRvalue[5] <= IRvalue[3]) && (IRvalue[5] <= IRvalue[4]) && (IRvalue[5] <= IRvalue[6])
          && (IRvalue[5] <= IRvalue[7])) {
        PrintMessage("CMD_ACT_ROT_0_90");
        lcd.print("5");
      }
      if ((IRvalue[6] <= IRvalue[0]) && (IRvalue[6] <= IRvalue[1]) && (IRvalue[6] <= IRvalue[2])
          && (IRvalue[6] <= IRvalue[3]) && (IRvalue[6] <= IRvalue[4]) && (IRvalue[6] <= IRvalue[5])
          && (IRvalue[6] <= IRvalue[7])) {
        PrintMessage("CMD_ACT_ROT_0_45");
        lcd.print("6");
      }
      if ((IRvalue[7] <= IRvalue[0]) && (IRvalue[7] <= IRvalue[1]) && (IRvalue[7] <= IRvalue[2])
          && (IRvalue[7] <= IRvalue[3]) && (IRvalue[7] <= IRvalue[4]) && (IRvalue[7] <= IRvalue[5])
          && (IRvalue[7] <= IRvalue[6])) {
        PrintMessage("CMD_ACT_ROT_0_0");
        lcd.print("7");
      }
    }
    
    int smallest_index(int IRvalue[8])
    {
      int small = IRvalue[0];
      int small_i = 0;
      for (int i = 1; i < 8; i++) {
        if (IRvalue[i] <= small) {
          small = IRvalue[i];
          small_i = i;
        }
      }
      return small_i;
    }
    
    void bar(int IRvalue[8])
    {
      int p = smallest_index(IRvalue);
      char buff[20];
      sprintf(buff, "CMD_ACT_ROT_0_%d", (7 - p) * 45);
      PrintMessage(buff);
      sprintf(buff, "%d", p);
      lcd.print(buff);
    }
    
    int main()
    {
      int arr[8] = { 5, 4, 3, 2, 1, 2, 3, 4 };
      foo(arr);
      bar(arr);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  2. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread