Thread: Need help reallly quick!!!!!!!!!!!!!!

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    19

    Need help reallly quick!!!!!!!!!!!!!!

    hey guyz i have to make the following program:

    Write a function to read and display the contents of names and marks. You then ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student.
    Next, sort the arrays, write them out and then ask the user for a name to search for. This time use the binarySearch to return -1 or an index. Display the student’s name and mark if found.

    The contents of names and marks are:


    Collins Bill 80
    Smith Bart 75
    Allen Jim 82
    Griffin Jim 55
    Stamey Marty 90
    Rose Geri 78
    Taylor Terri 56
    Johnson Jill 77
    Allison Jeff 45
    Looney Joe 89
    Wolfe Bill 63
    James Jean 72
    Weaver Jim 77
    Pore Bob 91
    Rutherford Greg 42
    Javens Renee 74
    Harrison Rose 58
    Setzer Cathy 93
    Pike Gordon 48
    Holland Beth 79


    I have finished my program and it looks like this:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void selectionSort(string[], int[], int);
    void displayData(const string[], const int[], int);
    void getNames(ifstream& inStream, string[], int[], int);
    int searchList(string list[], int numElems, string value);
    int binarySearch(string list[], int size, string value);
    
    
    const int numElems = 20;
    const int NUM_NAME = 20;
    int main ()
    {
    
    	
    
        string first_name[20];
        string last_name[20];
        string names[20];
        int marks[20];
        ifstream inStream;
        inStream.open("TextFile4.txt");  
    
    	getNames(inStream, names, marks, NUM_NAME);
    	displayData(names, marks, NUM_NAME);
    
    
        string value, fname, lname;
        cout<<"Please enter the name you would like to find: "<<endl;
        cin>>lname>>fname;
        value = lname + " " + fname;
    
        int result1 = searchList(names, numElems, value);
        if (result1==-1)
        {
            cout <<"Can not find the name!"<<endl;
        }
        else
            cout<<names[result1]<<" "<<marks[result1];
    
    	selectionSort(names, marks, numElems);
    	displayData(names, marks, NUM_NAME);
    
    	cout<<"Please enter the name you would like to find: "<<endl;
        cin>>lname>>fname;
        value = lname + " " + fname;
    
        int result2 = binarySearch(names, numElems, value);
        if (result2==-1)
        {
            cout <<"Can not find the name!"<<endl;
        }
        else
            cout<<names[result2]<<" "<<marks[result2];
    
    
        return 0;
    }
    
    void getNames(ifstream& inStream, string names[], int marks[], int numElems)
    {
    	string fname[NUM_NAME];
    	string lname[NUM_NAME];
    	inStream.open("file2.txt");    
    
        for(int i=0; i<numElems; i++)
        {
    
    		inStream>>lname[i]>>fname[i]>>marks[i];
    	}
    	for(int i=0; i<numElems; i++)
    	{
            names[i] = lname[i] + " " + fname[i];
        }
    }
    
    
    void displayData(const string names[] ,const int marks[], int numElems)
    {
    	for (int i = 0; i < numElems; i++)
    	{
    		cout<<names[i]<<" "<<marks[i]<<endl;
    	}
    }
    int searchList(string list[], int numElems, string 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
    }
    
    void selectionSort(string names[], int marks[], int numElems)
     {
        int startScan, minIndex;
    	string minValue;
    	int value;
     
        for (startScan = 0; startScan < (numElems - 1); startScan++)
        {
           minIndex = startScan;
           minValue = names[startScan];
           for(int index = startScan + 1; index < numElems; index++)
          {
              if (names[index] < minValue)
            {
                 minValue = names[index];
                 minIndex = index;
            }
    	   }
    	   value = marks[minIndex];
           names[minIndex] = names[startScan];
           names[startScan] = minValue;
    	   marks[minIndex] = marks[startScan];
    	   marks[startScan] = value;
        }
     }
    
    int binarySearch(string list[], int size, string value)
    {
       int first = 0,             // First list element
           last = size - 1,       // Last list 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 (list[middle] == value)      // If value is found at mid
          {
             found = true;
             position = middle;
          }
          else if (list[middle] > value)  // If value is in lower half
             last = middle - 1;
          else
             first = middle + 1;           // If value is in upper half
       }
       return position;
    }
    When i run this program it has no trouble compiling but it shows me random numbers. can u guyz plz tell me if i am something...or if i did something wrong....ty in advance.

  2. #2
    Registered User
    Join Date
    Dec 2009
    Posts
    19

    Help needed soon!

    can somebody plz just copy paste my program into their comp and lemme know wat the error is.....i really need this quick.............its due 2morrow........soo plz help me out guyz!

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    A lack of planning on your part does not constitute an emergency on ours.

    Impatience and placing tons of exclamation points in the title of your thread just about guarantees no one is going to help you much less view this thread.
    Last edited by VirtualAce; 12-07-2009 at 10:32 PM.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    19
    y dont u cut the crap....and help me if u know anything bout this. im pretty sure that u r 1 of those losers who sit on these type of threads all day long and have no clue bout programming watsoever.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  4. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM