0

cout<<count<<endl; sould provide an output according to the conditions , but it isn't printing anything, what is the fault/error/defects in the code that are causing such results ? it is my first question , sorry if i am not completely understandable.

i used the following code , i can't understand whats happening here.this is a simple input output question. the output provides us info about matching two team's uniform.

#include <stdio.h>
#include <iostream>

using namespace std;

main(){
    int a;
    cin>>a;
    int **b;
    b=new int*[a];
    for (int i = 0; i < a; i++)
    {
        b[i]=new int[2];
        for (int j = 0; j <2 ; j++)
        {
            cin>>b[i][j];
        }
    }

    int count=0;
    for (int i = 0; i < a*(a-1); i++)
    {   for (int j = 0; j < a; j++)
            if (b[i][0]==b[j][1])
                count=count+1;
    }
    cout<<count<<endl;

    for (size_t i = 0; i < a; i++)
    {
        delete b[i];
    }
    delete b;

}

input:

3
1 2
2 4
3 4

output does not contain anything

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
dev-k
  • 3
  • 2
  • 2
    If the `cout` never prints anything at all, then probably your program is interrupted by a segmentation fault prior to it. Think about the size of the array you allocate for `b` and the largest index of `b` that you access in the middle loop. What will happen if `a` is a large enough value? Also your code is not valid C++, because `main` must have a specified return type `int`. Furthermore you should be using `std::vector` and `std::string` in C++ instead of manually allocating arrays, in almost all cases. You are learning a bad/wrong programming style here. – walnut Sep 08 '19 at 12:32
  • thank you , I got it where I when wrong, there is an out of bound exception in my code. Actually i just switched from java to c++ ,its been a long time coding in c++. – dev-k Sep 09 '19 at 13:42
  • Sorry if I mistook it, but code like this is often posted here by students who seem to be learning more C than C++ (e.g. allocating arrays instead of using `std::vector`) and often with completely out-dated tools, sometimes even pre-standardization C++ compilers (where `main` without declared `int` return type might have been valid). I don't know much Java, but from what I have been hearing on here, most of what seems similar to Java in C++, actually isn't and causes confusion for programmers coming from Java. For example `new` is only supposed to be used in special circumstances. – walnut Sep 09 '19 at 13:49
  • Thanks for helping , I just switched from java to c++ therefor struggling in some stuffs. Can you pls help me more in how can i use 'std::string' and 'std::vector' in such cases and where can i learn a good programming style (a guide or tutorials or anthing which can help ). – dev-k Sep 09 '19 at 13:50
  • See here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – walnut Sep 09 '19 at 13:52

1 Answers1

1

You use the array out of bounds and delete when you should delete[]. Comments in code:

#include <iostream> // use the correct headers
#include <cstddef>

// not recommended: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice 
using namespace std;

int main() {  // main must return int
    size_t a; // better type for an array size
    cin >> a;
    int** b;
    b = new int*[a];
    for(size_t i = 0; i < a; i++) {
        b[i] = new int[2];
        for(size_t j = 0; j < 2; j++) {
            cin >> b[i][j];
        }
    }

    int count = 0;
    std::cout << a * (a - 1) << "\n"; // will print 6 for the given input
    for(size_t i = 0; i < a * (a - 1); i++) {
        // i will be in the range [0, 5]
        for(size_t j = 0; j < a; j++)             
            if(b[i][0] == b[j][1]) count = count + 1;
              // ^ undefined behaviour
    }
    cout << count << endl;

    for(size_t i = 0; i < a; i++) {
        delete[] b[i]; // use delete[] when you've used new[]
    }
    delete[] b; // use delete[] when you've used new[]
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108