Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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);
	system("pause");
	return 0;
}
 
Especially it is requested to respected OriginalGriff, Please eloborate. !!
Any Suggestions Will Be Appreciated . !!
Posted 10-Feb-13 1:22am
ctype.h1.7K
Edited 10-Feb-13 4:44am
v7
Comments
Sergey Alexandrovich Kryukov at 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.
—SA
Sergey Alexandrovich Kryukov at 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.
—SA
Usman Hunjra at 10-Feb-13 9:38am
   
I'M SORRY SIR .. !!
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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Usman Hunjra at 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 at 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 at 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 at 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 at 10-Feb-13 12:16pm
   
>> Because of this I've learned on more concept...
Briiliant :-)
Abhinav S at 10-Feb-13 11:30am
   
5. Neat.
Espen Harlinn at 10-Feb-13 12:16pm
   
Thank you, Abhinav :-D

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



Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100