Thread: Can i get some help with sorting arrays

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Can i get some help with sorting arrays

    i don't want you to do it for me but i don't know where i went wrong.
    can someone plz guide me through how to finish this without getting errors.
    this is the original question:
    This lab is to give you more experience with C++ Searching and Sorting Arrays

    Given two arrays shown later rewrite the following functions:

    1. Linear Search
    Modify the searchList function given below so that it searches for a given name rather than an int. The functions returns and int which is the index of the name found. If -1 is returned then say name is not found otherwise write out the name and the mark for that name.

    insert
    Code:
    int searchList(int list[], int numElems, int value)
    {
       int index = 0;      // Used as a subscript to search array
       int position = -1;  // To record position of search value
       bool found = false; // Flag to indicate if value was found
    
       while (index < numElems && !found)
       {
          if (list[index] == value) // If the value is found
          {
             found = true; // Set the flag
             position = index; // Record the value's subscript
          }
          index++; // Go to the next element
       }
    return position; // Return the position, or -1
    2. Selection Sort
    Modify the selectionSort given below so that it sorts by name instead of an int. Be sure to accept both arrays for sorting purposes. Write out the arrays before and after sorting.

    insert
    Code:
    void selectionSort(int array[], int size)
     {
        int startScan, minIndex, minValue;
     
        for (startScan = 0; startScan < (size - 1); startScan++)
        {
           minIndex = startScan;
           minValue = array[startScan];
           for(int index = startScan + 1; index < size; index++)
          {
              if (array[index] < minValue)
            {
                 minValue = array[index];
                 minIndex = index;
            }
         }
           array[minIndex] = array[startScan];
           array[startScan] = minValue;
        }
     }
    3. Binary Search
    Modify the binarySearch function given below so that it searches for a given name rather than an int. The functions returns and int which is the index of the name found. If -1 is returned then say name is not found otherwise write out the name and the mark for that name.
    insert
    Code:
    int binarySearch(int array[], int size, int value)
    {
       int first = 0,             // First array element
           last = size - 1,       // Last array element
           middle,                // Mid point of search
           position = -1;         // Position of search value
       bool found = false;        // Flag
    
       while (!found && first <= last)
       {
          middle = (first + last) / 2;     // Calculate mid point
          if (array[middle] == value)      // If value is found at mid
          {
             found = true;
             position = middle;
          }
          else if (array[middle] > value)  // If value is in lower half
             last = middle - 1;
          else
             first = middle + 1;           // If value is in upper half
       }
       return position;


    this is what i have insert
    Code:
    
    
    
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    
    
    
    
    
    const int classsize = 20;
    
    
    string names[classsize];
    
    
    int marks[classsize];
     
    int count;
    
    
    
    
    
    
    
    
    
    
    int displayData(string [],int [],int);
    
    
    void binarySearch(string [],int [],int,string);
    
    
    
    
    void selectionSortnames(string [],int [],int);
    
    
    int selectionSortmarks(int [],int);
    
    
    
    
    int main()
    {
    string names[classsize] = {
    
    
    "Bill Collins", "Bart Smith", "Jim Allen", "Jim Griffen", "Marty Stamey", "Geri Rose", "Terri Taylor", "Jill Johnson", "Jeff Allison", "Joe Looney", "Bill Wolfe", "Jean James", "Jim Weaver", "Bob Pore", "Greg Rutherford", "Renee Javens", "Rose Harrison", "Cathy Setzer", "Gordon Pike", "Beth Holland"};
    
    
    
    
    int marks[classsize] = {80,75,82,55,90,78,56,77,45,89,63,72,77,91,42,74,58,93,48,79};
    selectionSortnames(names,marks,classsize);
    
    
    }
    
    
    
    
    void selectionSortnames(string names[],int marks[],int class_size)
    {
    
    
    
    
    int startScan;
    
    
    
    
    int minIndex;
    string minValue;
    
    
    
    
    int minValue2;
    displayData(names,marks,count);
    
    
    
    
    
    
    for (startScan = 0; startScan < (classsize - 1); startScan++)
    {
    minIndex = startScan;
    minValue = names[startScan];
    
    
    
    
    for(int index = startScan + 1; index < classsize; index++)
    {
    
    
    
    
    if (names[index] < minValue)
    {
    minValue = names[index];
    minIndex = index;
    }
    }
    names[minIndex] = names[startScan];
    names[startScan] = minValue;
    }
    
    
    
    
    for (startScan = 0; startScan < (classsize - 1); startScan++)
    {
    minIndex = startScan;
    minValue2 = marks[startScan];
    
    
    
    
    for(int index = startScan + 1; index < classsize; index++)
    {
    
    
    
    
    if (marks[index] < minValue2)
    {
    minValue2 = marks[index];
    minIndex = index;
    }
    }
    marks[minIndex] = marks[startScan];
    marks[startScan] = minValue2;
    }
    displayData(names,marks,count);
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent properly, remove excess lines and follow the advice in the following article first: SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting through arrays
    By JaKXz in forum C Programming
    Replies: 8
    Last Post: 03-05-2010, 02:11 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Help with sorting arrays
    By Silence in forum C Programming
    Replies: 5
    Last Post: 05-17-2002, 10:05 AM
  5. Sorting Arrays
    By Jax in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 12:35 PM