program arrange names of students and there grades

Hello,
I want to write a program thats Arranges names of students and there grades,
the arrange of the 1st array (names) will be arranges by Alphabetical arrangement (a - b - c -....- z).
and if we arrange the names, the grades must be the same for the student
mean
name grade 1 grade 2
z 55 24
a 47 80

after arrange will be:

name grade 1 grade 2
a 47 80
z 55 24


I hope you can help me...
this is my try, but there is wrongs

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

#include<iostream.h>

#include<string.h>
const int x=2;
void main()
{

 char names[x][10];
 int grades[x][3];
 for(int i=0; i<x; i++)
		{
		 cout<<"Enter name of student "<<i+1<<" :";
		 cin>>names[i];
			for(int j=0;j<3;j++)
				  {
				  cout<<"Enter grade "<<j+1<<" for student "<<i+1<<" : ";
				  cin>>grades[i][j];
				  }

		 }

 cout<<"\nName\tGrade1\tGrade2\tGrade3\tAvarage\n";
  cout<<"___\t______\t______\t______\t_______\n";
	 int sum=0;
	for(i=0; i<x; i++)
		{
		sum=0;
		 cout<<names[i]<<"\t";
			for(int j=0;j<3;j++)
				  {
				  cout<<grades[i][j]<<"\t";
				  sum=sum+grades[i][j];
				  }
			  cout<<sum/3<<"\n";
		 }

		 cout<<"Do you want to arrange  the names?(y/n)";
		 char ans;
		 cin>>ans;
		 if(ans=='y' || ans=='Y')
		 {

		  for(i=0; i<x; i++)
		{

			for(int j=i;j<3;j++)
				  {
					 if(strcmp(names[i],names[j])>0)
						  {
							 char temp[10];
							 strcpy(temp,names[j]);
							 strcpy(names[j],names[i]);
							 strcpy(names[i],temp);
							 int temp2[3][x];
							 for(int r=0;r<3;r++)
									{
									  strcpy(temp2[r][i],grades[j-1][i]);
									  strcpy(grades[r][j-1],grades[i][j]);
									  strcpy(grades[r][j],temp2[r][i]);


									}



						  }

				  }

		 }


		 //print after arragment//

       cout<<"\nName\tGrade1\tGrade2\tGrade3\tAvarage\n";
  cout<<"___\t______\t______\t______\t_______\n";
	 int sum=0;
	for(i=0; i<x; i++)
		{
		sum=0;
		 cout<<names[i]<<"\t";
			for(int j=0;j<3;j++)
				  {
				  cout<<grades[i][j]<<"\t";
				  sum=sum+grades[i][j];
				  }
			  cout<<sum/3<<"\n";
		 }




		 }

}







 


OK, as I understand the problem you have a list od students and associated with each student is a set of grades.
You want to sort the list of students alphbetically and ensure that the grades stay with the right students.
One way to do this is to hold the data in a structure
1
2
3
4
5
struct studentData
{
    char name[10];
    inr grades[3];
}

then create an array of the structures
 
studentData students[x];

You can read more on strucs on http://www.cplusplus.com/doc/tutorial/structures.html
This removes the complication of trying to keep multiple arrays in synch.
I would also advise the use of suitable functions to break you code into small managable chunks. A good example from your current problem would be a 'Swap' function that takes swaps two entries.
It makes the code easier to read, and also makes testign and debugging simpler - you can create a temporary piece of code that creates two entries, printes them, swaps and prints again, for example. Once this works, you know the swap function works and can just use it in future.
Hope this helps.
Hello,
really, thank you from my heart for your helping.
I'm sorry, because i didn't know how can i use this structure, because i didn't learn it in university till now.
so can you help me, by how can i use it??
means to rewrite the program.

this is my try:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream.h>

#include<string.h>
const int x=2;
struct studentData
 {


 char names[x][10];
 int grades[x][3];

 }

