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;
	}
	}
}

Recommended Answers

All 5 Replies

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

What is i and j?
Should it not be x and y?

Seems to me you are feeling like a blind man who is trying to hit an egg with a stick.(Poor translation of a local proverb)
Have you read http://www.daniweb.com/code/snippet979.html ? The code is in C# but it should not be to difficult to understand it!
Have you read : http://en.wikipedia.org/wiki/Sorting_algorithm ? Here you will find info on sorting and some ways to code the things you want.
Happy programming!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.