Need help with struc coding

closed account (ETA9216C)
I need help with this piece. I have not learned enough of "data structure?", if that's whats it's called.
I manage to get some of it done with some help from my professor. I am still lost.
If you it's not any trouble, tell me how would this be done step by step.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

	  struct data
	  {
		  int n ;
		  int k ;
	  };

int binomial(int n,int k);

int main()
{
data coef;
char flag= 'y';

 while(flag=='y')
 {
	 get_input(coef);
	 //get_binomial_coefficient(n,k);
	print_output(binomial_coefficient);
 }
	system("pause");
	return 0;
}


 void get_input(data&coef)
 {
 cout<< "input value for n ";
 cin>>coef.n;
 cout<<"input value for k";
 cin>>coef.k;
 }

Much Appreciated , if i can get this done.
Last edited on
What do you want to achieve?
In which way does your program behave different to your expectations?
closed account (ETA9216C)
forgot to mention, i am suppose to create a program that computes the binomial coefficient.

closed account (ETA9216C)
the program is suppose to compute binomial coefficient, but i do not have enough info to complete the program.
Unfortunately, you are lost enough that you need to go see your professor.

You shouldn't actually need a struct in this program.

You are essentially being asked to compute a slice of Pascal's Triangle.
Your program will need a function to compute the factorial of a number.
As well as a function to compute the slice. There is a very good collection of formulas on Wikipedia for calculating (n k).

You may need some math help.

Good luck!
closed account (ETA9216C)
I probably don't have to , but for the sake of learning it's a must. He is just teaching the principal of it. i know how to calculate the binomial coefficient. I just never done it in code before.
closed account (ETA9216C)
revised
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

	  struct data
	  {
		  int n ;
		  int k ;
	  };

void getinput(data&);
void printoutput(data&);
void getbinomial_coefficient(data&);

int main()
{
    int n,k,binomial;
    data coef;
    char flag= 'y';

 while(flag=='y')
 {
	 getinput(coef);
	 getbinomial_coefficient(bc);
     printoutput(binomial);
 }
	system("pause");
	return 0;
}


 void getinput(data&coef)
 {
 cout<< "input value for n ";
 cin>>coef.n;
 cout<<"input value for k";
 cin>>coef.k;
 }

void getbinomial_coefficient(data&bc)
{
    int binomial,n,k;

    bc.binomial=factorial(bc.n)/(factorial(bc.k)*factorial(bc.n-bc.k));

}

void printoutput(data&b)
{

}
It would help you an awful lot if you were to try compiling your program here and there. Start small, add stuff, fix compile errors as they come.

Line 45: there is no member "binomial" in the "data" structure (lines 6-10).

And there shouldn't be. You should make your function return the bc.

1
2
3
4
int getbinomial_coefficient(data&bc)
{
    return factorial(bc.n)/(factorial(bc.k)*factorial(bc.n-bc.k));
}

Then in main you can say:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    data coef;
    int binomial;

    getinput(coef);
    binomial=getbinomial_coefficient(coef);
    printoutput(binomial);

    return 0;
}

Add stuff like loops later.

Also, you need to review the concept of "variable scope".

Hope this helps.
closed account (ETA9216C)
but i have getbinomial_coefficient as void. I even have in the header or prototype before main.

here is what i have so far :(made some changes)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

	  struct data
	  {
		  int n ;
		  int k ;
	  };

void getinput(data&);
void printoutput(data&);
void getbinomial_coefficient(data&);

int main()
{
    data coef;
    int binomial;

    char flag= 'y';
    {
    while(flag=='y')
 {
	 getinput(coef);
     getbinomial_coefficient(coef);
     printoutput(coef);
 }
	system("pause");
	return 0;
}


 void getinput(data&coef);
 {
 cout<< "input value for n ";
 cin>>coef.n;
 cout<<"input value for k";
 cin>>coef.k;
 }

void getbinomial_coefficient(data&bc);
{
    int binomial,n,k;

    bc.binomial=factorial(bc.n)/(factorial(bc.k)*factorial(bc.n-bc.k));

}


i've got " bc was not declared" error.
but i have getbinomial_coefficient as void. I even have in the header or prototype before main.

I hope you are trying to tell me that your professor requires this prototype, and not that you are unwilling to do something smart.

The code you just posted won't compile for a number of reasons.
Get rid of line 22 and try again.
closed account (ETA9216C)
not that i was trying to do something smart, i don't think it was required either. He put the format on the board, i just took as a template. So,I assumed it would be void getbinomial_coefficient.
Last edited on
I didn't mean to sound mean. Sorry.

What I meant was, I assume that you were required to use

void getbinomial_coefficient( data & bc )

instead of

int getbinomial_coefficient( data & bc )

? Because the first is not a good design.
closed account (ETA9216C)
i would assume so that i am suppose to use void getbinomial_coefficient.
When i went ahead to compile and got a few errors.
1. 'struct data' has no member named 'binomial'|
does that mean i need to declare binomial in struct data?

2. 'factorial' was not declared in this scope|
For this would need to use a for loop? like this?
1
2
3
4
for(i=1,i<=j,i++)
factorial =factorial*i
binomial= .....


3.expected ';' before 'bc'
i dont know what this means.
1) I've already answered that question.
2) You obviously need to go see your professor for more specialized help.
3) You are misplacing ';'s.
closed account (ETA9216C)
It looks like this is far as this goes i guess.

I really appreciate your help.
Topic archived. No new replies allowed.