#What's wrong with my code?

25 messages · Page 1 of 1 (latest)

civic marten
#

I was trying to solve problem 1512 on Leet Code, which demands to find all the good pairs of numbers in an array. I wrote code with two functions, but it doesn't work for some reason.

#include <iostream>
using namespace std;
class findthenecessarynumbers(int i,j) {
const int N = 10;
int A[N];
for(int i = 0; i < N; i++)
cin >> A[i];

int seen[N];
for(int i = 0; i < N; i++)
seen[i] = 0;

for(int i = 0; i < N; i++) {
if(seen[i] == 0) {
int count = 0;
for(int j = i; j < N; j++)
if(A[j] == A[i]) {
count += 1;
seen[j] = 1;
}
cout << A[i] << " occurs " << count << " times" << endl;
}
}

return 0;
}
class Solution {
public:
int numIdenticalPairs(int nums[6], int i, int j) {
cin>>i;
cin>>j;
cin>>nums[i];
cin>>nums[j];
if(nums[i] == nums[j] && i<=j)
{
if(nums[i]!=0 || nums[j]!=0)
{
cout<<findthenecessarynumbers(i,j)<<endl;
}
}
else{cout<<0<<endl;}
}
};

warm trellisBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

brave heron
#

In which way does it not work?

warm trellisBOT
#
How to Ask A Programming Question

Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.

What To Post

State your problem clearly and provide all necessary details:

  • the relevant portion of your code, or all of it
  • the expected output
  • the actual output (or the full error) 🏆 Gold Standard: Minimal Reproducible Example
Where To Post

Provide the relevant code in the message, and format it nicely with a code block*. If it's too much for one message, you can upload it:

  • Compiler Explorer for most C/C++ snippets
  • OnlineGDB for interaction, debugging ⛔ Do not post screenshots, let alone photos of your screen!
warm trellisBOT
#

I was trying to solve problem 1512 on Leet Code, which demands to find all the good pairs of numbers in an array. I wrote code with two functions, but it doesn't work for some reason.

#include <iostream>
using namespace std;
class findthenecessarynumbers(int i, j) {
 const int N = 10;
 int A[N];
 for (int i = 0; i < N; i++)
   cin >> A[i];

 int seen[N];
 for (int i = 0; i < N; i++)
   seen[i] = 0;

 for (int i = 0; i < N; i++) {
   if (seen[i] == 0) {
     int count = 0;
     for (int j = i; j < N; j++)
       if (A[j] == A[i]) {
         count += 1;
         seen[j] = 1;
       }
     cout << A[i] << " occurs " << count << " times" << endl;
   }
 }

 return 0;
}
class Solution {
public:
 int numIdenticalPairs(int nums[6], int i, int j) {
   cin >> i;
   cin >> j;
   cin >> nums[i];
   cin >> nums[j];
   if (nums[i] == nums[j] && i <= j) {
     if (nums[i] != 0 || nums[j] != 0) {
       cout << findthenecessarynumbers(i, j) << endl;
     }
   } else {
     cout << 0 << endl;
   }
 }
};
regulareverydaynormalmfer
brave herald
#

@civic marten A couple things to know

#
  1. Try to not do cin inside a class
#

Just pass them to the function as an array or something

#
  1. what's class findthenecessarynumbers(int i, j)
#

Are you defining a function

warm trellisBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

warm trellisBOT
#

This question thread is being automatically marked as stale.

civic marten
warm trellisBOT
brave herald
#
return_type function_name(parameters) {}
#

like this

civic marten
#

What is something else I should fix?

brave heron
#

Arshia mentioned not using cin inside classes too

#

In numIdenticalPairs you never check if j and i aren’t too big or too small

#

If you input 300 there when your array is of length 6 the program is going to crash

#

Also that function never returns anything

brave herald
# brave heron Also that function never returns anything

Yeah
I suggest this

#include <iostream>
using namespace std;

- class findthenecessarynumbers(int i, j) {
+ int* find_the_necessary_numbers(int A[]) {
  const int N = 10;
-  int A[N];
-  for (int i = 0; i < N; i++)
-    cin >> A[i];

+ int count[N];

  int seen[N];
  for (int i = 0; i < N; i++)
    seen[i] = 0;

  for (int i = 0; i < N; i++) {
    if (seen[i] == 0) {
      int count = 0;
      for (int j = i; j < N; j++)
        if (A[j] == A[i]) {
-          count += 1;
+          count[i]++;
          seen[j] = 1;
        }
-      cout << A[i] << " occurs " << count << " times" << endl;
    }
  }

-  return 0;
+  return count;
}
-class Solution {
- public:
-   int numIdenticalPairs(int nums[6], int i, int j) {
+ int num_identical_pairs(int nums[N], int i, int j) {
-   cin >> i;
-   cin >> j;
-   cin >> nums[i];
-   cin >> nums[j];
    if (nums[i] == nums[j] && i <= j) {
      if (nums[i] != 0 || nums[j] != 0) {
-        cout << findthenecessarynumbers(i, j) << endl;
+        return find_the_necessary_numbers(nums);
      }
      } else {
-        cout << 0 << endl;
+        return 0;
    }
}
-};

+ int main() {
+   const int N = 10;
+   int A[N];
+   for(int i = 0; i < N; ++i)
+      cin >> A[i];
+   
+   return 0;
+ }
#

Even then your code is a bit weird

brave heron
#

For one there is no main