Creating a array sensor

This is a discussion on Creating a array sensor within the C++ Programming forums, part of the General Programming Boards category; Hello, Everyone: I am trying to program the following: The bubble sort is inefficient for large arrays. Make the following ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    30

    Creating a array sensor

    Hello, Everyone:

    I am trying to program the following:

    The bubble sort is inefficient for large arrays. Make the following simple modifications to improve the performance of the bubble sort:
    a. After the first pass, the largest number is guaranteed to be in the highest-number element of the array; after the second pass, the two highest numbers are "in place," and so on. Instead of making nince comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass, and so on.

    b. The data in the array may already be in the proper order or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass if any sway have been made. If none have been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed.

    Here is the code I come up with:
    Code:
    #include<iostream>
    #include<fstream>
    #include<cstdlib>
    using namespace std;
    
    int main()
    {
        double time, motion;
        ifstream sensin;
        ofstream sensout;
    
        sensin.open("sensin.dat");
        sensout.open("sensout.dat");
    
        sensin >> time >> motion;
    
        int num_data_pts = 10;
        double max = motion, min = motion, sum = 0;
    
        while(!sensin.eof())
        {
             sum += motion;
             if( motion > max);
                 max = motion;
             if( motion < min);
                 min = motion;
        sensin >> time >> motion;
        }
    
        cout << "Number of Readings: " << num_data_pts << endl;
        cout << "Average Reading: " << sum/(num_data_pts) << endl;
        cout << "Maximum Reading: " << max << endl;
        cout << "Minimum Reading: " << min << endl;
    
        sensin.close();
    
    return 1;
    }
    When I compile the array, it does not show me anything. What did I do wrong in the program. Please anyone help me, I really would appreciate any help.

    Thank you..

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    First, if statements. They either 'do' until the next ; or whatever is in the {} following.
    you have if(motion > man);
    That means you have nothing for your if to do because of that trailing ; and so max = motion; will always be executed so your output will be wrong.

    For it not showing it:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    You should also check to see if opening the file failed, if it did fail but you do nothing the loop will never end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  3. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM

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