Thread: How To SORT DATA In Database With C/C++?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    32

    Question How To SORT DATA In Database With C/C++?

    There are many books on database programming available for VB 6 & Java in the market but I could not find any for C or C++.

    I would like to know how to sort data from database created with Microsoft Access and display it to the screen.

    I have the source code for sorting from Deitel & Deitel's book. I hope any C++ expert out there could tell me how to sort the 99 numbers if the data is in Microsoft Access database instead of the source code.

    If you have any source code on searching and sorting the database with C or C++, or if you know any online resources or tutorial, I would appreciate it very much if you could post it here.

    Thanks.

    Code:
    #include<iostream>
    using std::cout;
    using std::endl;
    using std::ios;
    #include<iomanip>
    
    using std::setw;
    using std::setiosflags;
    using std::setprecision;
    
    void mean(const int[], int);
    void median(int[], int);
    void mode(int[], int[], int);
    void bubbleSort(int[], int);
    void printArray(const int[], int);
    
    int main()
    {
    	const int responseSize = 99;
    	int frequency[10] = {0}, 
    		response[responseSize] = 
    	{6,7,8,9,8,7,8,9,8,9,
    	7,8,9,5,9,8,7,8,7,8,
    	6,7,8,9,3,9,8,7,8,7,
    	7,8,9,8,9,8,9,7,8,9,
    	6,7,8,7,8,7,9,8,9,2,
    	7,8,9,8,9,8,9,7,5,3,
    	5,6,7,2,5,3,9,4,6,4,
    	7,8,9,6,8,7,8,9,7,8,
    	7,4,4,2,5,3,8,7,5,6,
    	4,5,6,1,6,5,7,8,7};
    
    	mean(response, responseSize);
    	median(response, responseSize);
    	mode(frequency, response, responseSize);
    
    	return 0;
    }
    
    void mean(const int answer[], int arraySize)
    {
    	int total = 0;
    
    	cout<<"********\n Mean\n********\n";
    	for(int j =0; j < arraySize; j++)
    		total += answer[j];
    	cout<<"The mean is the average value of the data\n"
    		<<"items. The mean is equal to the total of\n"
    		<<"all the data items divided by the number\n"
    		<<"of data items ("<< arraySize
    		<<"). The mean value for\nthis run is: "
    		<< total <<" / " << arraySize << " = "
    		<<setiosflags(ios::fixed | ios::showpoint)
    		<<setprecision(4)
    		<<static_cast<double>(total) / arraySize << "\n\n";
    }
    
    void median(int answer[], int size)
    {
    	cout<<"********\n Median\n********\n"
    		<<" The unsorted array of response is";
    	printArray(answer, size);
    	bubbleSort(answer, size);
    	cout<<"\n\nThe sorted array is";
    	printArray(answer, size);
    	cout<<"\n\nThe median is element " << size/2
    	   <<" of \nthe sorted " <<size
    	   <<" element array. \nFor this run the median is "
    	   <<answer[size/2] << "\n\n";
    }
    
    void mode(int freq[], int answer[], int size)
    {
    	int rating, largest = 0, modeValue = 0;
    		cout<<"********\n Mode\n********\n";
    
    		for(rating = 1; rating <= 9; rating++)
    			freq[rating] = 0;
    		for(int j = 0; j < size; j++)
    			++freq[answer[j]];
    		cout<<"Response"<<setw(11)<<"Frequency"
    			<<setw(19)<<"Histogram\n\n"<<setw(55)
    			<<"1	1	2	2\n"<<setw(56)
    			<<"5	0	5	0	5\n\n";
    		for(rating =1; rating <= 9; rating++)
    		{
    			cout<<setw(8)<<rating<<setw(11)
    				<<freq[rating]<<"		";
    			if(freq[rating] > largest)
    			{
    			largest = freq[rating];
    			modeValue = rating;
    			}
    		for(int h = 1; h <= freq[rating]; h++)
    			cout<< '*';
    		cout<<'\n';
    		}
    
    cout<<"The mode is the most frequent value.\n"
    	<<"For this run the mode is "<<modeValue
    	<<"which occured "<<largest<<" times. "<<endl;
    }
    
    
    void bubbleSort(int a[], int size)
    {
    	int hold;
    	for(int pass =1; pass <size; pass++)
    		for(int j =0; j < size - 1; j++)
    			if(a[j] > a[j + 1])
    			{
    				hold = a[j];
    				a[j] = a[j+1];
    				a[j+1] = hold;
    			}
    }
    
    void printArray(const int a[], int size)
    {
    	for(int j =0; j < size; j++)
    	{
    		if(j % 20 ==0)
    			cout<<endl;
    		cout<<setw(2) << a[j];
    	}
    }
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You use your favourite database connector and send an SQL query to retrieve the data sorted.

    MS Access is not a general C++ issue.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    32

    Question

    So sorry to bump this thread.

    Anyone out there could help me with this problem?


    Thanks.


    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I answered your question. If you don't understand my answer then tell me so. Show me what you don't understand and I'll try to help you.

    But don't simply ignore my reply
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  5. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM