-1

The problem is to take 3 inputs: 1) No. of test cases 2) No. of digits in a number 3) N space separated digit numbers

And to output : 1) No. of sets 2) No. of combinations in each set

I want to print those outputs but No of combination output is returning zero on each and every set

I've already tried troubleshooting and debugging the problem, but none of those worked....

/* Read input from STDIN. Print your output to STDOUT*/
#include<iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

using namespace std;
int factorial (int count);

int main(int argc, char *a[])
{
//intialize variables
int i,T,b,S[i],N,NN[i],C[i],count=0;

cin >> T;

while(T>0) {
    cin >> N;


    for(i=0;i<N;i++) {


        cin >> NN[i];


        if(i<N-1) {

            S[i] = (N-i);// S[i] is Category 02

               count++;
       }//end of if

      }//end of for loop


     for(int j=0;j<N;j++) {
        C[i] = factorial(count)/(factorial(i)*factorial(count - i));//          
    }//end of for loop
      cout <<"No. of sets =" <<count++<<endl;

     for(int k=0;k<N;k++) {

        cout<<"No.of combinations on each set :";
        cout<<C[i]<<endl;
    }  // end fo for loop  

}//end of while loop

return 0;
}//end of main

int factorial(int count)
{
int i;
for(i = count-1; i > 1; i--)
    count *= i;

return count ;
}//end of function

THIS OUTPUT IS COMING: "No. of combinations on set 0 : 0" "No. of combinations on set 1 : 0" ………….

enter image description here

  • If anyone has a solution to this problem statement also, please let me know ! – Aman khan Roohaani Aug 04 '19 at 05:57
  • One way to help readability and debugging of your code is to use actual meaningful variable names. This isn't slideware or the 1960s where space is limited. – Casey Aug 04 '19 at 06:03
  • A new code solution to this task would also be well appreciated – Aman khan Roohaani Aug 04 '19 at 06:04
  • 1
    `NN[i],C[i]` -- This is not valid C++. Arrays in C++ must have their sizes denoted by a constant expression, not a variable. Use `std::vector NN(i)`; `std::vector C(i);` instead. – PaulMcKenzie Aug 04 '19 at 06:05
  • Yeah I lack some basics ! ,Can you suggest me the solution so that I can compare and contrast what irresponsible mistakes am I doing ? That would certainly help me – Aman khan Roohaani Aug 04 '19 at 06:11
  • 1
    @AmankhanRoohaani Stop using raw c-style arrays with c++ code please! Rather use `std::vector` or `std::array`. – πάντα ῥεῖ Aug 04 '19 at 06:12
  • 1
    Have a look at [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) and a solid recommendation to stop by [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?r=SearchResults&s=1|595.2486) – David C. Rankin Aug 04 '19 at 06:14

1 Answers1

1

Well it's gone wrong already here

//intialize variables
int i,T,b,S[i],N,NN[i],C[i],count=0;

What's the value of i here? Answer, it doesn't have one. If i doesn't have a value then what's the size of this array NN[i]? Answer, who knows.

When you declare an array in C++, you must give it a size. The size cannot be a variable, it must be a cosntant. And it especialy cannot be a variable without a value.

Your program has undefined behaviour.

EDIT - this would be an improvement

#include <vector>

int main()
{
    int T;
    cin >> T;
    while (T > 0)
    {
        int N;
        cin >> N;
        std::vector<int> NN(N), S(N);
        for (int i = 0; i < N; i++)
        {
            ...

First improvement is that I using a std::vector instead of an array. Unlike arrays vectors can have variable sizes. Second improvement is that I only declare variables when I need them, I don't declare all the variables at the start of the function. So I only declare NN and S when I know what the value of N is, so I know how big the vectors need to be.

john
  • 85,011
  • 4
  • 57
  • 81
  • I know, please consider me a noobie, can you suggest some solutions or a new code which does the task? – Aman khan Roohaani Aug 04 '19 at 06:05
  • I don't care about my profile reputation as long as I'm learning – Aman khan Roohaani Aug 04 '19 at 06:06
  • 1
    `#define MAXI 1024` (or some reasonable number for your situation -- but don't skimp!) and then `int ... S[MAXI] = {0}, ... NN[MAXI] = {0}, C[MAXI] = {0},...` (when you are learning, it is a good idea to initialize ALL variables, especially arrays) Or, since this is tagged C++, you can use `std:vector S, NN, C;` – David C. Rankin Aug 04 '19 at 06:10
  • 1
    @AmankhanRoohaani See editted question for some suggested improvements. – john Aug 04 '19 at 06:13