Click here to Skip to main content
15,940,921 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can someone explain to me, or even show me how to write a complete function that swaps the values of "two specified array elements."
For instance,,
// Selection_Sort.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>

using namespace std;

void fill(int a[], int length)
	for (int i = 0; i < length; i++)
		cout << "Element #" << i+1 << endl;
		cin >> a[i];
	cout << '\n' << endl; 

void print(int a[], int length)
	cout << "A R R A Y Of 10 Elements Are As." << endl;
	for (int j = 0; j < length; j++)
		cout << a[j] << " | ";
	cout << '\n' << endl;

void swap(int &x, int &y)
	int temp;
	temp = a[x];
	a[x] = a[y];
	a[y] = temp;

void selectionSort(int a[], int length)
  int i, j;
  int min, temp;

  for (i = 0; i < length-1; i++)
    min = i;
    for (j = i+1; j < length; j++)
      if (a[j] < a[min])
        min = j;
	/*temp = a[i];
	a[i] = a[min];
    a[min] = temp;*/
	swap(a[i], a[min];

int main()
	const int length = 10;
	int label[length];
	fill(label, length);
	print(label, length);
	selectionSort(label, length);
	print(label, length);
	return 0;

Especially it is requested to respected OriginalGriff, Please eloborate. !!
Any Suggestions Will Be Appreciated . !!
Updated 10-Feb-13 4:44am
Sergey Alexandrovich Kryukov 10-Feb-13 9:20am    
What a gibberish! Nothing can help you unless you stop doing what you are doing and read the very basics on C++ and programming in general.
Sergey Alexandrovich Kryukov 10-Feb-13 9:25am    
First of all, stop posting fake "answers". This is the abuse which can only cause abuse reports. A number of members already lost their membership accounts because of that.
I seriously advise you to stop asking questions to this forum until you get at least most basic knowledge. Learning by asking question on this level is utterly ineffective.
Usman Hunjra 10-Feb-13 9:38am    
Actually Sir, I've basic knowledge in c++, as I'm a student of computer sciences i am already posted many questions and they are well responded. yes i am new in functions that why i need a little help from seniors like you as well.

1 solution

In general you're looking for something like (C++11):
template <class T> void swap (T& a, T& b)
  T c(std::move(a)); a=std::move(b); b=std::move(c);
template <class T, size_t N> void swap (T &a[N], T &b[N])
  for (size_t i = 0; i<N; ++i) swap (a[i],b[i]);

where T must be copy-constructible and assignable.

This is also what std::swap does for you.

Best regards
Espen Harlinn
Share this answer
Usman Hunjra 10-Feb-13 10:35am    
hmm .. Actually I've no knowledge about classes, templates. I will learn about classes in next semester.
Is there any way that i can use swap fuction without using template. ?! :/
Espen Harlinn 10-Feb-13 10:41am    
well, it's simple to use, like: swap(a[i], a[min]);

Personally I would use: std::swap(a[i], a[min]);

Or, if I fix your swap function - the arguments are references, so will refer to the elements in your array, and hence no need to pass the array:
void swap(int& x, int& y)
int temp = x;
x = y;
y = temp;

Espen Harlinn 10-Feb-13 10:53am    
>> why you use std
because using the functionality from the standard c++ library is usually considered the right thing to do.
>> So, in swap function what should i pass .. ?!
if you use the void swap(int& x, int& y) I implmented above, then:
swap(a[i], a[min]);
should do the job ...
Usman Hunjra 10-Feb-13 11:01am    
Thanks it w o r k s.. :)
Thank you for your consideration.
Because of this I've learned on more concept. . .
Espen Harlinn 10-Feb-13 12:16pm    
>> Because of this I've learned on more concept...
Briiliant :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900