Matrix Column Swapping

Hi there, I'm curretnly stuck on a menu program. I'm supposed to create a 5x5 array of char. One of the menu options is to switch the first and the fourth column of the matrix. I can go ahead and post what I have. Any hints would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>

using namespace std;

const int ROW= 5;
const int COL= 6;

/////////////////////////
///////             /////
//////  Functions   /////
////////////////////////

//Function will display menu to user
void display_menu()
{
        cout<< "1. Interchange column 1 with column 4\n" << "2. Display the total 
        number of vowels\n" << "3. Display the array in a matrix (rows and 
        columns)\n" <<"4. Search for and display number of instances of any given 
        character\n"<< "5. Exit\n" <<endl;
}

//int function which gets and returns user selection

int get_user_choice()
{
        //will hold the user's choice
        int user_choice = 0;
    
        //reading in useer choice
        cin >> user_choice;
    
        return user_choice;
}

//Function will swap first and fourth column of matrix
 void swap_column(char arr[ROW][COL])
 {
         for(size_t i = 0; i < ROW; i++)
         {
             //temporary arry will hold column value of index 0
             char temp_array = arr[i][0];
             //index 0 for columns will hold what is in index 3
             arr[i][0] = arr[i][3];
             //element #3 in columns will hold value of temp_array
             arr[i][3] = temp_array;
         
         
         }
     cout<< "Swapped"<< endl;
 }
 
 //fuction should print out number of vowels

//Function should display an 2d array of char
void print_matrix(char arr[ROW][COL])
{
        //looping through row of matrix
        for(size_t i = 0; i < ROW; i ++)
        {
            //Looping through colms of matrix
            for(size_t j = 0; j < COL; j++)
            {
                //Printing out rows and columns
                cout<< arr[i][j];
            }
        cout<< endl;
    }
}




//////////////////////
////               ///
////    Main      ///
////             ///
///////////////////
int main()
{
    
    
    int user_choice= 0;
    
    do{
    
            display_menu();
        
            //Initializing a 2d array of char
            char my_array [5][6] = {"sweet","heart","egrit","clone","odors"};
            //Displaying Menu
            user_choice = get_user_choice();
    
    
            switch(user_choice)
           {
                case 1:
                    swap_column(my_array);
                    break;
                case 2:
                    
                    break;
                case 3:
                    print_matrix(my_array); 
                    break;
                case 4:
                    // search 
                    break;
                case 5:
                    //Exit
                break;
                default:
                    //nope nope nope
                    break;
        }
    }while(user_choice != 5);
    
    
    
    

    return 0;
} 


Last edited on
Your procedure swap_column already swaps first and fourth columns. So what is the problem?

Note that you are creating a 1-d array of c-strings (null-terminated strings), not strictly a 5x5 array of chars. If you want to continue like that you should simplify your print_matrix routine.
Last edited on
The output doesn't change whenever I select to change columns 1 and 4 of the matrix.
You keep reinitialising your array to the original values!

Set my_array BEFORE the do loop, not inside it.
Worked, thank you :))
Last edited on
Topic archived. No new replies allowed.