overloaded *operator, decisions, and loops

This is a discussion on overloaded *operator, decisions, and loops within the C++ Programming forums, part of the General Programming Boards category; I've just started a C++ class and my first assignment was to overload the + and * operators using data ...

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    overloaded *operator, decisions, and loops

    I've just started a C++ class and my first assignment was to overload the + and * operators using data that is read in from two separate files. The two lists can be of any size, so the temp list copies left over items instead of multiplying them.

    Part of my class

    Code:
    const int CAPACITY = 10;
    typedef short bool;
    #define true 1
    #define false !true
    
    class sequence
    {
      public:
        friend sequence operator*(const sequence& s1, const sequence& s2);	
    	
      private:
        double data[CAPACITY];
        int index;
        int used;
    };
    I've played around a bit with this which *seems* to work if either list is smaller than the other. I haven't tried it if both lists are equal, so I sense a disaster
    Code:
    sequence operator*(const sequence& s1, const sequence& s2)
    {
      sequence temp;
      int ci = 0;
    				
      while(ci < s1.size() && ci < s2.size())
      {
        temp.data[ci] = s1.data[ci] * s2.data[ci];
        ci++;
      }
    		
      switch(ci == s1.size() ?  true : false)
      {
        case true : while(ci < s2.size()) { temp.data[ci] = s2.data[ci]; ci++; }
                   break;
    	   
        case false: while(ci < s1.size()) { temp.data[ci] = s1.data[ci]; ci++; }
    	       break;
      };             
    	
      temp.used = ci;
    		
      return temp;
    }
    I know that there must be a better alternative, but I am having total brain lock as to how I should code this thing so it reads both lists and figures out which list is larger in order to copy the rest of the list to the temp.

    If any of you real programmers could throw me a bone, I'd apprciate it.

    TIA
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    What you have is right, just a basic merge where the sequences intersect, then append what's left :-)
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int i;
      int j;
      int k;
      const int asize = 5;
      const int bsize = 3;
      int a[asize] = {1,2,3,4,5};
      int b[bsize] = {5,4,3};
      int temp[10];
    
      // Multiply where the arrays intersect
      i = j = k = 0;
      while (i < asize && j < bsize)
      {
        temp[k++] = a[i++] * b[j++];
      }
    
      // Get the rest of a
      while (i < asize)
      {
        temp[k++] = a[i++];
      }
    
      // Get the rest of b
      while (j < bsize)
      {
        temp[k++] = b[j++];
      }
    
      for (int q = 0; q < k; q++)
      {
        cout<< temp[q] <<endl;
      }
      
      return 0;
    }
    *Cela*

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Thanks Cela. I believe this is exactly what I need.

    I cut and pasted bits of my original question when I first posted, so perhaps I wasn't too clear as to my problem. The data had to be manipulated using only loops. I hate it when my brain locks up.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21