Results 1 to 6 of 6

Thread: C++ code

  1. #1
    Join Date
    Jul 2011
    Location
    Nairobi,Kenya
    Beans
    127
    Distro
    Ubuntu 12.04 Precise Pangolin

    C++ code

    Hello.

    I am trying to write a c++ program to compute the total, average , highest and lowest score of n students. The code below does almost all, except it gives a memory location of the highest value after sorting an array of marks instead of giving the value itself. Where have I gone wrong?

    Code:
    //Program to compute students total marks, highest score, lowest score  and the //average .
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int n (0);
    
    int main ()
    {
        cout << "Enter the number of students: \n";
        cin >> n;
        int i (0), mark [n], total (0), av (0);
    
        for (i=0; i < n; i++ ){
        cout << "Enter marks\n";
        cin >>  mark [i];
        total += mark[i];
        }
    
        av = total / n; // Find an average of all the marks.
    
        sort (mark, mark + n);
        cout <<"Lowest: " << mark[0] << ". Highest: " << mark[n] << endl;
        cout << "Total Marks: "<< total << endl;
        cout << "Average: " << av << endl;
        return 0;
    }
    Last edited by lisati; November 17th, 2012 at 09:15 AM. Reason: Normalise fonts; add code tags for readbility
    People resist progression in every facet of life, computers are no different. Inevitably though, progress passes them by, and they become like dinosaurs...

  2. #2
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: C++ code

    Thread moved to Programming Talk.
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  3. #3
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,274

    Re: C++ code

    You should print the value of mark[n-1], not mark[n].

    Also, variable length arrays are not standard C++.

    Also also, the average should probably be a float/ double.

  4. #4
    Join Date
    May 2010
    Location
    SE England
    Beans
    210

    Re: C++ code

    If this is a homework project then you really need to sort it out yourself but as a clue - what is the highest allowable index No. of the array mark ( bearing in mind indices start from 0)?

    Bouncingwilf

  5. #5
    Join Date
    Jul 2011
    Location
    Nairobi,Kenya
    Beans
    127
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C++ code

    Quote Originally Posted by GeneralZod View Post
    You should print the value of mark[n-1], not mark[n].

    Also, variable length arrays are not standard C++.

    Also also, the average should probably be a float/ double.

    That worked! I just wanted to get the idea and thanks for your help.
    People resist progression in every facet of life, computers are no different. Inevitably though, progress passes them by, and they become like dinosaurs...

  6. #6
    Join Date
    Jul 2011
    Location
    Nairobi,Kenya
    Beans
    127
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C++ code

    Quote Originally Posted by bouncingwilf View Post
    If this is a homework project then you really need to sort it out yourself but as a clue - what is the highest allowable index No. of the array mark ( bearing in mind indices start from 0)?

    Bouncingwilf
    n-1
    People resist progression in every facet of life, computers are no different. Inevitably though, progress passes them by, and they become like dinosaurs...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •