English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I need a function written in visual studio using c++ that interchanges two values of type int. Also i need to write a driver program to test my function.

2006-11-02 15:28:14 · 3 answers · asked by Shaina B 1 in Computers & Internet Programming & Design

3 answers

int valuechage(int a, int b, int c)
{
a = 1;
b = 3;

c = b;
a = b;
b = a;

return 0;
}

2006-11-02 15:45:19 · answer #1 · answered by D 4 · 0 0

Use the XOR (unique OR) operator: "In laptop programming, the XOR substitute is an set of rules which makes use of the XOR bitwise operation to alter distinctive values of variables having the comparable documents type with out utilising a non everlasting variable." It appears like what your instructor is calling for. The bitwise XOR operator (^) is an operator it is accessible in C. there is pattern C code interior the link below.

2016-10-21 04:32:54 · answer #2 · answered by Anonymous · 0 0

For interchanging values of two variables from within a function, you should be using either reference variables or pointer.
Using reference variable:-
/*---------------------------------*/
void change(int &a, int &b)
{
int c = a;
a = b;
b = c;
return;
}
int main()
{
int a = 10;
int b = 20;
cout << "Original" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
change(a,b);
cout << "Modified" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
}
/*---------------------------------*/

Using pointer:-
/*---------------------------------*/
void change(int *a, int *b)
{
int c = *a;
*a = *b;
*b = *c;
return;
}
int main()
{
int a = 10;
int b = 20;
cout << "Original" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
change(&a,&b);
cout << "Modified" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
}
/*---------------------------------*/

2006-11-02 17:55:38 · answer #3 · answered by mashiur1973 2 · 0 0

fedest.com, questions and answers