print array in function call

I want to print students name in ascending according to marks. i manage to do that but there is something wrong with my output. the name of students for test is not shown. i don't know which part is wrong and how to correct it. what should i do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//Global constant
const int NUM_STUDENTS=5;
const int NUM_SCORES=4;

//Function prototypes
void ascenDescen(string [],double [][NUM_SCORES],int,string []);

int main()
{
	int choice;
	string name[5]={"Hani","Haziq","Aiman","Farah","Sabrina"};
	string mark[4]={"Test","Quiz","Assignment","Final Exam"};
	double scores[NUM_STUDENTS][NUM_SCORES]={{9.0,2.7,16.0,78.0},				 
                                                 {7.4,2.7,19.0,88.0},
					         {8.9,3.5,17.5,93.7},										 
                                                 {10.0,3.0,19.5,64.8},										 
                                                 {6.3,3.0,16.0,74.2}};
	
	cout<<"\aThis program will help you keep track of your academic record!"<<endl;
	cout<<setprecision(1)<<fixed<<endl;
	
	//Function call
	ascenDescen(name,scores,NUM_STUDENTS,mark);
	
	cout<<"\nTHANK YOU."<<endl;
	
	return 0;
}

void ascenDescen (string NAME[],double table[][NUM_SCORES],int rows,string MARK[])
{
	cout<<"\nPress ENTER to sort the students based on their marks for all tests, quizzes, assignments "; 
	cout<<"and final exam in ASCENDING and DESCENDING order."<<endl; 
	cout<<"Enter E/e if you wish to skip."<<endl;
	
	char enter;
	cin.ignore();
	
	if(cin.get()=='\n')
	{
		double ascen;
		
		string temp1[5];
		temp1==NAME;
		string temp2;
		
		//for ascending
		cout<<"\tASCENDING ORDER"<<endl;
		for(int col=0;col<NUM_SCORES;col++)
		{
			for(int row=0;row<NUM_STUDENTS;row++)
			{
				for(int j=row+1;j<NUM_STUDENTS;++j)
			 	{
			 		if(table[row][col]>table[j][col])
                	{
                		ascen=table[row][col];
                		temp2=temp1[row];
                		table[row][col]=table[j][col];
                		temp1[row]=temp1[j]; 
                		table[j][col]=ascen;
                		temp1[j]=temp2;
                	}	
			 	}
			}
			cout<<MARK[col]<<" mark : \n";
    		for(int row=0;row<NUM_STUDENTS;row++)
    		{
        		cout<<"\t";
        		cout<<temp1[row]<<"\t"<<table[row][col];
        		cout<<endl;
    		}
    		for(int i=0;i<NUM_STUDENTS;i++) 
    		{
    			temp1[i]=NAME[i];
    		}
    	}
    		
    	
}
}


My output:
ASCENDING ORDER
Test mark :
6.3
7.4
8.9
9.0
10.0
Quiz mark :
Hani 2.7
Haziq 2.7
Farah 3.0
Sabrina 3.0
Aiman 3.5
Assignment mark :
Hani 16.0
Sabrina 16.0
Aiman 17.5
Haziq 19.0
Farah 19.5
Final Exam mark :
Farah 64.8
Sabrina 74.2
Hani 78.0
Haziq 88.0
Aiman 93.7
i don't know why names are not showing at the test part.
Hello murasaki98,

Here is a tip for you. This keeps the initialization more to the left of the screen and a little easier to visualize.
1
2
3
4
5
6
7
8
double scores[NUM_STUDENTS][NUM_SCORES]
{ 
    { 9.0, 2.7, 16.0, 78.0 },
    { 7.4, 2.7, 19.0, 88.0 },
    { 8.9, 3.5, 17.5, 93.7 },
    { 10.0, 3.0, 19.5, 64.8 },
    { 6.3, 3.0, 16.0, 74.2 }
};

In your function:
1
2
3
4
5
6
7
8
9
void ascenDescen(string NAME[], double table[][NUM_SCORES], int rows, string MARK[])
{
    cout <<
        "\nPress ENTER to sort the students based on their marks for all tests, quizzes, assignments\n"
        "and final exam in ASCENDING and DESCENDING order.\n"
        "Enter E/e if you wish to skip.: ";

    //char enter;  // <--- Never used.
    //cin.ignore(); 

I changed the "cout" statement so that it was shorter across the screen and did not wrap around in the middle of a word. You can also write this as 1 statement.

The first problem is: temp1==NAME;. "==" means to compare, but you are not doing anything with the returned value of the compare which makes the line mostly useless.

Did you mean: temp1 = NAME;.
which means assign, but you still have a problem. "temp1" and "NAME" are arrays and you can not use "=" to set 1 array equal to the other. What you should have is: temp1[0] = NAME[0];, but still this is not enough.

What I did is take the for loop at lines 78 - 81 and copy that then put it at line 52. I deleted lines 41 and 42. Line 41 is never used and line 42 is not needed. Line 42 causes you to press enter twice.

The beginning of the function looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void ascenDescen(string NAME[], double table[][NUM_SCORES], int rows, string MARK[])
{
    cout <<
        "\nPress ENTER to sort the students based on their marks for all tests, quizzes, assignments\n"
        "and final exam in ASCENDING and DESCENDING order.\n"
        "Enter E/e if you wish to skip.: ";

    if (cin.get() == '\n')
    {
        double ascen;

        string temp1[5];
        //temp1[0] = NAME[0];
        string temp2;

        for (int i = 0; i < NUM_STUDENTS; i++)
        {
            temp1[i] = NAME[i];
        }

        std::cout << '\n';
    
        //for ascending
        cout << "\tASCENDING ORDER" << endl;


And produced the output of:

This program will help you keep track of your academic record!


Press ENTER to sort the students based on their marks for all tests, quizzes, assignments
and final exam in ASCENDING and DESCENDING order.
Enter E/e if you wish to skip.:

        ASCENDING ORDER
Test mark :
        Sabrina 6.3
        Haziq   7.4
        Aiman   8.9
        Hani    9.0
        Farah   10.0
Quiz mark :
        Hani    2.7
        Haziq   2.7
        Farah   3.0
        Sabrina 3.0
        Aiman   3.5
Assignment mark :
        Hani    16.0
        Sabrina 16.0
        Aiman   17.5
        Haziq   19.0
        Farah   19.5
Final Exam mark :
        Farah   64.8
        Sabrina 74.2
        Hani    78.0
        Haziq   88.0
        Aiman   93.7

THANK YOU.



And:

This program will help you keep track of your academic record!


Press ENTER to sort the students based on their marks for all tests, quizzes, assignments
and final exam in ASCENDING and DESCENDING order.
Enter E/e if you wish to skip.: e

THANK YOU.


The first blank line is because of the way I used the "output" tag, but your output might look better if you put that blank line in your code.

Andy
Thank you Andy. Really appreciate your respond. I understand all your explanation. Thank you so much.
Topic archived. No new replies allowed.