Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   i need help with some questions in c++ (http://www.go4expert.com/forums/i-help-questions-cpp-t22294/)

mimkst 2Jun2010 15:45

i need help with some questions in c++
 
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[i] 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[i][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[i][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 :)

xpi0t0s 3Jun2010 16:40

Re: i need help with some questions in c++
 
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[i][j] as shown; Comb[i][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[i]=(number of divisors of i).


All times are GMT +5.5. The time now is 09:52.