void main()
{



 studentData students[x];

 for(int i=0; i<x; i++)
		{
		 cout<<"Enter name of student "<<(i+1)<<" :";
		 cin>>names[i];
			for(int j=0;j<3;j++)
				  {
				  cout<<"Enter grade "<<(j+1)<<" for student "<<(i+1)<<" : ";
				  cin>>grades[i][j];
				  }

		 }

 cout<<"\nName\tGrade1\tGrade2\tGrade3\tAvarage\n";
  cout<<"___\t______\t______\t______\t_______\n";
	 int sum=0;
	for(i=0; i<x; i++)
		{
		sum=0;
		 cout<<names[i]<<"\t";
			for(int j=0;j<3;j++)
				  {
				  cout<<grades[i][j]<<"\t";
				  sum=sum+grades[i][j];
				  }
			  cout<<sum/3<<"\n";
		 }

		 cout<<"Do you want to arragment ahe names?(y/n)";
		 char ans;
		 cin>>ans;
		 if(ans=='y' || ans=='Y')
		 {

		  for(i=0; i<x; i++)
				{

					for(int j=i;j<3;j++)
					  {
						 if(strcmp(names[i],names[j])>0)
							{
							  char temp[10];
							 strcpy(temp,names[j]);
							 strcpy(names[j],names[i]);
							 strcpy(names[i],temp);


							 }










						  }

				  }

		 }


		 //print after arragment//

       cout<<"\nName\tGrade1\tGrade2\tGrade3\tAvarage\n";
  cout<<"___\t______\t______\t______\t_______\n";
	 int sum=0;
	for(i=0; i<x; i++)
		{
		sum=0;
		 cout<<names[i]<<"\t";
			for(int j=0;j<3;j++)
				  {
				  cout<<grades[i][j]<<"\t";
				  sum=sum+grades[i][j];
				  }
			  cout<<sum/3<<"\n";
		 }





}



OK. The Structure is for just one Student, so the arrays shoulf be one dimensional.
1
2
3
4
5
6
7
struct studentData
{
    char name[10];
    int grades[3];
}

studentData students[x];

You then use the '.' notation to access the parts of the structure.
I woudl also decalre a constant for the number of grades, just to make the code more obvious (anmd easy to change if you wanted to later make it 5 grades)
So
1
2
3
4
5
6
7
8
9
10
11
const int NUM_GRADES = 3;
for(int i=0; i<x; i++)
{
    cout<<"Enter name of student "<<(i+1)<<" :";
    cin>>strudent[i].names;
    for(int j=0;j<NUM_GRADES;j++)
    {
        cout<<"Enter grade "<<(j+1)<<" for student "<<(i+1)<<" : ";
        cin>>strudent[i].grades[j];
    }
}

and similary for the rest of the code.
Thank you for helping

this is my try
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
#include<iostream.h>
#include<string.h>
void main()

{

const int x = 3;
const int y= 3;


  struct studentData
{
	 char names[x][7];
	 int grades[y][3];
}
 char temp[x][10];
studentData students[x];


for(int i=0; i<x; i++)
{
	 cout<<"Enter name of student "<<(i+1)<<" :";
	 cin>>students[i].names[i];

	 for(int j=0;j<y;j++)
    {
        cout<<"Enter grade "<<(j+1)<<" for student "<<(i+1)<<" : ";
		  cin>>strudents[i].grades[j];
    }
}

for(i=0;i<x;i++)
	 {
	  cout<<students[i]<<endl;
	 }

	for(i=0;i<x;i++)
		  {
			for(j=i+1;j<y;j++)
				 { if (strcmp(students.names[i],students.names[j])>0)
								{
								 strcpy(temp[i],students.names[i]);
								 strcpy(students.names[i],students.names[j]);
								 strcpy(students.names[j],temp[i]);

								 }

		 }


   for(i=0;i<x;i++)
	 {
	  cout<<students[i]<<endl;
	 }
}



and i will ask my doctor in university somethings about structure
Hi,
you still have the array in the structure as two dimensional which you do not want.
1
2
3
4
5
struct studentData
{
	 char names[x][7];
	 int grades[y][3];
}

You also do not need temp to be two dimensional - you are only using it to hold one name at any point in time, so you can re-use it.
I would also use meaningful names for your constants and variables, rather than x, y, etc.
So you might have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int x NUMBER_OF_STUDENTS= 3;
const int y NUMBER_OF_GRADES = 3;

for(int currentStudent=0; currentStudent< NUMBER_OF_STUDENTS; currentStudent++)
{
    cout<<"Enter name of student "<<(currentStudent+1)<<" :";
    cin>>students[currentStudent].names;
    for(int currentGrade=0; currentGrade<NUMBER_OF_GRADES; currentGrade++)
    {
        cout<<"Enter grade "<<(currentGrade+1)<<" for student "<<(currentStudent+1)<<" : ";
        cin>>strudents[currentStudent].grades[currentGrade];
    }
}

etc.
Topic archived. No new replies allowed.