I'm having trouble writing a program. I have to determine the name of student
score of 5 modules. Hence display them in a table form where 1st column is the
modules and the 2nd column is the student's names.
where 1st column is the rank number, the 2nd column is the names of students and
the 3rd column is the average scores. I need to use arrays and pointer to solve
this problem.
This is how far i've managed to write the program without using pointers. I have difficulty in using pointers. Hope someone would help me out.
#include <stdio.h>
void sort( int [], int [], int );
main()
{
int numbers[25] = { 78, 61, 52, 85, 79, 67, 49, 75, 71, 85, 82, 70, 68, 50, 65, 55, 66, 79, 89, 77, 73, 75, 81, 74, 69};
int arrange[6] = {0, 1, 2, 3, 4, 5};
char names[5][30] = {"John", "Sam", "Clinton", "Harry", "Tom"};
int loop;
clrscr();
printf("Before the sort the array was \n\tMechanical Design Mathematics Programming Electronics Automation\n");
printf("John\t\t78\t\t61\t 52\t\t 85\t\t 79\n");
printf("Sam\t\t67\t\t49\t 75\t\t 71\t\t 85\n");
printf("Clinton\t\t82\t\t70\t 68\t\t 50\t\t 65\n");
printf("Harry\t\t55\t\t66\t 79\t\t 89\t\t 77\n");
printf("Tom\t\t73\t\t75\t 81\t\t 74\t\t 69\n");
sort( numbers, arrange, 3 );
printf("\nAfter the sort the array was\n");
printf("Modules\t\t\tName\t\t\Rank\tName\t\tAverage Score\n");
printf("=============================================================================\n");
printf("Mechanical Design\t%s\t\t1\t%s\t\t74.4",names[2],names[4]);
printf("\nMaths\t\t\t%s\t\t2\t%s\t\t73.2",names[4],names[3]);
printf("\nProgramming\t\t%s\t\t3\t%s\t\t71",names[4],names[0]);
printf("\nElectronics\t\t%s\t\t4\t%s\t\t69.4",names[3],names[1]);
printf("\nAutomation\t\t%s\t\t5\t%s\t\t67",names[1],names[2]);
printf("\nAverage\t\t\t%s",names[4]);
getch();
return 0;
}
void sort( int a[], int b[], int elements )
{
int i, j, temp, temp_b;
i = 0;
while( i < (elements - 1) ) {
j = i + 1;
while( j < elements ) {
if( a[i] > a[j] ) {
temp = a[i];
temp_b = b[i];
a[i] = a[j ];
b[i] = b[j];
a[j] = temp;
b[j] = temp_b;
}
j++;
}
i++;
}
}