Need some help with arrays

This is a discussion on Need some help with arrays within the C++ Programming forums, part of the General Programming Boards category; I am trying to get my program to return the smallest number and then output my list in reverse order. ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    15

    Need some help with arrays

    I am trying to get my program to return the smallest number and then output my list in reverse order. My largest function works but I can't figure out how to do the smallest or the reverse order.
    Please help

    Code:
    
    
    int main()
    {
    
    	printArray (listA, arraySize);
    	cout << endl << endl;
    
         
    	cout << "Largest Number: "
    	<< listA [indexLargestElement(listA, arraySize)]
    	<< endl << endl;
    
    	
    	cout << "Smallest Number: "
                          ???????
    
          //output the reverse order
    	printArrayReverse (listA, 0, 5);
    	cout << endl << endl;
    
        return 0;
    }
    
    {
       
    void printArray (const int list[], int listSize)
    {
    	int index;
    
    	for (index = 0; index < listSize; index++)
    		cout << list[index] << " ";
    }
     
    int indexLargestElement (const int list[], int listSize)
    {
    	int index;
    	int maxIndex = 0;
    
    	for (index = 1; index < listSize; index++)
    		if (list[maxIndex] < list[index])
    			maxIndex = index;
    
    	return maxIndex;
    }
    
    
    int indexSmallestElement (const int list[], int listSize)
    {
    ?????????????????????????
    }
     //function print the elements of an int array in reverse
    void printArrayReverse (const int list[], int listSize)
    {
        ????????????????????
    }
    Last edited by newbiec++; 10-05-2005 at 09:49 PM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    the quick and easy method would be just to use the reverse( ) function from the <algorithm> library:

    Code:
    reverse(&list[0], &list[5]);   //pass in the first element and last element of the range of the array to be sorted
    
    cout << "In reverse order:  ";
    
    for(int  i=0; i<5; i++)
    
          cout << list[i] << "   ";

    using the swap( ) function from <algorithm>
    Code:
    for(int i=0, j=array_size; i<array_size/2; i++, j--)
    
         swap(&list[i], &list[j]);

    or.. write your own reverse algorithm:
    Code:
    for(int i=0, j=array_size, temp; i<array_size/2; i++, j--)
    {
         temp = list[i];
    
         list[i] = list[j];
    
         list[j] = temp;
    }



    jmd15's method below is probably a more efficient method of what you are wanting to do though



    code == good; //
    Last edited by The Brain; 10-05-2005 at 09:23 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Its pretty easy man you just need to use your brain.

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Print out the array in reverse just initialize index to listSize and instead of incrementing it, decrement it. Like below:
    Code:
          	int index;
    
    	for (index = listSize; index >=0; index--)
    		cout << list[index] << " ";
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    15
    I used the follwing code that was provided by jmd15
    Code:
    int index;

    for (index = listSize; index >=0; index--)
    cout << list[index] << " ";
    [/code]

    It does print out the reverse, but when I input 1, 2, 3, 4, 5 it outputs 5, 4, 3, 2, 1 but it also has a large negative number in front of it -85899460 5 4 3 2 1

    What do I need to change?
    Also, any ideas on how to output the smallest number?

    Thanks for the help.

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Try:
    Code:
    for (index = listSize-1; index >=0; index--)
    Instead because it needs 1 extra space in the array.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    classic symptom of accessing memory outside the bounds of the array.. easy fix. a little bit of trial and error troubleshooting on your part and you would have had it figured out all by yourself.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    15
    It worked !!!!!

    I don't know if I could have figured it out by myself I am still very green.
    Brain your code is way over my head right now. I hope to get that good one day.

    To output the smallest number it should have similar code right ????

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    To output the smallest number it should have similar code right ????
    once your array is sorted.. you should know exactly where the smallest and largest numbers are located
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    15
    I thought I got the smallest function to work but it only returns the first number entered.
    Here is what I put in:

    Code:
    int indexSmallestElement (const int list[], int listSize)
    {
    	int index;
    	int minIndex = 0; //assume the first element is the smallest
    
    	for (index = -1; index >=0; index--)
    		if (list[minIndex] < list[index])
    			minIndex = index;
    
    	return minIndex;
    How do I get this to return the smallest number????
    Last edited by newbiec++; 10-05-2005 at 09:53 PM.

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    for (index = -1; index >=0; index--)
    need to use loop conditions that meet the need of your array algorithm.
    Last edited by The Brain; 10-05-2005 at 11:24 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    15
    I fixed it and it works now.
    Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 03:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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