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.
#include <iostream>
usingnamespace std;
constint ROW= 5;
constint 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;
}
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.