Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,095 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,629 people online right now. Registration is fast and FREE... Join Now!




Creating function to swap the right diagonal and a row in 2D array

 
Reply to this topicStart new topic

Creating function to swap the right diagonal and a row in 2D array

Hector6008
14 Oct, 2007 - 11:05 AM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 25


My Contributions
CODE
#include<iostream>
using namespace std;
#define rows 100
#define cols 100
void Read(int m[][cols])
{
    for (int r=0;r<rows;r++)
    {
        for(int c=0;c<cols;c++)
            cin>>m[r][c];
    }
}
void Swap(int y[][cols],char d,int f)
{
    int temp;
    if(char d=='l')
    {
        for(int r=0;r<rows;r++)
        {
            for(int c=0;c<cols<c++)
            {
                if(r==c)
                {
                    temp=y[r][c];
                    y[r][c]=y[f][c];
                    y[f][c]=temp;
                }
            }
        }
    }
    if(char d=='r') //  r is equivalent to right diagonal
    {  
            for ( int c=0;c<cols<c++)
                 {
                    for (int r=0;r<rows;r++)
                  ??????????????????????????


    
:blink:
void main()
{
    int x[rows][cols];
    int y[rows][cols];
    int f;
    char d;
    Read(x);
    Read(y);
    cout<<"Choose a row \n";
    cin>>f;
    cout<<"Choose diagonal l or r \n";
    cin>>d;
    Swap()
}


The user chooses a row and a diagonal from the first matrix to be swapped in the second matrix. I did the part of the left diagonal, but i can't swap the right diagonal with the row
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Creating Function To Swap The Right Diagonal And A Row In 2D Array
14 Oct, 2007 - 11:47 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
First off, you can seriously reduce the computation time for your program. For the first section of the function, since you are only making the swap when r==c, you can just use a single loop:
CODE

if (d=='l') {
        for (int r=0; r<rows; r++) {
            temp=y[r][r];
            y[r][r]=y[f][r];
            y[f][r]=temp;
        }
}

since the cases where r!=c are just a waste of cpu time with the looping.

For swapping the right diagonal, you want to swap the element in (rows-r-1, c) with the element in (f,c). The extra -1 in there reflects the fact that your array of n elements has a maximum index of n-1. Therefore, using the same loop-collapsing optimization as above, you would need:
CODE

if (d=='r') {
    for (int c=0; c<cols; c++) {
        temp=y[cols-c-1][c];
        y[cols-c-1][c]=y[f][c];
        y[f][c]=temp;
    }
}


And you should probably consider loading the matrix from a file or generate it automatically for your tests, since entering 200 elements by hand each time you want to test your code would be a bit of a tedious process.

Hope that helps,

-jjh
User is offlineProfile CardPM
+Quote Post

Hector6008
RE: Creating Function To Swap The Right Diagonal And A Row In 2D Array
15 Oct, 2007 - 12:55 AM
Post #3

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 25


My Contributions

icon_up.gif Thank you very much.

This post has been edited by Hector6008: 15 Oct, 2007 - 12:59 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month