Program in C need help with sorting by groups void sort_data(int *,double *,char
ID: 3824510 • Letter: P
Question
Program in C
need help with sorting by groups
void sort_data(int *,double *,char *, int):
int * - Integer pointer holding all the valid account IDs
double * - Double pointer holding all the corresponding valid amounts
char * - Character pointer holding all the corresponding valid designations
int – count of the number of valid records (remember this cannot be 13 anymore)
Now, you need to group them based on the designations, the first group must have the Juniors (J) and then the second group must have the Seniors (S) and the last group must be the Masters(M). Remember you are not sorting anything in ascending or descending order, you are group sorting them. When you swap the positions of designations while grouping, make sure the account IDs and the amounts match too after the grouping.
Explanation / Answer
Code:-
void sort_data(int *id,double *amounts,char *designations, int count){
if(count <= 13){
int i,j;
int check=-1;
char temp_d;
int temp_id;
double temp_amount;
for(i=0;i<2;i++){
for(j=0;j<count;j++){
if(i==0){
if(*(designations+j)=='J'){
temp_d=*(designations+j);
temp_id=*(id+j);
temp_amount=*(amounts+j);
check++;
}
}
if(i==1){
if(*(designations+j)=='S'){
temp_d=*(designations+j);
temp_id=*(id+j);
temp_amount=*(amounts+j);
check++;
}
}
*(designations+j)=*(designations+check);
*(designations+check)=temp_d;
*(amounts+j)=*(amounts+check);
*(amounts+check)=temp_amount;
*(id+j)=*(id+check);
*(id+check)=temp_id;
}
}
}
else{
printf("NO OF RECORDS CAN'T BE MORE THAN 13 ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.