Thread: 1 Question

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    1 Question

    Hi dears;

    i have written a code for c++ . this code recieve n and then recieve n student grades ,,, and finally it finds the average of grades !

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main(){
        int n,count=0,sum=0;
        float grade,avg;
        cout << "How many students grade do you want to input ? \n" <<endl;
        cin >> n;
        cout << endl;
        
        cout << "Please enter the student grade : \n"<<endl;
        while (count<n){
        cin >> grade;
        cout << endl;
        sum += grade;
        count++;
    }
      avg = (float)(sum)/(float)(n);
    
        cout << avg;
        getch();
        return 0;
    }
    but now i want to calculate the average of the "first (n-1) top grades ! "

    now my idea : finiding the min grade between inputed grades and sustract it from sum ... but i dont exactly know how can i find it between inputed grades !!! can u help me ?? tnx;
    Last edited by Seniorija; 11-17-2012 at 09:21 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Good idea

    I will make you an example and from this you will be inspired to modify your code ( you see I am fair, I am not stealing you all the fun )

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int min,n;
        cout << "Input number of elements" << endl;
        // Input validation!
        cin >> n;
        cout << endl;
        if( n <= 0 )
        {
            cout << "Non positive input.Exiting..." << endl;
            return -1;
        }
    
        int array[n];
    
        cin >> array[0];
        cout << endl;
        // we have only one element
        // now, so this is the min
        min = array[0];
    
        for(int i = 1 ; i < n ; i++)
        {
            cin >> array[i];
            cout << endl;
            // if the element we read
            // is smaller than min,
            // then assign to min
            // this element.
    
            if( array[i] < min )
               min = array[i];
        }
    
        cout << "min = " << min <<endl;
        return 0;
    }
    Hope this helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM
  5. A question of color...(not a racial question)
    By Sebastiani in forum Windows Programming
    Replies: 7
    Last Post: 01-15-2003, 08:05 PM