How to drop 2 lowest numbers (grades) in a C program

This is a discussion on How to drop 2 lowest numbers (grades) in a C program within the C Programming forums, part of the General Programming Boards category; I am in the middle of writing a program with 2D- arrays. The assignment is calculating the average for 18 ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    How to drop 2 lowest numbers (grades) in a C program

    I am in the middle of writing a program with 2D- arrays. The assignment is calculating the average for 18 students based on their 10 quiz grades. The two lowest grades should be dropped. I am currently only able to drop the lowest grade but not the second lowest. I would copy and paste what i have but school computers are locked from personal use. How would a go about dropping the second lowest grade?

    thanks in advance.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    I would copy and paste what i have but school computers are locked from personal use.
    This is the strictest school I've ever heard of, since no one ever stopped me from bringing a USB key with my homework on it, or something.

    Anyway, you could sort based on grade and drop N - 2 and N - 1. If you really must delete them rather than simply ignore them, then you'll have to reallocate the 2d array, since arrays are a fixed size.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    I'm currently off campus and my computer will not recognize the way i have my program saved on my usb. It doesn't matter if i delete or simply ignore them. I understand your suggestion but my professor hasn't covered bubble sorting for 2d arrays...only 1d.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You alredy found and dropped the lowest grade once...just do it again.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Well you could actually sort a 2d array, but if that's too difficult to figure out--

    make an array with positions in them first:
    Code:
     int positions[N] = {0, 1, 2, 3, ... N - 1 };
    This is what you're going to sort. In other words, you need to arrange it such that position[N - 1] for example will give you the index (by column or by row depending on the arrangement) of the lowest grade, and you ignore that one. To sort positions you only need to change how the comparison and swap is written. It depends on which dimension grades will be in, but you would compare:

    Code:
     if ( students[x][ positions[y] ] > students[x][ positions[ y+1 ] ] )
    And then you don't swap all of students[x] and students[ x+1 ] but positions[y] and positions[ y+1 ].

    And then you use the positions array to find out which grades to ignore because it will give you the appropriate index in the unsorted students 2d array.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    this is what i have for the first drop.

    Code:
    for (r= 0; r<ROWS; r=r+!){
    
    sum= 0;
    
    small=10;
    
    for(c=1; c<COLS; c= c+1){
    
    sum = sum + q[r][c];
    
    if (q[r][c] < small){
    
    small = q[r][c];
    
    }
    
    }
    
    avg = (sum-small)/ (COLS-1);
    
    }
    
    }

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    if (q[r][c] < small){
    
    small = q[r][c];
    
    }
    else if(q[r][c] < small2){
    
    small2 = q[r][c];
    
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drop lowest score?
    By unknownnoob in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2010, 02:27 PM
  2. Replies: 22
    Last Post: 11-13-2009, 04:51 PM
  3. Lowest Score Drop - C++
    By getName(C-Dub) in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2008, 06:02 PM
  4. Lowest Score Drop
    By naya22 in forum C++ Programming
    Replies: 16
    Last Post: 04-29-2007, 12:48 AM
  5. Drop Lowest Grade then Average
    By cmut in forum C Programming
    Replies: 4
    Last Post: 09-27-2002, 10:19 PM

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