I'm trying to solve some problems in c ++ (I think it's important to let me know that I'm still a beginner and I don't understand the language very well). my doubt is the following: Because when I run the code in the case of question A for what reason the variable: why_f gives a random number, if possible someone could explain to me how I should fix it?
obs; this code is to determine which roads leave and certain roads reach city x
and which city has the highest number of studies
proposed exercise:
Consider cities numbered 0 through π - 1 that are connected by a series of one-way roads. The connections between cities are represented by the elements of a square matrix πΏπ π₯ π, elements elements πππ assuming the value 1 or 0, depending on whether or not there is a direct road that leaves city i and reaches city π. Thus, the elements of row π indicate the roads that leave the city π, and the elements of the column π indicate the roads that arrive in the city π. πΏ = [1 1 0 1 1 0 1 0 1 0 0 0 1 1 1 1] Create a program that, given a square matrix πΏπ π₯ π that represents a presented structure, has a function that proposes a solution or problem in each of the items below. Your program must have a menu in a loop for the user to choose which problem to solve or when they want to exit it, it must allow the user to enter the desired matrix once and, for each item, the user must enter new specific entries make trouble. Explain your idea for solving each of the proposed problems. NOTE: The number of columns can be corrected by you in the code, but it should allow you to change it easily, through a variable.
a) Given a user-defined city ka, determine the roads according to which roads reach the city.
b) Given the input matrix, which city has the highest number of roads that leave it?
int main ()
{
setlocale(LC_ALL, "English");
int opera_sum_or_subtra, m, i, j, columns2, rows2, city, path, counter;
int by_d, by_f, cities, counter_2;
int l [size] [size] = {1,1,1,0,
0.1,1.0,
1.0,1.1,
0.0,1.1};
//
cout << "\ nchoose whether you want to answer question A or question B \ if you want to type it: 1 \ if you wantBa type: 2 \ n \ n";
cin >> opera_sum_or_subtra;
if (opera_sum_or_subtra == 1)
{
/// Question A
cout << "Enter the city of departure (remember that the maximum number is 4 and the minimum is 0" << endl;
cin >> city;
while (city <0 || city> 3) {
cout << "\ nVish will give no, as this city is not in the system :) \ n" << endl;
cout << "Enter the city of departure (remember that the maximum number is 4 and the minimum is 0" << endl;
cin >> city;
}
for (i = 0; i < size; i = i + 1)
if (city! = i)
{
if (l [i] [city] == 1) {by_d = by_d + 1; }
if (l [city] [i] == 1) {by_f = by_f + 1; }
}
please put code in code tags so it formats and highlights etc.
assuming you meant by_f ...
I see 2 issues.
your text says 0-4 is allowed.
your validation condition says otherwise.
by_f and by_d have no default value. no matter which condition(s) trigger the answer is making use of uninitialized values and spewing junk from that. They look like they should both start at zero.
Hint: You can hit "edit post", highlight your code and then press the <> formatting button. Note: This will not automatically indent your code! That part is up to you!
I've found it's easiest to copy and paste pre-indented code directly from the IDE, that way it is already properly formatted.
You can use the "preview" button at the bottom to see how it looks.
Using Code Tags, @Handy Andy, from cplusplus.com
Other than that, pay attention to what people say here, and don't expect them to just write your code for you.