Thread: Array problems

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    10

    Array problems

    Hi all C++ gurus
    i have a little problem with my codes
    basically i have 5,6,7,8,9 in my array

    i m supposed to come up with a program that will run through the numbers in the array...if any number that is more than 7 will be replaced by 7

    therefore it should look like this

    original: 5,6,7,8,9
    sorted: 5,6,7,7,7

    however when i run
    it looks like this

    original: 5,6,7,8,9
    sorted 5,7,7,7,0

    can anyone pls help with me this becoz i m rather new in c++??
    thanks


    Code:
    #include <vcl.h>
    #include <condefs.h>
    #include "MT262io.h"
    #define Size 5
    
    #pragma hdrstop
    
    void displayArray (int Array[Size]);
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
    
         int MyArray[Size] = {5,6,7,8,9};
        int TopOfUnsorted;
        int IndexOfLargest =7;
        int IndexOfSmallest;
        int Index;
        int TempInt;
    
        WriteString("Original Array : ");
        displayArray(MyArray);
    
        for (TopOfUnsorted=Size-1; TopOfUnsorted>0;    TopOfUnsorted = TopOfUnsorted-1)
        {
    
    
            for (Index=0; Index<=TopOfUnsorted; Index = Index + 1)
            {
                if (MyArray[Index] >= MyArray[IndexOfLargest])
                {
                    Index = IndexOfLargest;
                }
    
    
            }
            //-- swap the position of the largest element with the last
            //-- element in the unsorted part of the array
            TempInt = MyArray[TopOfUnsorted];
            MyArray[TopOfUnsorted] = MyArray[IndexOfLargest];
            MyArray[IndexOfLargest] = IndexOfLargest;
        }
    
        WriteString("Sorted array   : ");
        displayArray(MyArray);
    
    
            getchar();
            return 0;
    }
    
        void displayArray(int Array[Size])
    {
        int Index;
        for (Index=0; Index<Size; Index = Index + 1)
        {
            WriteIntPr(" ", Array[Index]);
        }
        WriteString("\n");
        return;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > int IndexOfLargest =7;
    But you only have 5 elements in your array, so using this as an index is a bad idea.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    10
    but my intention is to compare all the values in the array to 7
    all those numbers that is larger than 7 will be converted to 7...

    can anyone advice how i can do that??

  4. #4
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14
    Are there any restrictions on your assignment; as far as what you HAVE to have included? Or do you just need to write code to convert 5, 6, 7, 8, 9 to 5, 6, 7, 7, 7?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > but my intention is to compare all the values in the array to 7
    Yeah, you said that already.
    But look at your code

    You have things like this
    MyArray[Index] >= MyArray[IndexOfLargest]
    Which on the first pass of your code reduces to
    MyArray[0] >= MyArray[7]
    Your code is now dead in the water, you just accessed an out of bound array element.

    And also your swap code
    Code:
            TempInt = MyArray[TopOfUnsorted];
            MyArray[TopOfUnsorted] = MyArray[IndexOfLargest];
            MyArray[IndexOfLargest] = IndexOfLargest;  // Duh?? should be TempInt?
    You're not storing back into the array a value you read from the array.
    This is why it fills up with 7's

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    10
    thank you all so much
    but still even when i managed to compiled
    i still don't get the result i wanted....

    i set min =2; max= 8

    so my original array {0,1,2,3,4,5,6,7,8,9)

    shd look like this when i run the function

    {2,2,2,3,4,5,6,7,8,8}

    but i get

    {8,8,8,8,8,8,8,8} lol

    can anyone please enlighten me??
    thanks


    Code:
    #include <vcl.h>
    #include <condefs.h>
    #include "MT262io.h"
    #define Size 10
    
    #pragma hdrstop
    
    void setArray(int NumberArray[10]);
    void MinMaxLimit(int min, int max,int NumberArray[10]);
    float Average (int NumberArray[10]);
    void displayArray(int NumberArray[10]);
    
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
    
          int MyArray[10] = {0,1,2,3,4,5,6,7,8,9};
          int minimum =2;
          int maximum =8;
    
          MinMaxLimit(minimum, maximum ,MyArray);
    
          displayArray(MyArray);
    
        
         WriteStringCr (Average (MyArray));
    
        getchar();  //-- hold the screen
    
    }
    
    
    
    void setArray(int NumberArray[10])
    {
        int num[10] = {0,1,2,3,4,56,7,8,9};
    	setArray(num);
    	return;
    
    }
    
    void MinMaxLimit(int min, int max,int NumberArray[10])
    
    {
            int counter;
    
            for(counter =0; counter <10; counter ++)
            {
              if (NumberArray[counter] < min)
                  {
                  min =min +1;
                   }
               if (NumberArray[counter] > max )
                  {
                  max =max +1;
                  }
                counter = counter +1;
             }
    }
    
    float Average (int NumberArray[10])
    {
           int total;
           float average;
           int counter;
    
           for(counter =0; counter <10; counter ++)
           {
             total = total + NumberArray[counter];
           }
           average = total/10;
    
           return average;
    }
    
    void displayArray(int NumberArray[10])
    {
        int Index;
        for (Index=0; Index<Size; Index = Index + 1)
        {
            WriteIntPr(" ", NumberArray[10]);
        }
        WriteString("\n");
        return;
    }

  7. #7
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I'll assume the undefined functions are implemented somewhere in the headers you include.

    As for the actual program, I have no idea how your array ends up as all 8s since you never change the value of an element of your array. Most likely, it happens in one of those other functions.

    I hope you're not thinking MinMixLimit changes appropriate elements to the min or max. Look again at that function and tell me - what does it do?
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Problems passing an array to a function
    By ndenklau in forum C Programming
    Replies: 5
    Last Post: 09-20-2006, 08:14 AM
  4. Helllppp!!! I/O and array problems
    By dorky in forum C++ Programming
    Replies: 3
    Last Post: 07-02-2005, 09:24 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM