i need help with some questions in c++

Discussion in 'C++' started by mimkst, Jun 2, 2010.

  1. mimkst

    mimkst New Member

    Jul 14, 2009
    Likes Received:
    Trophy Points:
    Could you please help me with this home work , since i'm not sure from my sol. , the last question i know it , but number 3 and 2 i didn't understand them .

    2. Write a program that create an array Divisors[N] with the following values. Divisors[0]=0 and Divisors is the number of divisors of number i. Use this array to print the number of divisors for all even numbers less than or equal to 20.


    3. Combinatorial numbers C(n,k) can be defined in a recursive form as
    C(n,k)=C(n-1,k) + C(n-1,k-1); for n>k>0
    C(n,n)=C(n,0)=1; for n>=0.
    Write a program, that create an array Comb[M][M] where Comb[j]=C(i,j) for i≥j and 0 otherwise, and M=10. After creating this array your program should find and print the sum of values Comb[j] for each row i.

    4. Write a function that for each column in a 2-dimensional array Arr2D[M][N] finds the average value, and then stores these values in array AvrCol[N]. Design the function and write a short driver program to test it.

    thank you :)
  2. xpi0t0s

    xpi0t0s Mentor

    Aug 6, 2004
    Likes Received:
    Trophy Points:
    Senior Support Engineer
    3. Looks like all values will be positive, so create an int array Comb initialised to -1 in every element.
    Then you need 2 nested for loops for i and j each from 0 to M-1 inclusive.
    You can calculate Comb[j] as shown; Comb[j] = Comb[i-1][j] + Comb[i-1][j-1] if i and j are both greater than zero. This should be a recursive function.
    If Comb[i-1][j] and/or Comb[i-1][j-1] are -1, i.e. uninitialised, then calculate those values (by recursing again) and set the array accordingly, and if not then just pull the values out of the array. Essentially the array is an optimisation; you can calculate all values without using the array, but it's quicker if you have intermediate values stored (although for just 100 numbers it'll actually be quicker NOT to optimise, but this won't scale to 1000s of values).

    The recursive function will first need to check if i or j is 0, and if i==j, and if i<j according to the problem description as the behavior differs in these cases (i.e. the value is NOT Comb[i-1][j] + Comb[i-1][j-1]). Just handle each of these concepts one by one and it will become clear.

    2 is also pretty easy. Start with a program that just calculates and counts the divisors of a number. If you don't know what a divisor is then Google it and look for a Wikipedia article on it. Once you can calculate the number of divisors of any number, it's then trivial to loop for i=0..20, Divisors=(number of divisors of i).

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice