i have added a function to make selection sort to my code but i can't adjust it. i need that someone try the code and try to adjust it so can someone help me to adjust it plzzzzzzz....and try to help me in sorting in an ascending order by using the id_number thx in advance
my code is:

#include<iostream>

using namespace std;

struct student
{
int id_number;
int age;
int grade;

};
int z=1;
void input_data(struct student*data_ptr);
void output_data(struct student*data_ptr);
void selectionSort(int onestudent[] , int listLength);


void main()
{
	cout << "number of students  ";
    cin>>z;
	
   student *onestudent ;
  
   onestudent = new student[z] ;
   for ( int x = 0 ; x  <= z-1 ; x++)
       input_data(onestudent+i);
   for ( int y = 0 ; y  <= z-1 ; y++)
       output_data(onestudent+j);
   void selectionSort(int onestudent[], int x);

}

void input_data(struct student*data_ptr)
{
int k;


cout << "enter id_number :";
cin >> k;
data_ptr->id_number = k;
cout << "enter age :";
cin >> k;
data_ptr->age = k;
cout << "enter grade:";
cin >> k;
data_ptr->salary = k;
}


void output_data(struct student*data_ptr)
{
	
cout << "id_num = " << data_ptr->id_number<<endl;
cout << "age = " << data_ptr->age <<endl ;
cout << "grade = " << data_ptr->grade <<endl;
	
}

selectionSort(student onestudent[], int z)
 {
	for (int i = 0 ; i  <= z-1 ; i++)
	{
		int smallestIndex;
		int index;
		int minIndex;
		int temp;

		smallestIndex = index;
		for (minIndex = index;minIndex < z;minIndex++)
			if (z[minIndex].id_number < z[smallestIndex].id_number)
				smallestIndex = minIndex;
			temp = z[smallestIndex].id_number;
		z[smallestIndex].id_number = z[index].id_number;
	}
	}
}

Selectionsort looks more like a bubblesort to me. The swap? in your if-statement should be in braces{} I would declare my variables out of the for-loop. What is temp doing?

i have changed the code and here is the new one......... can anyone adjust it to me plzzzzzzzzz

#include<iostream>

using namespace std;

struct student
{
int id_number;
int age;
int grade;

};
int z=1;
void input_data(struct student*data_ptr);
void output_data(struct student*data_ptr);
void selectionSort(int onestudent[] , int listLength);


void main()
{
	cout << "number of students  ";
    cin>>z;
	
   student *onestudent ;
  
   onestudent = new student[z] ;
   for ( int x = 0 ; x  <= z-1 ; x++)
       input_data(onestudent+i);
   for ( int y = 0 ; y  <= z-1 ; y++)
       output_data(onestudent+j);
   void selectionSort(int onestudent[], int x);

}

void input_data(struct student*data_ptr)
{
int k;


cout << "enter id_number :";
cin >> k;
data_ptr->id_number = k;
cout << "enter age :";
cin >> k;
data_ptr->age = k;
cout << "enter grade:";
cin >> k;
data_ptr->salary = k;
}


void output_data(struct student*data_ptr)
{
	
cout << "id_num = " << data_ptr->id_number<<endl;
cout << "age = " << data_ptr->age <<endl ;
cout << "grade = " << data_ptr->grade <<endl;
	
}

selectionSort(student onestudent[], int z)
 {
	for (int i = 0 ; i  <= z-1 ; i++)
	{	if(strcmp(onestudent[x].id_number,onestudent[x+1].id_number)> 0)
{
strcpy(temp,onestudent[x]);
strcpy(onestudent[x],onestudent[x+1]);
strcpy(onestudent[x+1],temp);
} 
}
}

plzzzzzzzzzzzzzzz adjust the code for me

Firstly NEVER use void main() always int main() (of some variation).

Secondly do you know how functions work?

void selectionSort(int onestudent[], int x);

What is that doing inside your main method?

void selectionSort(student onestudent[] , int listLength);

void selectionSort(student onestudent[], int listLength){
  for (int i = 0 ; i  <= listLength-1 ; i++){
         if(strcmp(onestudent[x].id_number,onestudent[x+1].id_number)> 0){
                 strcpy(temp,onestudent[x]);
                 strcpy(onestudent[x],onestudent[x+1]);
                 strcpy(onestudent[x+1],temp);
         } 
  }
}

Note i also changed the function declaration to student rather than int & variable names changed so they actually matched!

And i used [code=cplusplus] [/code] so formatting is kept. I also re-indented the code for you so it is more re-adble to others, so they can look at you so called selection sort.

Chris

This article has been dead for over six months. Start a new discussion instead.