C++ for beginners : Array's problems

This is the problems:

Write a C++ program that prompts the user with the following options:
Matrix String Quit

1. If the user selects “M” or “m” then call a user-defined function called double average (void) that will the following:

a. Prompt the user to enter three sets of five numbers (type float) and store the numbers in a 3x 3 array.

b. Find the transpose of the matrix and print the results in this function.

c. Find the sum of the original matrix and transposed matrix and print the original, transpose, and the sum matrices in this function. Note the output should be in three rows and three columns format with space in between each column as follows:

1 2 3
2 0 9
12 3 7

2. If the user selects “S” or “s” then call a user-defined function void rev_str (void) that

a. Prompts the user to enter a string and store it into one-dimensional array.
b. Replace the content of the string with the string reversed and save into another array.
c. Print both the original string and reversed one in this function.

• If the user enters “Q” or “q” then you should terminate the program. If the user enters any other selection it should prompts the user invalid selection and try again.

Note: Do not use any global arrays or variables.




This is my code. I got problem when I when I try to do the part 2b and 2c. The complier keeps showing errors. Can anyone teach me where did I do wrong?. Thank you very much.

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
  #include<iostream>
#include<iomanip>
#include<string>
using namespace std;
float average (void);
void rev_str (void);

int main()
{
	char code;	
	char ch;
	
do{
	cout<<"Enter M for Matrix, Enter S for String"<<endl;
	cout<<"Matrix	String	Quit"<<endl;
	cin>>code;

switch(code)
{
case 'M':
case 'm':
	{
	average();
	}
	break;
	
case 'S':
case 's':
	{
	rev_str();
	}
	break;
	
default:
	{
	cout<<"Invalid Input"<<endl;
	}
	break;

}
cout<<"Please Enter Q to quit"<<endl;
cin>>ch;
	}
	while(ch!='Q'&& ch!='q');
}
float average(void)
{
	float arr[3][3],sumarr[3][3];
	cout<<"Enter 3 sets of 3 numbers for the 3x3 array"<<endl;
	for(int r=0; r<3; r++)
	{
			for(int c=0; c<3; c++)
			{
				cin>>arr[r][c];
			}
	}
	for(int r=0; r<3; r++)
	{
			for(int c=0; c<3; c++)
			{
				cout<<arr[r][c]<<" ";
			}
			cout<<endl;
	}
	cout<<"The Transpose of the Matrix is following:"<<endl;
	for(int r=0; r<3; r++)
	{
		for(int c=0;c<3;c++)
		{
			cout<<arr[c][r]<<" ";
		}
		cout<<endl;
	}
	cout<<"The sum of Regular matrix and Transpose matrix is following:"<<endl;
	for(int r=0; r<3; r++)
	{
		for(int c=0; c<3;c++)
		{
			sumarr[r][c]=arr[r][c]+arr[c][r];
		}
	}
	for(int r=0; r<3; r++)
	{
			for(int c=0; c<3; c++)
			{
				cout<<setw(3)<<setprecision(2)<<sumarr[r][c]<<" ";
			}
			cout<<endl;
	}
	return 0;
}
void rev_str()
{


char regname, revname;
cout<<"Enter a string"<<endl;
getline(cin.regname);
for(int i=0; i<regname.length();i++)
{
	revname=regname[i]+revname;
}
cout<<"The original String is:"<<endl;
cout<<regname<<endl;
cout<<"The reverse of the string is:"<<endl;

cout<<revname<<endl;


}
Last edited on
1) regmane and revname should be string, not char.

2) Line 98: there should be comma, not dot.

3) Reverse string can be made several ways:
1
2
3
4
5
6
7
8
9
10
11
12
13
//1) Naive approach
revname.resize(regname.size());
for(int i = 0; int i < regname.size(); ++i)
    revname[revname.size() - i - 1] = regname[i];

//2) Using reverse algorithm
#include <algorithm>
//...
std::string revname = regname;
std::reverse(revname.begin(), revname.end());

//3) Constructing string using reverse iterators
std::string revname(regname.rbegin(), regname.rend());

Topic archived. No new replies allowed.