Click here to Skip to main content
15,400,029 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Let’s say there’s a class of 30 students, and we want to store their grades for each of their courses (six courses in total).

All we need to do is to keep the first column(i.e., Column 0), fixed for students, and the rest of the columns to store grades. We will have to follow a specific order to make sure the scores and students don’t get mixed up. Each element of the first column will hold a reference to an array containing marks of the student. So, there will be 30 rows and 6 columns. Now we can calculate the student’s percentage, class average, and the student’s position.


Here I couldn't find the average of students and their respective position.

I need the updated code, I'm not able to solve any further!!

What I have tried:

#include <iostream>
using namespace std;
float Percent(float []);

int main()
{
    float marks[10][6]={0};
    int x;
    cout<<"Enter the number of students : ";
    cin>>x;
    cout<<"Enter the marks of students: \n";
    for(int i=0;i<x;i++)
    {
        cout<<"Enter the marks obtained by student "<<i+1<<" :"<<endl;
        cout<<"\tSub-1 = ";
        cin>>marks[i][0];
        
        cout<<"\tSub-2 = ";
        cin>>marks[i][1];
        
        cout<<"\tSubj-3 = ";
        cin>>marks[i][2];
        
        cout<<"\tSubj-4 = ";
        cin>>marks[i][3];
        
        cout<<"\tSubj-5 = ";
        cin>>marks[i][4];
        
        cout<<"\tSubj-6 = ";
        cin>>marks[i][5];
    }
    cout<<"-----------------------------------------------------------";
    cout<<"\n St.No.   Sub-1   Sub-2   Sub-3   Sub-4   Sub-5   Sub-6  ";
    cout<<"Percentage\n"<<endl;
    for(int j=0;j<x;j++)
    {
        cout<<"\t"<<j+1;
        cout<<"\t\t"<<marks[j][0];
        cout<<"\t\t"<<marks[j][1];
        cout<<"\t\t"<<marks[j][2];
        cout<<"\t\t"<<marks[j][3];
        cout<<"\t\t"<<marks[j][4];
        cout<<"\t\t"<<marks[j][5];
        cout<<"\t\t"<<Percent(marks[j]);
        cout<<"\t\t"<<endl;
    }
    cout<<"\n";
    
    return 0;
}
float Percent(float marks[])
{
    float sum=0;
    for(int count=0;count<6;count++)
    sum+=marks[count];
    return sum/6;
}
Posted
Updated 16-Sep-21 4:25am
v4
Comments
Richard MacCutchan 16-Sep-21 9:09am
   
You are not storing any of your values so it makes it harder to calculate the results you want. A better idea would be to create Student class and add each student object to a 1D array. That way you can travers the array to find the averages, [positions and class average.
Rick York 16-Sep-21 11:40am
   
Have you noticed the fact you have the value 6 hard-coded quite a few places? What would happen if your teacher decided you need to check 8 values? The answer is NOT change all 6s to 8s. The answer is to make the number of items a constant value and define it in ONE place and pass that as a parameter to functions like Percent and everywhere input and/or output are done should be loops.

In addition to Solution 1 and following up on Richard's comments.

You need to store the Percentage that you have calculated. You could just add another column to the array but to do that would be ignoring everything that C++ is capable of.

Create a class to store each student and the marks for each subject - and move that function Percent into the class as a method on the members of the class. You have some choices on how you define the class e.g.
C++
class student {
    public:
        float studentID;
        float Subj1;
        float Subj2;
        // etc
        float Average;
}
or
C++
class student {
    public:
        int studentID;
        float marks[6];
        float Average;
}
You might decide to not have Average as a property of the class but you are going to want to use that value a few times, so consider: do you really want to calculate it each time? All of these things are up to you and these decisions are what you will be graded on for your assignment

Once you have your class, you can declare an array of that class. Or a list.

Then, your input loop should create a new object from the class each time and assign it an id (hint: your loop counter i will do as the id).

You can have an inner loop that captures input from the user for each of the 6 subjects.

Once you have all the marks for a single student, calculate the Percentage by calling the method on that object.

Once you have done that for all students, sum the average across all students and divide by the number of students to get the class average. Store that in a variable.

Finally sort the array into descending order. The index of the sorted array is that student's "position" in the class
   
Quote:
Let’s say there’s a class of 30 students

I fear you don't have enough storage
float marks[10][6]={0};

Quote:
Here I couldn't find the average of students

The debugger is the tool to see what is going on in your code.
Quote:
Here I couldn't find the average of students and their respective position.

I fear you need to save student respective percentage before sorting the data.
I fear you also need to store an id for each student in order to tell which student is at each position.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

Debugging in Excel VBA - EASY Excel Macros[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

PS: Advice: Feel free to use spaces, it makes your code easier to read.
C++
float Percent(float marks[])
{
    float sum= 0;
    for(int count= 0; count < 6; count++)
        sum += marks[count];
    return sum/6;
}
   
v3
Comments
CPallini 16-Sep-21 9:37am
   
5.
Patrice T 16-Sep-21 10:45am
   
Thank you
hlw ankit sarkar
bro can you plz send the final complete code???
   
Comments
OriginalGriff 19-Sep-21 15:13pm
   
Reason form my vote of one: nobody is going to do your homework for you, even for an assignment as trivial as the one you are trying to avoid doing yourself.

If you don't do it yourself, you won't learn how to - and that means the next task will be harder still, and so on - until you fail the course because nobody will do your exam for you ...

Give it try: this isn't complex problem at all!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900