Oct 13, 2015 at 11:37am Oct 13, 2015 at 11:37am UTC
hi i just have created an account and i really want you guys to help me with my assignment..
{
Q-A: Write a method to initialize the array below with the given values.
1 2 3 4
5 6 7 8
9 4 0 1
B: Write a method to interchange two columns, indexed by two int parameters.
}
i already have done with A but when i tried to solve B i got lost
i'd really appreciate it if you guys help me .. any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
using namespace std;
int row=5;
int col=4;
int main()
{
int array[row][col];
int i,j;
int num=10;
for (i=0; i<row; i++)
{
for (j=0; j<4; j++)
{
array[i][j]=num;
cout<<array[i][j]<<" " ;
num++;
}
cout << endl;
}
return 0;
}
Last edited on Oct 14, 2015 at 1:30pm Oct 14, 2015 at 1:30pm UTC
Oct 13, 2015 at 11:53am Oct 13, 2015 at 11:53am UTC
So, you might have to interchange columns 1 and 3. They are your 'two int parameters'
If you want to swap any 2 numbers, forgetting about a 2d array, how do you do it?
Once you know that you just run down the two columns and 'swap the a's and b's'.
Hint: you could do with a temp.
Last edited on Oct 13, 2015 at 11:53am Oct 13, 2015 at 11:53am UTC
Oct 13, 2015 at 12:43pm Oct 13, 2015 at 12:43pm UTC
You can make it a little easier to follow once you realise that temp doesn't need to be indexed. It can be a simple temp without [][] dimesions.
temp = array[i][c1];
array[i][c1] = array[i][c2];
array[i][c2] = temp;
Also, remember that the column numbers are fixed and only i is changing.
Oct 13, 2015 at 1:00pm Oct 13, 2015 at 1:00pm UTC
yes it worked thank you.. kemort :)