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

Given three already declared int variables, i , j , and temp , write some code that swaps the values in i and j . Use temp to hold the value of i and then assign j 's value to i . The original value of i , which was saved in temp , can now be assigned to j .

My answer is:

temp = i;
j = i;

But it's not working? Weird???

2006-09-01 05:37:26 · 3 answers · asked by ? 1 in Computers & Internet Programming & Design

3 answers

You can't use "i" as a variable, it's a math function. Also, you can't use temp as a variable either I don't think. I think it's an arguement.

Also, have you tried declaring your variables:

int temprary=ivariable
ivariable=j
j=temporary

Looks like you also forgot to assign j's value with that of "i" (labled as ivariable here, since "i" is a math function)

Also, the method of switching with no temporary variable would work great.

2006-09-01 06:49:38 · answer #1 · answered by Rockstar 6 · 0 0

temp = i;
i=j;
j = temp;

You can just go thro' this like,
Take cup A as Full of Cofee
Take cup B as Full of Tee

You want to swap it, so use another Empty Cup called Temp. Thats all. Your problem solved.


Swapping Without temp variable:-

i = i+j
j = i-j;
i=i-j

2006-09-01 13:48:55 · answer #2 · answered by Sudha P 2 · 0 0

temp = i;
i=j;
j=temp;

2006-09-01 12:41:54 · answer #3 · answered by chharsha 3 · 2 0

fedest.com, questions and answers