Help C++

First, I wish you all a good day.

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?

#include <iostream>
#include <math.h>
#include <locale.h>
#set size 4
using std namespace;

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,


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; }

cout << "there are" << by_f << "outgoing roads and" << by_d << "incoming roads"; }

/// Question B

else if (opera_sum_or_subtra == 2) {

cities = - 1;
counter_2 = - 1;

for (m = 0; m < size; m = m + 1) {

for (m = 0; m <size; i ++) {
counter = 0;

for (j = 0; j <size; j ++) {
if (l [j] [m] == 1) {
counter ++;
if (counter> counter_2) {
counter_2 = counter;
cities = m;

cout << "The cities that have more outputs and entries are:" << cities << "with" << counter_2-1 << endl;

/// Question C

else if (opera_sum_or_subtra == 3) {

/// Question of

else if (opera_sum_or_subtra == 4) {

return 0;

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.
Last edited on
@The apprentice,
PLEASE USE CODE TAGS (the <> formatting button to the right), when posting code!

Along with the proper indenting, it makes it easier to read your code, and thus also easier to respond to your post.

Tutorials on how to use code tags:

I've found the second link to be the most help.

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

Other than that, pay attention to what people say here, and don't expect them to just write your code for you.

Topic archived. No new replies allowed.